Module: Extlib::Inflection
- Defined in:
- lib/quickbooks/extlib/inflection.rb
Overview
English Nouns Number Inflection.
This module provides english singular <-> plural noun inflections.
Class Attribute Summary collapse
-
.plural_of ⇒ Object
readonly
Returns the value of attribute plural_of.
-
.singular_of ⇒ Object
readonly
Returns the value of attribute singular_of.
Class Method Summary collapse
-
.camelize(lower_case_and_underscored_word, *args) ⇒ Object
By default, camelize converts strings to UpperCamelCase.
-
.classify(name) ⇒ Object
Take an underscored name and make it into a camelized name.
- .clear(type = :all) ⇒ Object
-
.constantize(camel_cased_word) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string.
-
.demodulize(class_name_in_module) ⇒ Object
Removes the module part from the expression in the string.
-
.foreign_key(class_name, key = "id") ⇒ Object
Creates a foreign key name from a class name.
-
.humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips _id.
-
.plural(word) ⇒ Object
(also: pluralize)
Convert an English word from singular to plurel.
-
.plural_rule(singular, plural) ⇒ Object
Define a plurualization rule.
-
.plural_word(singular, plural) ⇒ Object
Define a pluralization exception.
-
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
-
.rule(singular, plural, whole_word = false) ⇒ Object
Define a general rule.
-
.singular(word) ⇒ Object
(also: singularize)
Convert an English word from plurel to singular.
-
.singular_rule(singular, plural) ⇒ Object
Define a singularization rule.
-
.singular_word(singular, plural) ⇒ Object
Define a singularization exception.
-
.singularization_rules ⇒ Object
Read prepared singularization rules.
-
.tableize(class_name) ⇒ Object
Create the name of a table like Rails does for models to table names.
-
.underscore(camel_cased_word) ⇒ Object
The reverse of
camelize
. -
.word(singular, plural = nil) ⇒ Object
Defines a general inflection exception case.
Class Attribute Details
.plural_of ⇒ Object (readonly)
Returns the value of attribute plural_of.
254 255 256 |
# File 'lib/quickbooks/extlib/inflection.rb', line 254 def plural_of @plural_of end |
.singular_of ⇒ Object (readonly)
Returns the value of attribute singular_of.
254 255 256 |
# File 'lib/quickbooks/extlib/inflection.rb', line 254 def singular_of @singular_of end |
Class Method Details
.camelize(lower_case_and_underscored_word, *args) ⇒ Object
By default, camelize converts strings to UpperCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces
27 28 29 |
# File 'lib/quickbooks/extlib/inflection.rb', line 27 def camelize(lower_case_and_underscored_word, *args) lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end |
.classify(name) ⇒ Object
Take an underscored name and make it into a camelized name
15 16 17 |
# File 'lib/quickbooks/extlib/inflection.rb', line 15 def classify(name) camelize(singularize(name.to_s.sub(/.*\./, ''))) end |
.clear(type = :all) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/quickbooks/extlib/inflection.rb', line 130 def clear(type = :all) if type == :singular || type == :all @singular_of = {} @singular_rules = [] @singularization_rules, @singularization_regex = nil, nil end if type == :plural || type == :all @singular_of = {} @singular_rules = [] @singularization_rules, @singularization_regex = nil, nil end end |
.constantize(camel_cased_word) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
90 91 92 93 94 95 96 |
# File 'lib/quickbooks/extlib/inflection.rb', line 90 def constantize(camel_cased_word) unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end Object.module_eval("::#{$1}", __FILE__, __LINE__) end |
.demodulize(class_name_in_module) ⇒ Object
Removes the module part from the expression in the string
59 60 61 |
# File 'lib/quickbooks/extlib/inflection.rb', line 59 def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end |
.foreign_key(class_name, key = "id") ⇒ Object
Creates a foreign key name from a class name.
79 80 81 |
# File 'lib/quickbooks/extlib/inflection.rb', line 79 def foreign_key(class_name, key = "id") underscore(demodulize(class_name.to_s)) << "_" << key.to_s end |
.humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.
50 51 52 |
# File 'lib/quickbooks/extlib/inflection.rb', line 50 def humanize(lower_case_and_underscored_word) lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
.plural(word) ⇒ Object Also known as: pluralize
Convert an English word from singular to plurel.
"boy".plural #=> boys
"tomato".plural #=> tomatoes
Parameters
- word<String>
-
word to pluralize
Returns
- <String>
-
pluralized form of word
Notes
Aliased as pluralize (a Railism)
297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/quickbooks/extlib/inflection.rb', line 297 def plural(word) # special exceptions return "" if word == "" if result = plural_of[word] return result.dup end result = word.dup regex, hash = pluralization_rules result.sub!(regex) {|m| hash[m]} plural_of[word] = result return result end |
.plural_rule(singular, plural) ⇒ Object
Define a plurualization rule.
Parameters
- singular<String>
-
ending of the word in singular form
- plural<String>
-
ending of the word in plural form
Examples
Once the following rule is defined: English::Inflect.singular_rule ‘fe’, ‘ves’
You can see the following results: irb> “wife”.plural
> wives
229 230 231 |
# File 'lib/quickbooks/extlib/inflection.rb', line 229 def plural_rule(singular, plural) @plural_rules << [singular, plural] end |
.plural_word(singular, plural) ⇒ Object
Define a pluralization exception.
Parameters
- singular<String>
-
singular form of the word
- plural<String>
-
plural form of the word
163 164 165 166 |
# File 'lib/quickbooks/extlib/inflection.rb', line 163 def plural_word(singular, plural) @plural_of[singular] = plural @plural_of[singular.capitalize] = plural.capitalize end |
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
245 246 247 248 249 250 251 252 |
# File 'lib/quickbooks/extlib/inflection.rb', line 245 def pluralization_rules if defined?(@pluralization_regex) && @pluralization_regex return [@pluralization_regex, @pluralization_hash] end @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i") @pluralization_hash = Hash[*@plural_rules.flatten] [@pluralization_regex, @pluralization_hash] end |
.rule(singular, plural, whole_word = false) ⇒ Object
Define a general rule.
Parameters
- singular<String>
-
ending of the word in singular form
- plural<String>
-
ending of the word in plural form
- whole_word<Boolean>
-
for capitalization, since words can be capitalized (Man => Men) #
Examples
Once the following rule is defined: English::Inflect.rule ‘y’, ‘ies’
You can see the following results: irb> “fly”.plural
> flies
irb> “cry”.plural
> cries
Define a general rule.
189 190 191 192 193 |
# File 'lib/quickbooks/extlib/inflection.rb', line 189 def rule(singular, plural, whole_word = false) singular_rule(singular, plural) plural_rule(singular, plural) word(singular, plural) if whole_word end |
.singular(word) ⇒ Object Also known as: singularize
Convert an English word from plurel to singular.
"boys".singular #=> boy
"tomatoes".singular #=> tomato
Parameters
- word<String>
-
word to singularize
Returns
- <String>
-
singularized form of word
Notes
Aliased as singularize (a Railism)
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/quickbooks/extlib/inflection.rb', line 269 def singular(word) if result = singular_of[word] return result.dup end result = word.dup regex, hash = singularization_rules result.sub!(regex) {|m| hash[m]} singular_of[word] = result return result end |
.singular_rule(singular, plural) ⇒ Object
Define a singularization rule.
Parameters
- singular<String>
-
ending of the word in singular form
- plural<String>
-
ending of the word in plural form
Examples
Once the following rule is defined: English::Inflect.singular_rule ‘o’, ‘oes’
You can see the following results: irb> “heroes”.singular
> hero
210 211 212 |
# File 'lib/quickbooks/extlib/inflection.rb', line 210 def singular_rule(singular, plural) @singular_rules << [singular, plural] end |
.singular_word(singular, plural) ⇒ Object
Define a singularization exception.
Parameters
- singular<String>
-
singular form of the word
- plural<String>
-
plural form of the word
151 152 153 154 |
# File 'lib/quickbooks/extlib/inflection.rb', line 151 def singular_word(singular, plural) @singular_of[plural] = singular @singular_of[plural.capitalize] = singular.capitalize end |
.singularization_rules ⇒ Object
Read prepared singularization rules.
234 235 236 237 238 239 240 241 242 |
# File 'lib/quickbooks/extlib/inflection.rb', line 234 def singularization_rules if defined?(@singularization_regex) && @singularization_regex return [@singularization_regex, @singularization_hash] end # No sorting needed: Regexen match on longest string @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i") @singularization_hash = Hash[*@singular_rules.flatten].invert [@singularization_regex, @singularization_hash] end |
.tableize(class_name) ⇒ Object
Create the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.
70 71 72 |
# File 'lib/quickbooks/extlib/inflection.rb', line 70 def tableize(class_name) pluralize(class_name.to_const_path.gsub(/\//, '_')) end |
.underscore(camel_cased_word) ⇒ Object
The reverse of camelize
. Makes an underscored form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
40 41 42 |
# File 'lib/quickbooks/extlib/inflection.rb', line 40 def underscore(camel_cased_word) camel_cased_word.to_const_path end |
.word(singular, plural = nil) ⇒ Object
Defines a general inflection exception case.
Parameters
- singular<String>
-
singular form of the word
- plural<String>
-
plural form of the word
Examples
Here we define erratum/errata exception case:
English::Inflect.word “erratum”, “errata”
In case singular and plural forms are the same omit second argument on call:
English::Inflect.word ‘information’
124 125 126 127 128 |
# File 'lib/quickbooks/extlib/inflection.rb', line 124 def word(singular, plural=nil) plural = singular unless plural singular_word(singular, plural) plural_word(singular, plural) end |