class ActiveSupport::Inflector::Inflections
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/active_support/inflector/inflections.rbs class ActiveSupport::Inflector::Inflections def self.instance_or_fallback: (Symbol locale) -> ActiveSupport::Inflector::Inflections end
before any of the rules that may already have been loaded.
singularization rules that is runs. This guarantees that your rules run
rule for cactus will now be the first of the pluralization and
New rules are added at the top. So in the example above, the irregular
end
inflect.uncountable ‘equipment’
inflect.irregular ‘cactus’, ‘cacti’
inflect.singular /^(ox)en/i, ‘1’
inflect.plural /^(ox)$/i, ‘12en’
ActiveSupport::Inflector.inflections(:en) do |inflect|
default locale is :en
. Only rules for English are provided.
an optional locale, rules for other languages can be specified. The
which can then be used to specify additional inflection rules. If passed
A singleton instance of this class is yielded by Inflector.inflections,
def self.instance(locale = :en)
def self.instance(locale = :en) @__instance__[locale] ||= new end
def self.instance_or_fallback(locale)
Experimental RBS support (using type sampling data from the type_fusion
project).
def self.instance_or_fallback: (Symbol locale) -> ActiveSupport::Inflector::Inflections
This signature was generated using 12 samples from 1 application.
def self.instance_or_fallback(locale) I18n.fallbacks[locale].each do |k| return @__instance__[k] if @__instance__.key?(k) end instance(locale) end
def acronym(word)
underscore 'McDonald' # => 'mcdonald'
acronym 'McDonald'
camelize 'restful_controller' # => 'RESTfulController'
camelize 'restful' # => 'RESTful'
titleize 'RESTfulController' # => 'RESTful Controller'
underscore 'RESTfulController' # => 'restful_controller'
underscore 'RESTful' # => 'restful'
acronym 'RESTful'
restriction is that the word must begin with a capital letter.
otherwise needs to maintain a non-standard capitalization. The only
+acronym+ may be used to specify any word that contains an acronym or
camelize(pluralize('api')) # => 'APIs'
acronym 'APIs'
camelize(pluralize('api')) # => 'Apis'
acronym 'API'
form as an acronym as well:
pluralized result. To work around this, you must specify the pluralized
recognized, since the acronym will not occur as a delimited unit in the
Note: Acronyms that are passed to +pluralize+ will no longer be
underscore 'HTTPS' # => 'https'
camelize 'https' # => 'HTTPS'
acronym 'HTTPS'
underscore 'HTTPS' # => 'http_s', not 'https'
camelize 'https' # => 'Https', not 'HTTPs'
camelize 'my_http_delimited' # => 'MyHTTPDelimited'
acronym 'HTTP'
another word for conversions to recognize it:
The acronym, however, must occur as a delimited unit and not be part of
underscore 'MyHTML' # => 'my_html'
camelize 'html' # => 'HTML'
titleize 'html' # => 'HTML'
acronym 'HTML'
into a non-delimited single lowercase word when passed to +underscore+.
the acronym when titleized or humanized, and will convert the acronym
+titleize+. A camelized string that contains the acronym will maintain
will retain the acronym when passed to +camelize+, +humanize+, or
in a camelized string. An underscore string that contains the acronym
Specifies a new acronym. An acronym must be specified as it will appear
def acronym(word) @acronyms[word.downcase] = word define_acronym_regex_patterns end
def clear(scope = :all)
clear :all
:humans, :acronyms.
options are: :plurals, :singulars, :uncountables,
:all). Give the scope as a symbol of the inflection type, the
Clears the loaded inflections within a given scope (default is
def clear(scope = :all) case scope when :all clear(:acronyms) clear(:plurals) clear(:singulars) clear(:uncountables) clear(:humans) when :acronyms @acronyms = {} define_acronym_regex_patterns when :uncountables @uncountables = Uncountables.new when :plurals, :singulars, :humans instance_variable_set "@#{scope}", [] end end
def define_acronym_regex_patterns
def define_acronym_regex_patterns @acronym_regex = @acronyms.empty? ? /(?=a)b/ : /#{@acronyms.values.join("|")}/ @acronyms_camelize_regex = /^(?:#{@acronym_regex}(?=\b|[A-Z_])|\w)/ @acronyms_underscore_regex = /(?:(?<=([A-Za-z\d]))|\b)(#{@acronym_regex})(?=\b|[^a-z])/ end
def human(rule, replacement)
human /_cnt$/i, '\1_count'
'The name', not 'the_name').
string is used, the human form should be specified as desired (example:
the normal humanize formatting is called after the replacement. When a
by a string mapping. When using a regular expression based replacement,
Specifies a humanized form of a string by a regular expression rule or
def human(rule, replacement) @humans.prepend([rule, replacement]) end
def initialize
def initialize @plurals, @singulars, @uncountables, @humans, @acronyms = [], [], Uncountables.new, [], {} define_acronym_regex_patterns end
def initialize_dup(orig) # :nodoc:
Private, for the test suite.
def initialize_dup(orig) # :nodoc: %w(plurals singulars uncountables humans acronyms).each do |scope| instance_variable_set("@#{scope}", orig.public_send(scope).dup) end define_acronym_regex_patterns end
def irregular(singular, plural)
irregular 'cactus', 'cacti'
plural form.
regular expressions. You simply pass the irregular in singular and
singularization at the same time. This can only be used for strings, not
Specifies a new irregular that applies to both pluralization and
def irregular(singular, plural) @uncountables.delete(singular) @uncountables.delete(plural) s0 = singular[0] srest = singular[1..-1] p0 = plural[0] prest = plural[1..-1] if s0.upcase == p0.upcase plural(/(#{s0})#{srest}$/i, '\1' + prest) plural(/(#{p0})#{prest}$/i, '\1' + prest) singular(/(#{s0})#{srest}$/i, '\1' + srest) singular(/(#{p0})#{prest}$/i, '\1' + srest) else plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest) plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest) plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest) plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest) singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest) singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest) singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest) singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest) end end
def plural(rule, replacement)
always be a string that may include references to the matched data from
either be a string or a regular expression. The replacement should
Specifies a new pluralization rule and its replacement. The rule can
def plural(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @plurals.prepend([rule, replacement]) end
def singular(rule, replacement)
always be a string that may include references to the matched data from
either be a string or a regular expression. The replacement should
Specifies a new singularization rule and its replacement. The rule can
def singular(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @singulars.prepend([rule, replacement]) end
def uncountable(*words)
uncountable 'money', 'information'
uncountable 'money'
Specifies words that are uncountable and should not be inflected.
def uncountable(*words) @uncountables.add(words) end