Module: Beatport::Support::Inflector
Instance Method Summary collapse
- #camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
- #constantize(camel_cased_word) ⇒ Object
- #process_keys(obj, &block) ⇒ Object
- #underscore(camel_cased_word) ⇒ Object
Instance Method Details
#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/beatport/support/inflector.rb', line 17 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1] end end |
#constantize(camel_cased_word) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/beatport/support/inflector.rb', line 6 def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end |
#process_keys(obj, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/beatport/support/inflector.rb', line 35 def process_keys(obj, &block) case obj when Hash Hash[obj.map {|k, v| [yield(k), process_keys(v, &block)]}] when Array obj.map {|o| process_keys(o, &block)} else obj end end |
#underscore(camel_cased_word) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/beatport/support/inflector.rb', line 25 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |