Skip to content

Commit

Permalink
Add Inflection rules for String#humanize. [#535 state:resolved] [dcma…
Browse files Browse the repository at this point in the history
…nges]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
lifo committed Jul 2, 2008
1 parent 3a95ee7 commit 4f75840
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
8 changes: 8 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,13 @@
*Edge*

* Add Inflection rules for String#humanize. #535 [dcmanges]

ActiveSupport::Inflector.inflections do |inflect|
inflect.human(/_cnt$/i, '\1_count')
end

'jargon_cnt'.humanize # => 'Jargon count'

* 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]

* Added TimeZone #=~, to support matching zones by regex in time_zone_select. #195 [Ernie Miller]
Expand Down
22 changes: 18 additions & 4 deletions activesupport/lib/active_support/inflector.rb
Expand Up @@ -30,10 +30,10 @@ module Inflector
class Inflections
include Singleton

attr_reader :plurals, :singulars, :uncountables
attr_reader :plurals, :singulars, :uncountables, :humans

def initialize
@plurals, @singulars, @uncountables = [], [], []
@plurals, @singulars, @uncountables, @humans = [], [], [], []
end

# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
Expand Down Expand Up @@ -76,9 +76,20 @@ def uncountable(*words)
(@uncountables << words).flatten!
end

# Specifies a humanized form of a string by a regular expression rule or by a string mapping.
# When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
# When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
#
# Examples:
# human /_cnt$/i, '\1_count'
# human "legacy_col_person_name", "Name"
def human(rule, replacement)
@humans.insert(0, [rule, replacement])
end

# Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
# Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
# <tt>:singulars</tt>, <tt>:uncountables</tt>.
# <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
#
# Examples:
# clear :all
Expand Down Expand Up @@ -209,7 +220,10 @@ def dasherize(underscored_word)
# "employee_salary" # => "Employee salary"
# "author_id" # => "Author"
def humanize(lower_case_and_underscored_word)
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
result = lower_case_and_underscored_word.to_s.dup

inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
end

# Removes the module part from the expression in the string.
Expand Down
29 changes: 25 additions & 4 deletions activesupport/test/inflector_test.rb
Expand Up @@ -110,6 +110,23 @@ def test_humanize
end
end

def test_humanize_by_rule
ActiveSupport::Inflector.inflections do |inflect|
inflect.human(/_cnt$/i, '\1_count')
inflect.human(/^prefx_/i, '\1')
end
assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
end

def test_humanize_by_string
ActiveSupport::Inflector.inflections do |inflect|
inflect.human("col_rpted_bugs", "Reported bugs")
end
assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
end

def test_constantize
assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") }
assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") }
Expand Down Expand Up @@ -148,7 +165,7 @@ def test_underscore_to_lower_camel
end
end

%w{plurals singulars uncountables}.each do |inflection_type|
%w{plurals singulars uncountables humans}.each do |inflection_type|
class_eval "
def test_clear_#{inflection_type}
cached_values = ActiveSupport::Inflector.inflections.#{inflection_type}
Expand All @@ -160,25 +177,29 @@ def test_clear_#{inflection_type}
end

def test_clear_all
cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables
cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables, ActiveSupport::Inflector.inflections.humans
ActiveSupport::Inflector.inflections.clear :all
assert ActiveSupport::Inflector.inflections.plurals.empty?
assert ActiveSupport::Inflector.inflections.singulars.empty?
assert ActiveSupport::Inflector.inflections.uncountables.empty?
assert ActiveSupport::Inflector.inflections.humans.empty?
ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
end

def test_clear_with_default
cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables
cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables, ActiveSupport::Inflector.inflections.humans
ActiveSupport::Inflector.inflections.clear
assert ActiveSupport::Inflector.inflections.plurals.empty?
assert ActiveSupport::Inflector.inflections.singulars.empty?
assert ActiveSupport::Inflector.inflections.uncountables.empty?
assert ActiveSupport::Inflector.inflections.humans.empty?
ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
end

Irregularities.each do |irregularity|
Expand Down Expand Up @@ -217,7 +238,7 @@ def test_clear_with_default
end
end

{ :singulars => :singular, :plurals => :plural, :uncountables => :uncountable }.each do |scope, method|
{ :singulars => :singular, :plurals => :plural, :uncountables => :uncountable, :humans => :human }.each do |scope, method|
ActiveSupport::Inflector.inflections do |inflect|
define_method("test_clear_inflections_with_#{scope}") do
# save the inflections
Expand Down

0 comments on commit 4f75840

Please sign in to comment.