Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    289289      assert_equal(human, Inflector.humanize(underscore)) 
    290290    end 
    291291  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 
    292318 
    293319  def test_constantize 
    294320    assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") 
  • lib/active_support/inflector.rb

    old new  
    1414  #     inflect.irregular 'octopus', 'octopi' 
    1515  # 
    1616  #     inflect.uncountable "equipment" 
     17  # 
     18  #     inflect.human /^prefx_/i, '\1' 
    1719  #   end 
    1820  # 
    1921  # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the 
     
    2224  class Inflections 
    2325    include Singleton 
    2426     
    25     attr_reader :plurals, :singulars, :uncountables 
     27    attr_reader :plurals, :singulars, :uncountables, :humans, :human_strings 
    2628     
    2729    def initialize 
    28       @plurals, @singulars, @uncountables = [], [], [] 
     30      @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 
    2931    end 
    3032     
    3133    # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.  
     
    6163      (@uncountables << words).flatten! 
    6264    end 
    6365     
     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     
    6486    # 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 
    6688    # 
    6789    # Examples: 
    6890    #   clear :all 
     
    7092    def clear(scope = :all) 
    7193      case scope 
    7294        when :all 
    73           @plurals, @singulars, @uncountables = [], [], [] 
     95          @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 
    7496        else 
    7597          instance_variable_set "@#{scope}", [] 
     98          @human_strings = {} if scope == :humans 
    7699      end 
    77100    end 
    78101  end 
     
    134157  end 
    135158 
    136159  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 
    138167  end 
    139168 
    140169  def demodulize(class_name_in_module)