Ticket #6989: humanize_inflections.2.diff
| File humanize_inflections.2.diff, 4.2 kB (added by dcmanges, 2 years ago) |
|---|
-
test/inflector_test.rb
old new 289 289 assert_equal(human, Inflector.humanize(underscore)) 290 290 end 291 291 end 292 293 def test_humanize_by_rule 294 Inflector.inflections do |inflect| 295 inflect.human(/_cnt$/i, '\1_count') 296 inflect.human(/^prefx_/i, '\1') 297 end 298 assert_equal("Jargon count", Inflector.humanize("jargon_cnt")) 299 assert_equal("Request", Inflector.humanize("prefx_request")) 300 end 301 302 def test_humanize_by_string 303 Inflector.inflections do |inflect| 304 inflect.human("col_rpted_bugs" => "Reported bugs") 305 end 306 assert_equal("Reported bugs", Inflector.humanize("col_rpted_bugs")) 307 assert_equal("Col rpted bugs", Inflector.humanize("COL_rpted_bugs")) 308 end 309 310 def test_clear_humans 311 Inflector.inflections do |inflect| 312 inflect.human("test","clear") 313 end 314 assert_equal("clear", "test".humanize) 315 Inflector.inflections.clear(:humans) 316 assert_equal("Test","test".humanize) 317 end 292 318 293 319 def test_constantize 294 320 assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") -
lib/active_support/inflector.rb
old new 14 14 # inflect.irregular 'octopus', 'octopi' 15 15 # 16 16 # inflect.uncountable "equipment" 17 # 18 # inflect.human /^prefx_/i, '\1' 17 19 # end 18 20 # 19 21 # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the … … 22 24 class Inflections 23 25 include Singleton 24 26 25 attr_reader :plurals, :singulars, :uncountables 27 attr_reader :plurals, :singulars, :uncountables, :humans, :human_strings 26 28 27 29 def initialize 28 @plurals, @singulars, @uncountables = [], [], []30 @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 29 31 end 30 32 31 33 # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. … … 61 63 (@uncountables << words).flatten! 62 64 end 63 65 66 # Specifies a humanized form of a string by a regular expression rule or by a string mapping. 67 # When using a regular expression based replacement, the normal humanize formatting is called after the replacement. 68 # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name') 69 # 70 # Examples: 71 # human /_cnt$/i, '\1_count' 72 # human "legacy_col_person_name", "Name" 73 # human "strt_dt" => "Start date", 74 # "upd_dt" => "Updated date" 75 def human(first, last = nil) 76 case first 77 when Hash 78 @human_strings.merge!(first) 79 when String 80 @human_strings[first] = last 81 else 82 @humans.insert(0, [first, last]) 83 end 84 end 85 64 86 # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type, 65 # the options are: :plurals, :singulars, :uncountables 87 # the options are: :plurals, :singulars, :uncountables, :humans 66 88 # 67 89 # Examples: 68 90 # clear :all … … 70 92 def clear(scope = :all) 71 93 case scope 72 94 when :all 73 @plurals, @singulars, @uncountables = [], [], []95 @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 74 96 else 75 97 instance_variable_set "@#{scope}", [] 98 @human_strings = {} if scope == :humans 76 99 end 77 100 end 78 101 end … … 134 157 end 135 158 136 159 def humanize(lower_case_and_underscored_word) 137 lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 160 if result = inflections.human_strings[lower_case_and_underscored_word] 161 result 162 else 163 result = lower_case_and_underscored_word.to_s.dup 164 inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } 165 result.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 166 end 138 167 end 139 168 140 169 def demodulize(class_name_in_module)