Class: Rich::Pluralization::Inflector::Inflections::Inflection

Inherits:
Struct
  • Object
show all
Defined in:
lib/rich/pluralization/inflector/inflections.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exceptionsObject

Returns the value of attribute exceptions

Returns:

  • (Object)

    the current value of exceptions



72
73
74
# File 'lib/rich/pluralization/inflector/inflections.rb', line 72

def exceptions
  @exceptions
end

#ruleObject

Returns the value of attribute rule

Returns:

  • (Object)

    the current value of rule



72
73
74
# File 'lib/rich/pluralization/inflector/inflections.rb', line 72

def rule
  @rule
end

Class Method Details

.build(*args) ⇒ Object



74
75
76
# File 'lib/rich/pluralization/inflector/inflections.rb', line 74

def self.build(*args)
  self.new *(args.first.is_a?(Hash) ? args.first.values_at(*%w(rule replacement exceptions)) : args)
end

Instance Method Details

#exception?(word) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rich/pluralization/inflector/inflections.rb', line 86

def exception?(word)
  exceptions.any?{|e| match?(word, e)}
end

#inflect(word) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/rich/pluralization/inflector/inflections.rb', line 95

def inflect(word)
  if exception?(word)
    word
  else
    rule.split(",").collect do |x|
      word.dup.gsub! parse_pattern(x, true), self[:replacement]
    end.detect{|x| !x.nil?} || word
  end
end

#inflect!(word) ⇒ Object



105
106
107
# File 'lib/rich/pluralization/inflector/inflections.rb', line 105

def inflect!(word)
  (result = inflect(word)) == word ? nil : result
end

#inspectObject



109
110
111
# File 'lib/rich/pluralization/inflector/inflections.rb', line 109

def inspect
  to_hash.inspect
end

#match?(word, pattern) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/rich/pluralization/inflector/inflections.rb', line 90

def match?(word, pattern)
  w, p = word.downcase, parse_pattern(pattern)
  p.is_a?(Regexp) ? !!w.match(p) : (w == p)
end

#to_hashObject



113
114
115
# File 'lib/rich/pluralization/inflector/inflections.rb', line 113

def to_hash
  Hash[*members.zip(values).flatten]
end