0
@@ -5,270 +5,272 @@ require 'singleton'
0
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
0
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
0
- # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
0
- # inflection rules. Examples:
0
- # Inflector.inflections do |inflect|
0
- # inflect.plural /^(ox)$/i, '\1\2en'
0
- # inflect.singular /^(ox)en/i, '\1'
0
- # inflect.irregular 'octopus', 'octopi'
0
- # inflect.uncountable "equipment"
0
- # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
0
- # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
0
- # already have been loaded.
0
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
0
+ # inflection rules. Examples:
0
+ # Inflector.inflections do |inflect|
0
+ # inflect.plural /^(ox)$/i, '\1\2en'
0
+ # inflect.singular /^(ox)en/i, '\1'
0
+ # inflect.irregular 'octopus', 'octopi'
0
+ # inflect.uncountable "equipment"
0
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
0
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
0
+ # already have been loaded.
0
-
attr_reader :plurals, :singulars, :uncountables
0
+
attr_reader :plurals, :singulars, :uncountables
0
- @plurals, @singulars, @uncountables = [], [], []
0
+ @plurals, @singulars, @uncountables = [], [], []
0
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
0
+ # The replacement should always be a string that may include references to the matched data from the rule.
0
+ def plural(rule, replacement)
0
+ @plurals.insert(0, [rule, replacement])
0
- # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
0
- # The replacement should always be a string that may include references to the matched data from the rule.
0
- def plural(rule, replacement)
0
- @plurals.insert(0, [rule, replacement])
0
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
0
+ # The replacement should always be a string that may include references to the matched data from the rule.
0
+ def singular(rule, replacement)
0
+ @singulars.insert(0, [rule, replacement])
0
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
0
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
0
+ # irregular 'octopus', 'octopi'
0
+ # irregular 'person', 'people'
0
+ def irregular(singular, plural)
0
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
0
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
0
+ # Add uncountable words that shouldn't be attempted inflected.
0
+ # uncountable "money", "information"
0
+ # uncountable %w( money information rice )
0
+ def uncountable(*words)
0
+ (@uncountables << words).flatten!
0
+ # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
0
+ # the options are: :plurals, :singulars, :uncountables
0
+ def clear(scope = :all)
0
+ @plurals, @singulars, @uncountables = [], [], []
0
+ instance_variable_set "@#{scope}", []
0
- # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
0
- # The replacement should always be a string that may include references to the matched data from the rule.
0
- def singular(rule, replacement)
0
- @singulars.insert(0, [rule, replacement])
0
+ yield Inflections.instance
0
- # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
0
- # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
0
+ # Returns the plural form of the word in the string.
0
- # irregular 'octopus', 'octopi'
0
- # irregular 'person', 'people'
0
- def irregular(singular, plural)
0
- plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
0
- singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
0
+ # "post".pluralize #=> "posts"
0
+ # "octopus".pluralize #=> "octopi"
0
+ # "sheep".pluralize #=> "sheep"
0
+ # "words".pluralize #=> "words"
0
+ # "the blue mailman".pluralize #=> "the blue mailmen"
0
+ # "CamelOctopus".pluralize #=> "CamelOctopi"
0
+ result = word.to_s.dup
0
+ if inflections.uncountables.include?(result.downcase)
0
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
0
- #
Add uncountable words that shouldn't be attempted inflected.
0
+ #
The reverse of pluralize, returns the singular form of a word in a string.
0
- # uncountable "money", "information"
0
- # uncountable %w( money information rice )
0
- def uncountable(*words)
0
- (@uncountables << words).flatten!
0
+ # "posts".singularize #=> "post"
0
+ # "octopi".singularize #=> "octopus"
0
+ # "sheep".singluarize #=> "sheep"
0
+ # "word".singluarize #=> "word"
0
+ # "the blue mailmen".singularize #=> "the blue mailman"
0
+ # "CamelOctopi".singularize #=> "CamelOctopus"
0
+ result = word.to_s.dup
0
+ if inflections.uncountables.include?(result.downcase)
0
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
0
- # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
0
- # the options are: :plurals, :singulars, :uncountables
0
+ # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
0
+ # is set to ":lower" then camelize produces lowerCamelCase.
0
- def clear(scope = :all)
0
- @plurals, @singulars, @uncountables = [], [], []
0
- instance_variable_set "@#{scope}", []
0
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
0
+ # "active_record".camelize #=> "ActiveRecord"
0
+ # "active_record".camelize(:lower) #=> "activeRecord"
0
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
0
+ # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
0
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
0
+ if first_letter_in_uppercase
0
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
0
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
0
- yield Inflections.instance
0
+ # Capitalizes all the words and replaces some characters in the string to create
0
+ # a nicer looking title. Titleize is meant for creating pretty output. It is not
0
+ # used in the Rails internals.
0
+ # titleize is also aliased as as titlecase
0
+ # "man from the boondocks".titleize #=> "Man From The Boondocks"
0
+ # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
0
+ humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
0
- # Returns the plural form of the word in the string.
0
- # "post".pluralize #=> "posts"
0
- # "octopus".pluralize #=> "octopi"
0
- # "sheep".pluralize #=> "sheep"
0
- # "words".pluralize #=> "words"
0
- # "the blue mailman".pluralize #=> "the blue mailmen"
0
- # "CamelOctopus".pluralize #=> "CamelOctopi"
0
- result = word.to_s.dup
0
- if inflections.uncountables.include?(result.downcase)
0
- inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
0
+ # The reverse of +camelize+. Makes an underscored form from the expression in the string.
0
+ # Changes '::' to '/' to convert namespaces to paths.
0
+ # "ActiveRecord".underscore #=> "active_record"
0
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
0
+ def underscore(camel_cased_word)
0
+ camel_cased_word.to_s.gsub(/::/, '/').
0
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
0
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
0
- # The reverse of pluralize, returns the singular form of a word in a string.
0
- # "posts".singularize #=> "post"
0
- # "octopi".singularize #=> "octopus"
0
- # "sheep".singluarize #=> "sheep"
0
- # "word".singluarize #=> "word"
0
- # "the blue mailmen".singularize #=> "the blue mailman"
0
- # "CamelOctopi".singularize #=> "CamelOctopus"
0
- result = word.to_s.dup
0
- if inflections.uncountables.include?(result.downcase)
0
- inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
0
+ # Replaces underscores with dashes in the string.
0
+ # "puni_puni" #=> "puni-puni"
0
+ def dasherize(underscored_word)
0
+ underscored_word.gsub(/_/, '-')
0
- # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
0
- # is set to ":lower" then camelize produces lowerCamelCase.
0
- # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
0
- # "active_record".camelize #=> "ActiveRecord"
0
- # "active_record".camelize(:lower) #=> "activeRecord"
0
- # "active_record/errors".camelize #=> "ActiveRecord::Errors"
0
- # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
0
- def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
0
- if first_letter_in_uppercase
0
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
0
- lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
0
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
0
+ # Like titleize, this is meant for creating pretty output.
0
+ # "employee_salary" #=> "Employee salary"
0
+ # "author_id" #=> "Author"
0
+ def humanize(lower_case_and_underscored_word)
0
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
0
- # Capitalizes all the words and replaces some characters in the string to create
0
- # a nicer looking title. Titleize is meant for creating pretty output. It is not
0
- # used in the Rails internals.
0
- # titleize is also aliased as as titlecase
0
- # "man from the boondocks".titleize #=> "Man From The Boondocks"
0
- # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
0
- humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
0
- # The reverse of +camelize+. Makes an underscored form from the expression in the string.
0
- # Changes '::' to '/' to convert namespaces to paths.
0
- # "ActiveRecord".underscore #=> "active_record"
0
- # "ActiveRecord::Errors".underscore #=> active_record/errors
0
- def underscore(camel_cased_word)
0
- camel_cased_word.to_s.gsub(/::/, '/').
0
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
0
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
0
- # Replaces underscores with dashes in the string.
0
- # "puni_puni" #=> "puni-puni"
0
- def dasherize(underscored_word)
0
- underscored_word.gsub(/_/, '-')
0
- # Capitalizes the first word and turns underscores into spaces and strips _id.
0
- # Like titleize, this is meant for creating pretty output.
0
- # "employee_salary" #=> "Employee salary"
0
- # "author_id" #=> "Author"
0
- def humanize(lower_case_and_underscored_word)
0
- lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
0
+ # Removes the module part from the expression in the string
0
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
0
+ # "Inflections".demodulize #=> "Inflections"
0
+ def demodulize(class_name_in_module)
0
+ class_name_in_module.to_s.gsub(/^.*::/, '')
0
- # Removes the module part from the expression in the string
0
- # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
0
- # "Inflections".demodulize #=> "Inflections"
0
- def demodulize(class_name_in_module)
0
- class_name_in_module.to_s.gsub(/^.*::/, '')
0
+ # Create the name of a table like Rails does for models to table names. This method
0
+ # uses the pluralize method on the last word in the string.
0
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
0
+ # "egg_and_ham".tableize #=> "egg_and_hams"
0
+ # "fancyCategory".tableize #=> "fancy_categories"
0
+ def tableize(class_name)
0
+ pluralize(underscore(class_name))
0
- # Create the name of a table like Rails does for models to table names. This method
0
- # uses the pluralize method on the last word in the string.
0
- # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
0
- # "egg_and_ham".tableize #=> "egg_and_hams"
0
- # "fancyCategory".tableize #=> "fancy_categories"
0
- def tableize(class_name)
0
- pluralize(underscore(class_name))
0
+ # Create a class name from a table name like Rails does for table names to models.
0
+ # Note that this returns a string and not a Class. (To convert to an actual class
0
+ # follow classify with constantize.)
0
+ # "egg_and_hams".classify #=> "EggAndHam"
0
+ # "post".classify #=> "Post"
0
+ def classify(table_name)
0
+ # strip out any leading schema name
0
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
0
- # Create a class name from a table name like Rails does for table names to models.
0
- # Note that this returns a string and not a Class. (To convert to an actual class
0
- # follow classify with constantize.)
0
- # "egg_and_hams".classify #=> "EggAndHam"
0
- # "post".classify #=> "Post"
0
- def classify(table_name)
0
- # strip out any leading schema name
0
- camelize(singularize(table_name.to_s.sub(/.*\./, '')))
0
+ # Creates a foreign key name from a class name.
0
+ # +separate_class_name_and_id_with_underscore+ sets whether
0
+ # the method should put '_' between the name and 'id'.
0
+ # "Message".foreign_key #=> "message_id"
0
+ # "Message".foreign_key(false) #=> "messageid"
0
+ # "Admin::Post".foreign_key #=> "post_id"
0
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
0
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
0
- # Creates a foreign key name from a class name.
0
- # +separate_class_name_and_id_with_underscore+ sets whether
0
- # the method should put '_' between the name and 'id'.
0
- # "Message".foreign_key #=> "message_id"
0
- # "Message".foreign_key(false) #=> "messageid"
0
- # "Admin::Post".foreign_key #=> "post_id"
0
- def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
0
- underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
0
+ # Constantize tries to find a declared constant with the name specified
0
+ # in the string. It raises a NameError when the name is not in CamelCase
0
+ # or is not initialized.
0
+ # "Module".constantize #=> Module
0
+ # "Class".constantize #=> Class
0
+ def constantize(camel_cased_word)
0
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
0
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
0
- # Constantize tries to find a declared constant with the name specified
0
- # in the string. It raises a NameError when the name is not in CamelCase
0
- # or is not initialized.
0
- # "Module".constantize #=> Module
0
- # "Class".constantize #=> Class
0
- def constantize(camel_cased_word)
0
- unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
0
- raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
0
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
0
- Object.module_eval("::#{$1}", __FILE__, __LINE__)
0
- # Ordinalize turns a number into an ordinal string used to denote the
0
- # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
0
- # ordinalize(1) # => "1st"
0
- # ordinalize(2) # => "2nd"
0
- # ordinalize(1002) # => "1002nd"
0
- # ordinalize(1003) # => "1003rd"
0
- def ordinalize(number)
0
- if (11..13).include?(number.to_i % 100)
0
+ # Ordinalize turns a number into an ordinal string used to denote the
0
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
0
+ # ordinalize(1) # => "1st"
0
+ # ordinalize(2) # => "2nd"
0
+ # ordinalize(1002) # => "1002nd"
0
+ # ordinalize(1003) # => "1003rd"
0
+ def ordinalize(number)
0
+ if (11..13).include?(number.to_i % 100)
Comments
No one has commented yet.