public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add Inflection rules for String#humanize. [#535 state:resolved] [dcmanges]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
lifo (author)
Wed Jul 02 05:25:17 -0700 2008
commit  4f75840d72b96fff34d65b59480da7d6c7494120
tree    cd13f9cf0359811c262e47737e704b059312ee05
parent  3a95ee73cfbcfeada3b053c5b0fb7b5125ef12c7
...
1
2
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -1,5 +1,13 @@
0
 *Edge*
0
 
0
+* Add Inflection rules for String#humanize. #535 [dcmanges]
0
+
0
+  ActiveSupport::Inflector.inflections do |inflect|
0
+    inflect.human(/_cnt$/i, '\1_count')
0
+  end
0
+
0
+  'jargon_cnt'.humanize # => 'Jargon count'
0
+
0
 * TimeWithZone: when crossing DST boundary, treat Durations of days, months or years as variable-length, and all other values as absolute length. A time + 24.hours will advance exactly 24 hours, but a time + 1.day will advance 23-25 hours, depending on the day. Ensure consistent behavior across all advancing methods [Geoff Buesing]
0
 
0
 * Added TimeZone #=~, to support matching zones by regex in time_zone_select. #195 [Ernie Miller]
...
30
31
32
33
 
34
35
36
 
37
38
39
...
76
77
78
 
 
 
 
 
 
 
 
 
 
 
79
80
81
 
82
83
84
...
209
210
211
212
 
 
 
 
213
214
215
...
30
31
32
 
33
34
35
 
36
37
38
39
...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
92
93
94
95
...
220
221
222
 
223
224
225
226
227
228
229
0
@@ -30,10 +30,10 @@ module ActiveSupport
0
     class Inflections
0
       include Singleton
0
 
0
-      attr_reader :plurals, :singulars, :uncountables
0
+      attr_reader :plurals, :singulars, :uncountables, :humans
0
 
0
       def initialize
0
-        @plurals, @singulars, @uncountables = [], [], []
0
+        @plurals, @singulars, @uncountables, @humans = [], [], [], []
0
       end
0
 
0
       # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
0
@@ -76,9 +76,20 @@ module ActiveSupport
0
         (@uncountables << words).flatten!
0
       end
0
 
0
+      # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
0
+      # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
0
+      # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
0
+      #
0
+      # Examples:
0
+      #   human /_cnt$/i, '\1_count'
0
+      #   human "legacy_col_person_name", "Name"
0
+      def human(rule, replacement)
0
+        @humans.insert(0, [rule, replacement])
0
+      end
0
+
0
       # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
0
       # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
0
-      # <tt>:singulars</tt>, <tt>:uncountables</tt>.
0
+      # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
0
       #
0
       # Examples:
0
       #   clear :all
0
@@ -209,7 +220,10 @@ module ActiveSupport
0
     #   "employee_salary" # => "Employee salary"
0
     #   "author_id"       # => "Author"
0
     def humanize(lower_case_and_underscored_word)
0
-      lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
0
+      result = lower_case_and_underscored_word.to_s.dup
0
+
0
+      inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
0
+      result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
0
     end
0
 
0
     # Removes the module part from the expression in the string.
...
110
111
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
114
115
...
148
149
150
151
 
152
153
154
...
160
161
162
163
 
164
165
166
167
 
168
169
170
 
171
172
173
174
 
175
176
177
178
 
179
180
181
 
182
183
184
...
217
218
219
220
 
221
222
223
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
...
165
166
167
 
168
169
170
171
...
177
178
179
 
180
181
182
183
184
185
186
187
188
189
190
191
192
 
193
194
195
196
197
198
199
200
201
202
203
204
205
...
238
239
240
 
241
242
243
244
0
@@ -110,6 +110,23 @@ class InflectorTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+  def test_humanize_by_rule
0
+    ActiveSupport::Inflector.inflections do |inflect|
0
+      inflect.human(/_cnt$/i, '\1_count')
0
+      inflect.human(/^prefx_/i, '\1')
0
+    end
0
+    assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
0
+    assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
0
+  end
0
+
0
+  def test_humanize_by_string
0
+    ActiveSupport::Inflector.inflections do |inflect|
0
+      inflect.human("col_rpted_bugs", "Reported bugs")
0
+    end
0
+    assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
0
+    assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
0
+  end
0
+
0
   def test_constantize
0
     assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") }
0
     assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") }
0
@@ -148,7 +165,7 @@ class InflectorTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
-  %w{plurals singulars uncountables}.each do |inflection_type|
0
+  %w{plurals singulars uncountables humans}.each do |inflection_type|
0
     class_eval "
0
       def test_clear_#{inflection_type}
0
         cached_values = ActiveSupport::Inflector.inflections.#{inflection_type}
0
@@ -160,25 +177,29 @@ class InflectorTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_clear_all
0
-    cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables
0
+    cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables, ActiveSupport::Inflector.inflections.humans
0
     ActiveSupport::Inflector.inflections.clear :all
0
     assert ActiveSupport::Inflector.inflections.plurals.empty?
0
     assert ActiveSupport::Inflector.inflections.singulars.empty?
0
     assert ActiveSupport::Inflector.inflections.uncountables.empty?
0
+    assert ActiveSupport::Inflector.inflections.humans.empty?
0
     ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
0
     ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
0
     ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
0
+    ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
0
   end
0
 
0
   def test_clear_with_default
0
-    cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables
0
+    cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables, ActiveSupport::Inflector.inflections.humans
0
     ActiveSupport::Inflector.inflections.clear
0
     assert ActiveSupport::Inflector.inflections.plurals.empty?
0
     assert ActiveSupport::Inflector.inflections.singulars.empty?
0
     assert ActiveSupport::Inflector.inflections.uncountables.empty?
0
+    assert ActiveSupport::Inflector.inflections.humans.empty?
0
     ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
0
     ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
0
     ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
0
+    ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
0
   end
0
 
0
   Irregularities.each do |irregularity|
0
@@ -217,7 +238,7 @@ class InflectorTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
-  { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable }.each do |scope, method|
0
+  { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable, :humans => :human }.each do |scope, method|
0
     ActiveSupport::Inflector.inflections do |inflect|
0
       define_method("test_clear_inflections_with_#{scope}") do
0
         # save the inflections

Comments