JSON
Inflector | 59
Inflector inflections.rb, inflector.rb
The Inflector module provides a set of simple transformations on English words to facilitate
ActiveRecord??™s manipulations of class and table names. Following the policy of
???convention over configuration,??? for example, a model class named Message would correspond
to a table name of messages.
The core of Inflector is the pluralization rules, contained in inflections.rb. The default set
of rules is fairly broad, but additional rules can be added easily in config/initializers/
inflections.rb or a similar configuration file, after the framework loads:
Inflector.inflections do |inflect|
inflect.plural /^(ox)$/i, '\1\2en'
inflect.singular /^(ox)en/i, '\1'
inflect.irregular 'octopus', 'octopi'
inflect.uncountable "equipment"
end
Inflector.inflections yields a singleton instance of the inflections object. Rules are
prepended to the list of inflections, so these rules will override the default as long as they
are loaded after the Rails framework. Another consequence of the load order is that the
rules should be ordered from most general to most specific within a block; the last appropriate
inflection rule seen will be used.
The regular expression captures and backreferences ensure that initial capitals are handled
correctly; the initial letters (upper- or lowercase) are captured with a case-insensitive regex
and substituted into the replacement.
Pages:
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