Module: Inflector
Instance Method Summary collapse
- #camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
-
#constantize(camel_cased_word) ⇒ Object
:nodoc:.
- #dasherize(underscored_word) ⇒ Object
- #demodulize(class_name_in_module) ⇒ Object
- #foreign_key(class_name, separate_class_name_and_id_with_underscore = true) ⇒ Object
- #ordinalize(number) ⇒ Object
- #underscore(camel_cased_word) ⇒ Object
Instance Method Details
#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/ext/string.rb', line 4 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
:nodoc:
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ext/string.rb', line 46 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 |
#dasherize(underscored_word) ⇒ Object
22 23 24 |
# File 'lib/ext/string.rb', line 22 def dasherize(underscored_word) underscored_word.gsub(/_/, '-') end |
#demodulize(class_name_in_module) ⇒ Object
26 27 28 |
# File 'lib/ext/string.rb', line 26 def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end |
#foreign_key(class_name, separate_class_name_and_id_with_underscore = true) ⇒ Object
30 31 32 |
# File 'lib/ext/string.rb', line 30 def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") end |
#ordinalize(number) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ext/string.rb', line 58 def ordinalize(number) if (11..13).include?(number.to_i % 100) "#{number}th" else case number.to_i % 10 when 1; "#{number}st" when 2; "#{number}nd" when 3; "#{number}rd" else "#{number}th" end end end |
#underscore(camel_cased_word) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/ext/string.rb', line 12 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 |