lib/active_support/inflector/inflections.rb in activesupport-5.1.7 vs lib/active_support/inflector/inflections.rb in activesupport-5.2.0.beta1

- old
+ new

@@ -1,9 +1,12 @@ +# frozen_string_literal: true + require "concurrent/map" require "active_support/core_ext/array/prepend_and_append" require "active_support/core_ext/regexp" require "active_support/i18n" +require "active_support/deprecation" module ActiveSupport module Inflector extend self @@ -63,20 +66,25 @@ def self.instance(locale = :en) @__instance__[locale] ||= new end attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex + deprecate :acronym_regex + attr_reader :acronyms_camelize_regex, :acronyms_underscore_regex # :nodoc: + def initialize - @plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], Uncountables.new, [], {}, /(?=a)b/ + @plurals, @singulars, @uncountables, @humans, @acronyms = [], [], Uncountables.new, [], {} + define_acronym_regex_patterns end # Private, for the test suite. def initialize_dup(orig) # :nodoc: - %w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope| + %w(plurals singulars uncountables humans acronyms).each do |scope| instance_variable_set("@#{scope}", orig.send(scope).dup) end + define_acronym_regex_patterns end # Specifies a new acronym. An acronym must be specified as it will appear # in a camelized string. An underscore string that contains the acronym # will retain the acronym when passed to +camelize+, +humanize+, or @@ -126,11 +134,11 @@ # acronym 'McDonald' # underscore 'McDonald' # => 'mcdonald' # camelize 'mcdonald' # => 'McDonald' def acronym(word) @acronyms[word.downcase] = word - @acronym_regex = /#{@acronyms.values.join("|")}/ + define_acronym_regex_patterns end # Specifies a new pluralization rule and its replacement. The rule can # either be a string or a regular expression. The replacement should # always be a string that may include references to the matched data from @@ -221,9 +229,17 @@ @plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, [] else instance_variable_set "@#{scope}", [] end end + + private + + 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 end # Yields a singleton instance of Inflector::Inflections so you can specify # additional inflector rules. If passed an optional locale, rules for other # languages can be specified. If not specified, defaults to <tt>:en</tt>.