Ticket #11511: inflector_acronym_config_patch.diff
| File inflector_acronym_config_patch.diff, 7.7 kB (added by garru, 4 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 … … 59 67 CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore| 60 68 assert_equal(camel, Inflector.camelize(underscore)) 61 69 end 70 71 # Adding acronyms to make underscore to camelize reversible 72 Inflector.inflections.acronym('HTML') 73 Inflector.inflections.acronym('BSD') 74 CamelWithModuleToUnderscoreWithSlashWithoutReverse.each do |camel, underscore| 75 assert_equal(camel, Inflector.camelize(underscore)) 76 end 77 Inflector.inflections.clear('camelizes') 62 78 end 63 79 64 80 def test_underscore_with_slashes 65 81 CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore| 66 82 assert_equal(underscore, Inflector.underscore(camel)) 67 83 end 84 85 CamelWithModuleToUnderscoreWithSlashWithoutReverse.each do |camel, underscore| 86 assert_equal(underscore, Inflector.underscore(camel)) 87 end 68 88 end 69 89 70 90 def test_demodulize … … 154 174 end 155 175 end 156 176 157 %w{plurals singulars uncountables }.each do |inflection_type|177 %w{plurals singulars uncountables camelizes}.each do |inflection_type| 158 178 class_eval " 159 179 def test_clear_#{inflection_type} 160 180 cached_values = Inflector.inflections.#{inflection_type} … … 166 186 end 167 187 168 188 def test_clear_all 169 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables 189 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables, Inflector.inflections.camelizes 170 190 Inflector.inflections.clear :all 171 191 assert Inflector.inflections.plurals.empty? 172 192 assert Inflector.inflections.singulars.empty? 173 193 assert Inflector.inflections.uncountables.empty? 194 assert Inflector.inflections.camelizes.empty? 174 195 Inflector.inflections.instance_variable_set :@plurals, cached_values[0] 175 196 Inflector.inflections.instance_variable_set :@singulars, cached_values[1] 176 197 Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 198 Inflector.inflections.instance_variable_set :@camelizes, cached_values[3] 177 199 end 178 200 179 201 def test_clear_with_default 180 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables 202 cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables, Inflector.inflections.camelizes 181 203 Inflector.inflections.clear 182 204 assert Inflector.inflections.plurals.empty? 183 205 assert Inflector.inflections.singulars.empty? 184 206 assert Inflector.inflections.uncountables.empty? 207 assert Inflector.inflections.uncountables.empty? 185 208 Inflector.inflections.instance_variable_set :@plurals, cached_values[0] 186 209 Inflector.inflections.instance_variable_set :@singulars, cached_values[1] 187 210 Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 211 Inflector.inflections.instance_variable_set :@camelizes, cached_values[3] 188 212 end 189 213 190 214 Irregularities.each do |irregularity| … … 202 226 Inflector.inflections do |inflect| 203 227 define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do 204 228 # save all the inflections 205 singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables229 singulars, plurals, uncountables, camelizes = inflect.singulars, inflect.plurals, inflect.uncountables, inflect.camelizes 206 230 207 231 # clear all the inflections 208 232 inflect.clear(*scope) … … 210 234 assert_equal [], inflect.singulars 211 235 assert_equal [], inflect.plurals 212 236 assert_equal [], inflect.uncountables 237 assert_equal [], inflect.camelizes 213 238 214 239 # restore all the inflections 215 240 singulars.reverse.each { |singular| inflect.singular(*singular) } -
activesupport/test/inflector_test_cases.rb
old new 120 120 "FreeBSD" => "free_bsd", 121 121 "HTML" => "html", 122 122 } 123 124 CamelWithModuleToUnderscoreWithSlashWithoutReverse = { 125 "HTML::Tidy" => "html/tidy", 126 "Tidy::HTML" => "tidy/html" 127 } 123 128 124 129 CamelWithModuleToUnderscoreWithSlash = { 125 130 "Admin::Product" => "admin/product", -
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 camelize(Regexp.new("(^|_)#{acronym}"), acronym.upcase) 55 camelize(Regexp.new("\/#{acronym}"), "/#{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