Module: AridCache::Inflector
Defined Under Namespace
Classes: Inflections
Instance Method Summary collapse
-
#classify(table_name) ⇒ Object
Create a class name from a plural table name like Rails does for table names to models.
-
#humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing “_id”, if any.
-
#inflections ⇒ Object
Yields a singleton instance of Inflector::Inflections so you can specify additional inflector rules.
-
#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.
-
#titleize(word) ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
Instance Method Details
#classify(table_name) ⇒ Object
Create a class name from a plural table name like Rails does for table names to models. Note that this returns a string and not a Class. (To convert to an actual class follow classify
with constantize
.)
Examples:
"egg_and_hams".classify # => "EggAndHam"
"posts".classify # => "Post"
Singular names are not handled correctly:
"business".classify # => "Busines"
208 209 210 211 |
# File 'lib/arid_cache/inflector/inflections.rb', line 208 def classify(table_name) # strip out any leading schema name camelize(singularize(table_name.to_s.sub(/.*\./, ''))) end |
#humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing “_id”, if any. Like titleize
, this is meant for creating pretty output.
Examples:
"employee_salary" # => "Employee salary"
"author_id" # => "Author"
167 168 169 170 171 172 |
# File 'lib/arid_cache/inflector/inflections.rb', line 167 def humanize(lower_case_and_underscored_word) result = lower_case_and_underscored_word.to_s.dup inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } result.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
#inflections ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/arid_cache/inflector/inflections.rb', line 115 def inflections if block_given? yield Inflections.instance else Inflections.instance end 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"
"CamelOctopus".pluralize # => "CamelOctopi"
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/arid_cache/inflector/inflections.rb', line 131 def pluralize(word) result = word.to_s.dup if word.empty? || inflections.uncountables.include?(result.downcase) result else inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } result end end |
#singularize(word) ⇒ Object
The reverse of pluralize
, returns the singular form of a word in a string.
Examples:
"posts".singularize # => "post"
"octopi".singularize # => "octopus"
"sheep".singularize # => "sheep"
"word".singularize # => "word"
"CamelOctopi".singularize # => "CamelOctopus"
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/arid_cache/inflector/inflections.rb', line 150 def singularize(word) result = word.to_s.dup if inflections.uncountables.any? { |inflection| result =~ /#{inflection}\Z/i } result else inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } result end 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"
194 195 196 |
# File 'lib/arid_cache/inflector/inflections.rb', line 194 def tableize(class_name) pluralize(underscore(class_name)) end |
#titleize(word) ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize
is meant for creating pretty output. It is not used in the Rails internals.
titleize
is also aliased as as titlecase
.
Examples:
"man from the boondocks".titleize # => "Man From The Boondocks"
"x-men: the last stand".titleize # => "X Men: The Last Stand"
183 184 185 |
# File 'lib/arid_cache/inflector/inflections.rb', line 183 def titleize(word) humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize } end |