Module: Extlib::Inflection

Defined in:
lib/extlib/inflection.rb

Class Method Summary collapse

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

Examples:

"active_record".camelize #=> "ActiveRecord"
"active_record/errors".camelize #=> "ActiveRecord::Errors"


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

Examples:

"egg_and_hams".classify #=> "EggAndHam"
"post".classify #=> "Post"


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.

Examples:

"Module".constantize #=> Module
"Class".constantize #=> Class


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

Examples:

"ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
"Inflections".demodulize #=> "Inflections"


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.

Examples:

"Message".foreign_key #=> "message_id"
"Admin::Post".foreign_key #=> "post_id"


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.

Examples:

"employee_salary" #=> "Employee salary"
"author_id" #=> "Author"


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.

Examples:

"post".pluralize #=> "posts"
"octopus".pluralize #=> "octopi"
"sheep".pluralize #=> "sheep"
"words".pluralize #=> "words"
"the blue mailman".pluralize #=> "the blue mailmen"
"CamelOctopus".pluralize #=> "CamelOctopi"


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

Examples:

"posts".singularize #=> "post"
"octopi".singularize #=> "octopus"
"sheep".singluarize #=> "sheep"
"word".singluarize #=> "word"
"the blue mailmen".singularize #=> "the blue mailman"
"CamelOctopi".singularize #=> "CamelOctopus"


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.

Examples:

"RawScaledScorer".tableize #=> "raw_scaled_scorers"
"egg_and_ham".tableize #=> "egg_and_hams"
"fancyCategory".tableize #=> "fancy_categories"


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.

Examples:

"ActiveRecord".underscore #=> "active_record"
"ActiveRecord::Errors".underscore #=> active_record/errors


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