Class: Inflector
- Inherits:
-
Object
- Object
- Inflector
- Defined in:
- lib/motion_model/ext.rb
Overview
Inflector is a singleton class that helps singularize, pluralize and other-thing-ize words. It is very much based on the Rails ActiveSupport implementation or Inflector
Instance Attribute Summary collapse
-
#irregulars ⇒ Object
readonly
Returns the value of attribute irregulars.
-
#plurals ⇒ Object
readonly
Returns the value of attribute plurals.
-
#singulars ⇒ Object
readonly
Returns the value of attribute singulars.
-
#uncountables ⇒ Object
readonly
Returns the value of attribute uncountables.
Class Method Summary collapse
Instance Method Summary collapse
-
#inflect(word, direction) ⇒ Object
nodoc.
-
#initialize ⇒ Inflector
constructor
nodoc.
- #irregular(rule, replacement) ⇒ Object
- #plural(rule, replacement) ⇒ Object
- #pluralize(word) ⇒ Object
- #reset ⇒ Object
- #singular(rule, replacement) ⇒ Object
- #singularize(word) ⇒ Object
- #uncountable(word) ⇒ Object
- #uncountable?(word) ⇒ Boolean
Constructor Details
#initialize ⇒ Inflector
nodoc
59 60 61 |
# File 'lib/motion_model/ext.rb', line 59 def initialize #nodoc reset end |
Instance Attribute Details
#irregulars ⇒ Object (readonly)
Returns the value of attribute irregulars.
151 152 153 |
# File 'lib/motion_model/ext.rb', line 151 def irregulars @irregulars end |
#plurals ⇒ Object (readonly)
Returns the value of attribute plurals.
151 152 153 |
# File 'lib/motion_model/ext.rb', line 151 def plurals @plurals end |
#singulars ⇒ Object (readonly)
Returns the value of attribute singulars.
151 152 153 |
# File 'lib/motion_model/ext.rb', line 151 def singulars @singulars end |
#uncountables ⇒ Object (readonly)
Returns the value of attribute uncountables.
151 152 153 |
# File 'lib/motion_model/ext.rb', line 151 def uncountables @uncountables end |
Class Method Details
.inflections ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/motion_model/ext.rb', line 153 def self.inflections if block_given? yield Inflector.instance else Inflector.instance end end |
.instance ⇒ Object
nodoc
55 56 57 |
# File 'lib/motion_model/ext.rb', line 55 def self.instance #nodoc @__instance__ ||= new end |
Instance Method Details
#inflect(word, direction) ⇒ Object
nodoc
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/motion_model/ext.rb', line 182 def inflect(word, direction) #nodoc return word if uncountable?(word) subject = word.dup @irregulars.each do |rule| return subject if subject.gsub!(rule.first, rule.last) end sense_group = direction == :singularize ? @singulars : @plurals sense_group.each do |rule| return subject if subject.gsub!(rule.first, rule.last) end subject end |
#irregular(rule, replacement) ⇒ Object
173 174 175 |
# File 'lib/motion_model/ext.rb', line 173 def irregular(rule, replacement) @irregulars << [rule, replacement] end |
#plural(rule, replacement) ⇒ Object
169 170 171 |
# File 'lib/motion_model/ext.rb', line 169 def plural(rule, replacement) @plurals << [rule, replacement] end |
#pluralize(word) ⇒ Object
202 203 204 |
# File 'lib/motion_model/ext.rb', line 202 def pluralize(word) inflect word, :pluralize end |
#reset ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/motion_model/ext.rb', line 63 def reset # Put singular-form to plural form transformations here @plurals = [ [/^person$/, 'people'], [/^man$/, 'men'], [/^child$/, 'children'], [/^sex$/, 'sexes'], [/^move$/, 'moves'], [/^cow$/, 'kine'], [/^zombie$/, 'zombies'], [/(quiz)$/i, '\1zes'], [/^(oxen)$/i, '\1'], [/^(ox)$/i, '\1en'], [/^(m|l)ice$/i, '\1ice'], [/^(m|l)ouse$/i, '\1ice'], [/(matr|vert|ind)(?:ix|ex)$/i, '\1ices'], [/(x|ch|ss|sh)$/i, '\1es'], [/([^aeiouy]|qu)y$/i, '\1ies'], [/(hive)$/i, '\1s'], [/(?:([^f])fe|([lr])f)$/i, '\1\2ves'], [/sis$/i, 'ses'], [/([ti])a$/i, '\1a'], [/([ti])um$/i, '\1a'], [/(buffal|tomat)o$/i, '\1oes'], [/(bu)s$/i, '\1ses'], [/(alias|status)$/i, '\1es'], [/(octop|vir)i$/i, '\1i'], [/(octop|vir|alumn)us$/i, '\1i'], [/^(ax|test)is$/i, '\1es'], [/s$/i, 's'], [/$/, 's'] ] # Put plural-form to singular form transformations here @singulars = [ [/^people$/, 'person'], [/^men$/, 'man'], [/^children$/, 'child'], [/^sexes$/, 'sex'], [/^moves$/, 'move'], [/^kine$/, 'cow'], [/^zombies$/, 'zombie'], [/(database)s$/i, '\1'], [/(quiz)zes$/i, '\1'], [/(matr)ices$/i, '\1ix'], [/(vert|ind)ices$/i, '\1ex'], [/^(ox)en/i, '\1'], [/(alias|status)(es)?$/i, '\1'], [/(octop|vir|alumn)(us|i)$/i, '\1us'], [/^(a)x[ie]s$/i, '\1xis'], [/(cris|test)(is|es)$/i, '\1is'], [/(shoe)s$/i, '\1'], [/(o)es$/i, '\1'], [/(bus)(es)?$/i, '\1'], [/^(m|l)ice$/i, '\1ouse'], [/(x|ch|ss|sh)es$/i, '\1'], [/(m)ovies$/i, '\1ovie'], [/(s)eries$/i, '\1eries'], [/([^aeiouy]|qu)ies$/i, '\1y'], [/([lr])ves$/i, '\1f'], [/(tive)s$/i, '\1'], [/(hive)s$/i, '\1'], [/([^f])ves$/i, '\1fe'], [/(^analy)(sis|ses)$/i, '\1sis'], [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '\1sis'], [/([ti])a$/i, '\1um'], [/(n)ews$/i, '\1ews'], [/(ss)$/i, '\1'], [/s$/i, ''] ] @irregulars = [ ] @uncountables = [ 'equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'jeans', 'police' ] end |
#singular(rule, replacement) ⇒ Object
165 166 167 |
# File 'lib/motion_model/ext.rb', line 165 def singular(rule, replacement) @singulars << [rule, replacement] end |
#singularize(word) ⇒ Object
198 199 200 |
# File 'lib/motion_model/ext.rb', line 198 def singularize(word) inflect word, :singularize end |
#uncountable(word) ⇒ Object
161 162 163 |
# File 'lib/motion_model/ext.rb', line 161 def uncountable(word) @uncountables << word end |
#uncountable?(word) ⇒ Boolean
177 178 179 180 |
# File 'lib/motion_model/ext.rb', line 177 def uncountable?(word) return word if @uncountables.include?(word.downcase) false end |