Module: Extlib::Inflection
- Defined in:
- lib/extlib/inflection.rb
Class Method Summary collapse
-
.camelize(lower_case_and_underscored_word, *args) ⇒ Object
By default, camelize converts strings to UpperCamelCase.
-
.classify(name) ⇒ Object
Take an underscored name and make it into a camelized name.
-
.constantize(camel_cased_word) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string.
-
.demodulize(class_name_in_module) ⇒ Object
Removes the module part from the expression in the string.
-
.foreign_key(class_name, key = "id") ⇒ Object
Creates a foreign key name from a class name.
-
.humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips _id.
-
.pluralize(word) ⇒ Object
Returns the plural form of the word in the string.
-
.singularize(word) ⇒ Object
The reverse of pluralize, returns the singular form of a word in a string.
-
.tableize(class_name) ⇒ Object
Create the name of a table like Rails does for models to table names.
-
.underscore(camel_cased_word) ⇒ Object
The reverse of
camelize
.
Class Method Details
.camelize(lower_case_and_underscored_word, *args) ⇒ Object
By default, camelize converts strings to UpperCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces
35 36 37 |
# File 'lib/extlib/inflection.rb', line 35 def camelize(lower_case_and_underscored_word, *args) lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end |
.classify(name) ⇒ Object
Take an underscored name and make it into a camelized name
23 24 25 |
# File 'lib/extlib/inflection.rb', line 23 def classify(name) camelize(singularize(name.to_s.sub(/.*\./, ''))) end |
.constantize(camel_cased_word) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
102 103 104 105 106 107 108 |
# File 'lib/extlib/inflection.rb', line 102 def constantize(camel_cased_word) unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end Object.module_eval("::#{$1}", __FILE__, __LINE__) end |
.demodulize(class_name_in_module) ⇒ Object
Removes the module part from the expression in the string
71 72 73 |
# File 'lib/extlib/inflection.rb', line 71 def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end |
.foreign_key(class_name, key = "id") ⇒ Object
Creates a foreign key name from a class name.
91 92 93 |
# File 'lib/extlib/inflection.rb', line 91 def foreign_key(class_name, key = "id") underscore(demodulize(class_name.to_s)) << "_" << key.to_s end |
.humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.
62 63 64 |
# File 'lib/extlib/inflection.rb', line 62 def humanize(lower_case_and_underscored_word) lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
.pluralize(word) ⇒ Object
Returns the plural form of the word in the string.
135 136 137 |
# File 'lib/extlib/inflection.rb', line 135 def pluralize(word) English::Inflect.plural(word) end |
.singularize(word) ⇒ Object
The reverse of pluralize, returns the singular form of a word in a string. Wraps the English gem
121 122 123 |
# File 'lib/extlib/inflection.rb', line 121 def singularize(word) English::Inflect.singular(word) end |
.tableize(class_name) ⇒ Object
Create the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.
82 83 84 |
# File 'lib/extlib/inflection.rb', line 82 def tableize(class_name) pluralize(underscore(class_name)) end |
.underscore(camel_cased_word) ⇒ Object
The reverse of camelize
. Makes an underscored form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
48 49 50 51 52 53 54 |
# File 'lib/extlib/inflection.rb', line 48 def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |