/lib/pylon/mixin/convert_to_class_name.rb

https://github.com/fujin/pylon · Ruby · 70 lines · 36 code · 11 blank · 23 comment · 5 complexity · b95ef4430ddbf3690c7332253d285dd2 MD5 · raw file

  1. #
  2. # Author:: Adam Jacob (<adam@opscode.com>)
  3. # Author:: Christopher Walters (<cw@opscode.com>)
  4. # Copyright:: Copyright (c) 2008, 2009 Opscode, Inc.
  5. # License:: Apache License, Version 2.0
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  16. # implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. # Taken from Opscode's Chef @
  21. # chef/lib/chef/mixin/convert_to_class_name.rb
  22. # Used for Knifes' subcommand loader.
  23. # Repurposed for pylons command handler.
  24. class Pylon
  25. module Mixin
  26. module ConvertToClassName
  27. extend self
  28. def convert_to_class_name(str)
  29. rname = nil
  30. regexp = %r{^(.+?)(_(.+))?$}
  31. mn = str.match(regexp)
  32. if mn
  33. rname = mn[1].capitalize
  34. while mn && mn[3]
  35. mn = mn[3].match(regexp)
  36. rname << mn[1].capitalize if mn
  37. end
  38. end
  39. rname
  40. end
  41. def convert_to_snake_case(str, namespace=nil)
  42. str = str.dup
  43. str.sub!(/^#{namespace}(\:\:)?/, '') if namespace
  44. str.gsub!(/[A-Z]/) {|s| "_" + s}
  45. str.downcase!
  46. str.sub!(/^\_/, "")
  47. str
  48. end
  49. def snake_case_basename(str)
  50. with_namespace = convert_to_snake_case(str)
  51. with_namespace.split("::").last.sub(/^_/, '')
  52. end
  53. def filename_to_qualified_string(base, filename)
  54. file_base = File.basename(filename, ".rb")
  55. base.to_s + (file_base == 'default' ? '' : "_#{file_base}")
  56. end
  57. end
  58. end #ConvertToClassName
  59. end #Pylon