Class: DocumentHydrator::Inflector::Inflections
- Inherits:
-
Object
- Object
- DocumentHydrator::Inflector::Inflections
- Defined in:
- lib/document_hydrator/inflector/inflections.rb
Instance Attribute Summary collapse
-
#plurals ⇒ Object
readonly
Returns the value of attribute plurals.
-
#uncountables ⇒ Object
readonly
Returns the value of attribute uncountables.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Inflections
constructor
A new instance of Inflections.
-
#irregular(singular, plural) ⇒ Object
Specifies a new irregular that applies to both pluralization and singularization at the same time.
-
#plural(rule, replacement) ⇒ Object
Specifies a new pluralization rule and its replacement.
-
#uncountable(*words) ⇒ Object
Add uncountable words that shouldn’t be attempted inflected.
Constructor Details
#initialize ⇒ Inflections
Returns a new instance of Inflections.
11 12 13 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 11 def initialize @plurals, @uncountables, @humans = [], [], [] end |
Instance Attribute Details
#plurals ⇒ Object (readonly)
Returns the value of attribute plurals.
9 10 11 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 9 def plurals @plurals end |
#uncountables ⇒ Object (readonly)
Returns the value of attribute uncountables.
9 10 11 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 9 def uncountables @uncountables end |
Class Method Details
.instance ⇒ Object
5 6 7 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 5 def self.instance @__instance__ ||= new end |
Instance Method Details
#irregular(singular, plural) ⇒ Object
Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used for strings, not regular expressions. You simply pass the irregular in singular and plural form.
Examples:
irregular 'octopus', 'octopi'
irregular 'person', 'people'
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 29 def irregular(singular, plural) @uncountables.delete(singular) @uncountables.delete(plural) if singular[0,1].upcase == plural[0,1].upcase plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1]) plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1]) else plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1]) plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1]) plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1]) plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1]) end end |
#plural(rule, replacement) ⇒ Object
Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. The replacement should always be a string that may include references to the matched data from the rule.
17 18 19 20 21 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 17 def plural(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @plurals.insert(0, [rule, replacement]) end |
#uncountable(*words) ⇒ Object
Add uncountable words that shouldn’t be attempted inflected.
Examples:
uncountable "money"
uncountable "money", "information"
uncountable %w( money information rice )
49 50 51 |
# File 'lib/document_hydrator/inflector/inflections.rb', line 49 def uncountable(*words) (@uncountables << words).flatten! end |