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

Ticket #11511: inflector_acronym_config_patch.diff

File inflector_acronym_config_patch.diff, 7.7 kB (added by garru, 4 months ago)

added implementation and test cases for camel with module with this patch.

  • activesupport/test/inflector_test.rb

    old new  
    4444    CamelToUnderscore.each do |camel, underscore| 
    4545      assert_equal(camel, Inflector.camelize(underscore)) 
    4646    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') 
    4755  end 
    4856 
    4957  def test_underscore 
     
    5967    CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore| 
    6068      assert_equal(camel, Inflector.camelize(underscore)) 
    6169    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')  
    6278  end 
    6379 
    6480  def test_underscore_with_slashes 
    6581    CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore| 
    6682      assert_equal(underscore, Inflector.underscore(camel)) 
    6783    end 
     84     
     85    CamelWithModuleToUnderscoreWithSlashWithoutReverse.each do |camel, underscore| 
     86      assert_equal(underscore, Inflector.underscore(camel)) 
     87    end  
    6888  end 
    6989 
    7090  def test_demodulize 
     
    154174    end 
    155175  end 
    156176   
    157   %w{plurals singulars uncountables}.each do |inflection_type| 
     177  %w{plurals singulars uncountables camelizes}.each do |inflection_type| 
    158178    class_eval " 
    159179      def test_clear_#{inflection_type} 
    160180        cached_values = Inflector.inflections.#{inflection_type} 
     
    166186  end 
    167187   
    168188  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 
    170190    Inflector.inflections.clear :all 
    171191    assert Inflector.inflections.plurals.empty? 
    172192    assert Inflector.inflections.singulars.empty? 
    173193    assert Inflector.inflections.uncountables.empty? 
     194    assert Inflector.inflections.camelizes.empty? 
    174195    Inflector.inflections.instance_variable_set :@plurals, cached_values[0] 
    175196    Inflector.inflections.instance_variable_set :@singulars, cached_values[1] 
    176197    Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 
     198    Inflector.inflections.instance_variable_set :@camelizes, cached_values[3] 
    177199  end 
    178200   
    179201  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 
    181203    Inflector.inflections.clear 
    182204    assert Inflector.inflections.plurals.empty? 
    183205    assert Inflector.inflections.singulars.empty? 
    184206    assert Inflector.inflections.uncountables.empty? 
     207    assert Inflector.inflections.uncountables.empty? 
    185208    Inflector.inflections.instance_variable_set :@plurals, cached_values[0] 
    186209    Inflector.inflections.instance_variable_set :@singulars, cached_values[1] 
    187210    Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 
     211    Inflector.inflections.instance_variable_set :@camelizes, cached_values[3] 
    188212  end 
    189213 
    190214  Irregularities.each do |irregularity| 
     
    202226    Inflector.inflections do |inflect| 
    203227      define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do 
    204228        # save all the inflections 
    205         singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables 
     229        singulars, plurals, uncountables, camelizes = inflect.singulars, inflect.plurals, inflect.uncountables, inflect.camelizes 
    206230 
    207231        # clear all the inflections 
    208232        inflect.clear(*scope) 
     
    210234        assert_equal [], inflect.singulars 
    211235        assert_equal [], inflect.plurals 
    212236        assert_equal [], inflect.uncountables 
     237        assert_equal [], inflect.camelizes 
    213238 
    214239        # restore all the inflections 
    215240        singulars.reverse.each { |singular| inflect.singular(*singular) } 
  • activesupport/test/inflector_test_cases.rb

    old new  
    120120    "FreeBSD"               => "free_bsd", 
    121121    "HTML"                  => "html", 
    122122  } 
     123   
     124  CamelWithModuleToUnderscoreWithSlashWithoutReverse = { 
     125    "HTML::Tidy" => "html/tidy", 
     126    "Tidy::HTML" => "tidy/html" 
     127  } 
    123128 
    124129  CamelWithModuleToUnderscoreWithSlash = { 
    125130    "Admin::Product" => "admin/product", 
  • activesupport/lib/active_support/inflector.rb

    old new  
    2222  class Inflections 
    2323    include Singleton 
    2424 
    25     attr_reader :plurals, :singulars, :uncountables 
     25    attr_reader :plurals, :singulars, :uncountables, :camelizes 
    2626 
    2727    def initialize 
    28       @plurals, @singulars, @uncountables = [], [], [] 
     28      @plurals, @singulars, @uncountables , @camelizes = [], [], [], [] 
    2929    end 
    3030 
    3131    # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. 
     
    3939    def singular(rule, replacement) 
    4040      @singulars.insert(0, [rule, replacement]) 
    4141    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     
    4358    # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used 
    4459    # for strings, not regular expressions. You simply pass the irregular in singular and plural form. 
    4560    # 
     
    7792    def clear(scope = :all) 
    7893      case scope 
    7994        when :all 
    80           @plurals, @singulars, @uncountables = [], [], [] 
     95          @plurals, @singulars, @uncountables , @camelizes = [], [], [], [] 
    8196        else 
    8297          instance_variable_set "@#{scope}", [] 
    8398      end 
     
    145160  #   "active_record/errors".camelize #=> "ActiveRecord::Errors" 
    146161  #   "active_record/errors".camelize(:lower) #=> "activeRecord::Errors" 
    147162  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) } 
    148165    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 } 
    150167    else 
    151       lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] 
     168      result.first + camelize(result)[1..-1] 
    152169    end 
    153170  end 
    154171