Class: ApipieBindings::Inflections

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInflections

Returns a new instance of Inflections.


14
15
16
# File 'lib/apipie_bindings/inflector.rb', line 14

def initialize
  @plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], [], [], {}, /(?=a)b/
end

Instance Attribute Details

#acronym_regexObject (readonly)

Returns the value of attribute acronym_regex


12
13
14
# File 'lib/apipie_bindings/inflector.rb', line 12

def acronym_regex
  @acronym_regex
end

#acronymsObject (readonly)

Returns the value of attribute acronyms


12
13
14
# File 'lib/apipie_bindings/inflector.rb', line 12

def acronyms
  @acronyms
end

#humansObject (readonly)

Returns the value of attribute humans


12
13
14
# File 'lib/apipie_bindings/inflector.rb', line 12

def humans
  @humans
end

#pluralsObject (readonly)

Returns the value of attribute plurals


12
13
14
# File 'lib/apipie_bindings/inflector.rb', line 12

def plurals
  @plurals
end

#singularsObject (readonly)

Returns the value of attribute singulars


12
13
14
# File 'lib/apipie_bindings/inflector.rb', line 12

def singulars
  @singulars
end

#uncountablesObject (readonly)

Returns the value of attribute uncountables


12
13
14
# File 'lib/apipie_bindings/inflector.rb', line 12

def uncountables
  @uncountables
end

Class Method Details

.instance(locale = :en) ⇒ Object


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

def self.instance(locale = :en)
  @__instance__[locale] ||= new
end

Instance Method Details

#acronym(word) ⇒ Object


18
19
20
21
# File 'lib/apipie_bindings/inflector.rb', line 18

def acronym(word)
  @acronyms[word.downcase] = word
  @acronym_regex = /#{@acronyms.values.join("|")}/
end

#clear(scope = :all) ⇒ Object


72
73
74
75
76
77
78
79
# File 'lib/apipie_bindings/inflector.rb', line 72

def clear(scope = :all)
  case scope
    when :all
      @plurals, @singulars, @uncountables, @humans = [], [], [], []
    else
      instance_variable_set "@#{scope}", []
  end
end

#human(rule, replacement) ⇒ Object


68
69
70
# File 'lib/apipie_bindings/inflector.rb', line 68

def human(rule, replacement)
  @humans.unshift([rule, replacement])
end

#irregular(singular, plural) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/apipie_bindings/inflector.rb', line 35

def irregular(singular, plural)
  @uncountables.delete(singular)
  @uncountables.delete(plural)

  s0 = singular[0].chr
  srest = singular[1..-1]

  p0 = plural[0].chr
  prest = plural[1..-1]

  if s0.upcase == p0.upcase
    plural(/(#{s0})#{srest}$/i, '\1' + prest)
    plural(/(#{p0})#{prest}$/i, '\1' + prest)

    singular(/(#{s0})#{srest}$/i, '\1' + srest)
    singular(/(#{p0})#{prest}$/i, '\1' + srest)
  else
    plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest)
    plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest)
    plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest)
    plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest)

    singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest)
    singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest)
    singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest)
    singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest)
  end
end

#plural(rule, replacement) ⇒ Object


23
24
25
26
27
# File 'lib/apipie_bindings/inflector.rb', line 23

def plural(rule, replacement)
  @uncountables.delete(rule) if rule.is_a?(String)
  @uncountables.delete(replacement)
  @plurals.unshift([rule, replacement])
end

#singular(rule, replacement) ⇒ Object


29
30
31
32
33
# File 'lib/apipie_bindings/inflector.rb', line 29

def singular(rule, replacement)
  @uncountables.delete(rule) if rule.is_a?(String)
  @uncountables.delete(replacement)
  @singulars.unshift([rule, replacement])
end

#uncountable(*words) ⇒ Object


64
65
66
# File 'lib/apipie_bindings/inflector.rb', line 64

def uncountable(*words)
  (@uncountables << words).flatten!
end