Ticket #11511: inflector_acronym_config_patch.2.diff
| File inflector_acronym_config_patch.2.diff, 6.2 kB (added by garru, 3 months ago) |
|---|
-
activesupport/test/inflector_test.rb
old new 44 44 CamelToUnderscore.each do |camel, underscore| 45 45 assert_equal(camel, Inflector.camelize(underscore)) 46 46 end 47 48 # Adding acronyms to make underscore to camelize reversible 49 Inflector.inflections.acronym('HTML') 50 Inflector.inflections.acronym('BSD') 51 CamelToUnderscoreWithoutReverse.each do |camel, underscore| 52 assert_equal(camel, Inflector.camelize(underscore)) 53 end 54 Inflector.inflections.clear('camelizes') 47 55 end 48 56 49 57 def test_underscore … … 154 162 end 155 163 end 156 164 157 %w{plurals singulars uncountables }.each do |inflection_type|165 %w{plurals singulars uncountables camelizes}.each do |inflection_type| 158 166 class_eval " 159 167 def test_clear_#{inflection_type} 160 168 cached_values = Inflector.inflections.#{inflection_type} … … 166 174 end 167 175 168 176 def test_clear_all 169 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables 177 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables, Inflector.inflections.camelizes 170 178 Inflector.inflections.clear :all 171 179 assert Inflector.inflections.plurals.empty? 172 180 assert Inflector.inflections.singulars.empty? 173 181 assert Inflector.inflections.uncountables.empty? 182 assert Inflector.inflections.camelizes.empty? 174 183 Inflector.inflections.instance_variable_set :@plurals, cached_values[0] 175 184 Inflector.inflections.instance_variable_set :@singulars, cached_values[1] 176 185 Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 186 Inflector.inflections.instance_variable_set :@camelizes, cached_values[3] 177 187 end 178 188 179 189 def test_clear_with_default 180 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables 190 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables, Inflector.inflections.camelizes 181 191 Inflector.inflections.clear 182 192 assert Inflector.inflections.plurals.empty? 183 193 assert Inflector.inflections.singulars.empty? 184 194 assert Inflector.inflections.uncountables.empty? 195 assert Inflector.inflections.uncountables.empty? 185 196 Inflector.inflections.instance_variable_set :@plurals, cached_values[0] 186 197 Inflector.inflections.instance_variable_set :@singulars, cached_values[1] 187 198 Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 199 Inflector.inflections.instance_variable_set :@camelizes, cached_values[3] 188 200 end 189 201 190 202 Irregularities.each do |irregularity| … … 202 214 Inflector.inflections do |inflect| 203 215 define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do 204 216 # save all the inflections 205 singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables217 singulars, plurals, uncountables, camelizes = inflect.singulars, inflect.plurals, inflect.uncountables, inflect.camelizes 206 218 207 219 # clear all the inflections 208 220 inflect.clear(*scope) … … 210 222 assert_equal [], inflect.singulars 211 223 assert_equal [], inflect.plurals 212 224 assert_equal [], inflect.uncountables 225 assert_equal [], inflect.camelizes 213 226 214 227 # restore all the inflections 215 228 singulars.reverse.each { |singular| inflect.singular(*singular) } -
activesupport/lib/active_support/inflector.rb
old new 22 22 class Inflections 23 23 include Singleton 24 24 25 attr_reader :plurals, :singulars, :uncountables 25 attr_reader :plurals, :singulars, :uncountables, :camelizes 26 26 27 27 def initialize 28 @plurals, @singulars, @uncountables =[], [], []28 @plurals, @singulars, @uncountables , @camelizes = [], [], [], [] 29 29 end 30 30 31 31 # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. … … 39 39 def singular(rule, replacement) 40 40 @singulars.insert(0, [rule, replacement]) 41 41 end 42 42 43 # Specifies a new camelization rule and its replacement. The rule can either be a string or a regular expression. 44 def camelize(rule, replacement) 45 @camelizes.insert(0, [rule, replacement]) 46 end 47 48 # Specifies a new camelization rule and its replacement for an acronym. The acronym must be a string 49 # Examples: 50 # acronym 'pdf' 51 # acronym 'HTML' 52 def acronym(acronym) 53 acronym.downcase! 54 rule = Regexp.new("(^|_)#{acronym}") 55 camelize(rule, acronym.upcase!) 56 end 57 43 58 # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used 44 59 # for strings, not regular expressions. You simply pass the irregular in singular and plural form. 45 60 # … … 77 92 def clear(scope = :all) 78 93 case scope 79 94 when :all 80 @plurals, @singulars, @uncountables =[], [], []95 @plurals, @singulars, @uncountables , @camelizes = [], [], [], [] 81 96 else 82 97 instance_variable_set "@#{scope}", [] 83 98 end … … 145 160 # "active_record/errors".camelize #=> "ActiveRecord::Errors" 146 161 # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors" 147 162 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) 163 result = lower_case_and_underscored_word.to_s.dup 164 inflections.camelizes.each { |(rule, replacement)| result.gsub!(rule, replacement) } 148 165 if first_letter_in_uppercase 149 lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }166 result.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } 150 167 else 151 lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]168 result.first + camelize(result)[1..-1] 152 169 end 153 170 end 154 171