module Xcodeproj::ActiveSupport::Inflector
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
"active_record/errors".camelize # => "ActiveRecord::Errors"
"active_record".camelize(:lower) # => "activeRecord"
"active_record".camelize # => "ActiveRecord"
Examples:
+camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
is set to :lower then +camelize+ produces lowerCamelCase.
By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
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.first.downcase + camelize(lower_case_and_underscored_word)[1..-1] end end
def classify(table_name)
Singular names are not handled correctly:
"posts".classify # => "Post"
"egg_and_hams".classify # => "EggAndHam"
Examples:
follow +classify+ with +constantize+.)
Note that this returns a string and not a Class. (To convert to an actual class
Create a class name from a plural table name like Rails does for table names to models.
def classify(table_name) # strip out any leading schema name camelize(singularize(table_name.to_s.sub(/.*\./, ''))) end
def dasherize(underscored_word)
Example:
Replaces underscores with dashes in the string.
def dasherize(underscored_word) underscored_word.gsub(/_/, '-') end
def demodulize(class_name_in_module)
"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
Examples:
Removes the module part from the expression in the string.
def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end
def humanize(lower_case_and_underscored_word)
"employee_salary" # => "Employee salary"
Examples:
trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
Capitalizes the first word and turns underscores into spaces and strips a
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
def inflections
inflect.uncountable "rails"
ActiveSupport::Inflector.inflections do |inflect|
Example:
inflector rules.
Yields a singleton instance of Inflector::Inflections so you can specify additional
def inflections if block_given? yield Inflections.instance else Inflections.instance end end
def ordinalize(number)
ordinalize(1002) # => "1002nd"
ordinalize(2) # => "2nd"
ordinalize(1) # => "1st"
Examples:
ordered sequence such as 1st, 2nd, 3rd, 4th.
Turns a number into an ordinal string used to denote the position in an
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
def pluralize(word)
"words".pluralize # => "words"
"sheep".pluralize # => "sheep"
"octopus".pluralize # => "octopi"
"post".pluralize # => "posts"
Examples:
Returns the plural form of the word in the string.
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
def singularize(word)
"word".singularize # => "word"
"sheep".singluarize # => "sheep"
"octopi".singularize # => "octopus"
"posts".singularize # => "post"
Examples:
The reverse of +pluralize+, returns the singular form of a word in a string.
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
def titleize(word)
"man from the boondocks".titleize # => "Man From The Boondocks"
Examples:
+titleize+ is also aliased as as +titlecase+.
used in the Rails internals.
a nicer looking title. +titleize+ is meant for creating pretty output. It is not
Capitalizes all the words and replaces some characters in the string to create
def titleize(word) humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize } end
def underscore(camel_cased_word)
"ActiveRecord".underscore # => "active_record"
Examples:
Changes '::' to '/' to convert namespaces to paths.
The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
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