Class: Inflector::Words
- Inherits:
-
Object
- Object
- Inflector::Words
- Defined in:
- lib/inflector.rb
Instance Attribute Summary collapse
-
#plurals ⇒ Object
readonly
Returns the value of attribute plurals.
-
#singulars ⇒ Object
readonly
Returns the value of attribute singulars.
Class Method Summary collapse
Instance Method Summary collapse
- #add(singular, plural = nil) ⇒ Object
- #add_plural_rule(singular, plural) ⇒ Object
- #add_singular_rule(singular, plural) ⇒ Object
- #inflect(singular, plural) ⇒ Object
-
#initialize ⇒ Words
constructor
A new instance of Words.
- #write_plural_rules ⇒ Object
- #write_singular_rules ⇒ Object
Constructor Details
#initialize ⇒ Words
Returns a new instance of Words.
11 12 13 14 |
# File 'lib/inflector.rb', line 11 def initialize @singulars, @singular_rules = {}, [] @plurals, @plural_rules = {}, [] end |
Instance Attribute Details
#plurals ⇒ Object (readonly)
Returns the value of attribute plurals.
9 10 11 |
# File 'lib/inflector.rb', line 9 def plurals @plurals end |
#singulars ⇒ Object (readonly)
Returns the value of attribute singulars.
9 10 11 |
# File 'lib/inflector.rb', line 9 def singulars @singulars end |
Class Method Details
.instance ⇒ Object
54 55 56 |
# File 'lib/inflector.rb', line 54 def self.instance @__instance__ ||= new end |
Instance Method Details
#add(singular, plural = nil) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/inflector.rb', line 16 def add(singular, plural = nil) plural = singular unless plural @plurals[singular] = plural @singulars[plural] = singular end |
#add_plural_rule(singular, plural) ⇒ Object
32 33 34 |
# File 'lib/inflector.rb', line 32 def add_plural_rule(singular, plural) @plural_rules << [singular, plural] end |
#add_singular_rule(singular, plural) ⇒ Object
28 29 30 |
# File 'lib/inflector.rb', line 28 def add_singular_rule(singular, plural) @singular_rules << [plural, singular] end |
#inflect(singular, plural) ⇒ Object
23 24 25 26 |
# File 'lib/inflector.rb', line 23 def inflect(singular, plural) add_singular_rule(singular, plural) add_plural_rule(singular, plural) end |
#write_plural_rules ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/inflector.rb', line 45 def write_plural_rules return @plural_inflections if @plural_inflections rex = /(#{@plural_rules.map { |si, pl| si }.join('|')})$/i hash = Hash[*@plural_rules.flatten] @plural_inflections = [rex, hash] end |
#write_singular_rules ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/inflector.rb', line 36 def write_singular_rules return @singular_inflections if @singular_inflections rex = /(#{@singular_rules.map { |pl, si| pl }.join('|')})$/i hash = Hash[*@singular_rules.flatten] @singular_inflections = [rex, hash] end |