Module: Extlib::Inflection

Defined in:
lib/extlib_lite/inflections.rb

Overview

English Nouns Number Inflection.

This module provides english singular <-> plural noun inflections.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.plural_ofObject (readonly)

Returns the value of attribute plural_of.



205
206
207
# File 'lib/extlib_lite/inflections.rb', line 205

def plural_of
  @plural_of
end

.singular_ofObject (readonly)

Returns the value of attribute singular_of.



205
206
207
# File 'lib/extlib_lite/inflections.rb', line 205

def singular_of
  @singular_of
end

Class Method Details

.classify(name) ⇒ Object

Take an underscored name and make it into a camelized name

Examples:

"egg_and_hams".classify #=> "EggAndHam"
"enlarged_testes".classify #=> "EnlargedTestis"
"post".classify #=> "Post"


16
17
18
19
20
# File 'lib/extlib_lite/inflections.rb', line 16

def classify(name)
  words = name.to_s.sub(/.*\./, '').split('_')
  words[-1] = singularize(words[-1])
  words.collect { |word| word.capitalize }.join
end

.clear(type = :all) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/extlib_lite/inflections.rb', line 81

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.

Examples:

"Module".constantize #=> Module
"Class".constantize #=> Class


41
42
43
44
45
46
47
# File 'lib/extlib_lite/inflections.rb', line 41

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

.plural(word) ⇒ Object Also known as: pluralize

Convert an English word from singular to plural.

"boy".plural     #=> boys
"tomato".plural  #=> tomatoes

Parameters

word<String>

word to pluralize

Returns

<String>

pluralized form of word

Notes

Aliased as pluralize (a Railism)



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/extlib_lite/inflections.rb', line 248

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



180
181
182
# File 'lib/extlib_lite/inflections.rb', line 180

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



114
115
116
117
# File 'lib/extlib_lite/inflections.rb', line 114

def plural_word(singular, plural)
  @plural_of[singular] = plural
  @plural_of[singular.capitalize] = plural.capitalize
end

.pluralization_rulesObject

Read prepared pluralization rules.



196
197
198
199
200
201
202
203
# File 'lib/extlib_lite/inflections.rb', line 196

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.



140
141
142
143
144
# File 'lib/extlib_lite/inflections.rb', line 140

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 plural 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)



220
221
222
223
224
225
226
227
228
229
# File 'lib/extlib_lite/inflections.rb', line 220

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



161
162
163
# File 'lib/extlib_lite/inflections.rb', line 161

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



102
103
104
105
# File 'lib/extlib_lite/inflections.rb', line 102

def singular_word(singular, plural)
  @singular_of[plural] = singular
  @singular_of[plural.capitalize] = singular.capitalize
end

.singularization_rulesObject

Read prepared singularization rules.



185
186
187
188
189
190
191
192
193
# File 'lib/extlib_lite/inflections.rb', line 185

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

.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.

Examples:

"ActiveRecord".underscore #=> "active_record"
"ActiveRecord::Errors".underscore #=> active_record/errors


30
31
32
# File 'lib/extlib_lite/inflections.rb', line 30

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’



75
76
77
78
79
# File 'lib/extlib_lite/inflections.rb', line 75

def word(singular, plural=nil)
  plural = singular unless plural
  singular_word(singular, plural)
  plural_word(singular, plural)
end