Class: Inflector::Words

Inherits:
Object
  • Object
show all
Defined in:
lib/inflector.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWords

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

#pluralsObject (readonly)

Returns the value of attribute plurals.



9
10
11
# File 'lib/inflector.rb', line 9

def plurals
  @plurals
end

#singularsObject (readonly)

Returns the value of attribute singulars.



9
10
11
# File 'lib/inflector.rb', line 9

def singulars
  @singulars
end

Class Method Details

.instanceObject



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_rulesObject



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_rulesObject



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