Changeset 1184

Show
Ignore:
Timestamp:
01/06/08 15:06:18 (11 months ago)
Author:
e.@brainspl.at
Message:

add specs for Hash core_ext, fix up symbolize_keys! (do we even need this in core?) closes #409 [chuyeow@gmail.com + sethrasmussen@gmail.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/core_ext/hash.rb

    r1133 r1184  
    7575          hash 
    7676        end 
    77          
    78        # convert this hash to a query string param 
    79        #   {:name => "Bob", :address => {:street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222']}} 
     77 
     78  # convert this hash to a query string param 
     79  #   {:name => "Bob", :address => {:street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222']}}.to_params 
    8080  #   #=> "name=Bob&address[city]=Ruby Central&address[phones]=111-111-1111222-222-2222&address[street]=111 Ruby Ave." 
    8181  def to_params 
     
    100100   
    101101  # lets through the keys in the argument 
    102   #   $ {:one => 1, :two => 2, :three => 3}.pass(:one) 
     102  #   {:one => 1, :two => 2, :three => 3}.pass(:one) 
    103103  #   #=> {:one=>1} 
    104   def pass(*allowed)  
     104  def only(*allowed)  
    105105    self.reject { |k,v| ! allowed.include?(k) }  
    106106  end 
    107   alias only pass 
    108    
     107 
    109108  # blocks the keys in the arguments 
    110   #   $ {:one => 1, :two => 2, :three => 3}.block(:one) 
     109  #   {:one => 1, :two => 2, :three => 3}.block(:one) 
    111110  #   #=> {:two=>2, :three=>3} 
    112   def block(*rejected)  
     111  def except(*rejected)  
    113112    self.reject { |k,v| rejected.include?(k) }  
    114113  end 
    115   alias except block 
    116114 
    117115  # Converts the hash into xml attributes 
     
    120118  def to_xml_attributes 
    121119    map do |k,v| 
    122       "#{k.to_s.camelize.sub(/^(.{1,1})/){|m| m.downcase}}=\"#{v}\"" 
     120      %{#{k.to_s.camelize.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}  
    123121    end.join(" ") 
    124122  end 
     
    142140    end 
    143141  end 
    144    
     142 
    145143  # Destructively convert all keys to symbols recursively. 
     144  #  { 'one' => 1, 'two' => 2 }.symbolize_keys! 
     145  #  #=> { :one => 1, :two => 2 } 
    146146  def symbolize_keys! 
    147     keys.each do |key| 
    148       unless key.is_a?(Symbol) 
    149         self[key.to_sym] = self[key] 
    150         delete(key) 
    151       end 
    152       if Hash === (sub = self[key.to_sym]) 
    153         sub.symbolize_keys! 
    154       end   
     147    each do |k,v|  
     148          sym = k.respond_to?(:to_sym) ? k.to_sym : k  
     149          self[sym] = Hash === v ? v.symbolize_keys! : v  
     150          delete(k) unless k == sym 
    155151    end 
    156152    self 
    157153  end 
    158    
     154 
    159155  # Destructively convert each key to an uppercase string non-recursively. 
    160156  #   {:name => "Bob", "age" => 12, "nick" => "Bobinator"}.environmentize_keys! 
  • trunk/spec/merb/core_ext_spec.rb

    r1133 r1184  
    202202    { :test_1  => { :test2 => 'test'}}.environmentize_keys!.should ==  
    203203    { 'TEST_1' => { :test2 => 'test'}} 
     204  end 
     205end 
     206 
     207describe Hash, "only" do 
     208 
     209  before do 
     210    @hash = { :one => 'ONE', 'two' => 'TWO', 3 => 'THREE' } 
     211  end 
     212 
     213  it "should return a hash with only the given key(s)" do 
     214    @hash.only(:one).should == { :one => 'ONE' } 
     215    @hash.only(:one, 3).should == { :one => 'ONE', 3 => 'THREE' } 
     216  end 
     217end 
     218 
     219describe Hash, "except" do |variable| 
     220 
     221  before do 
     222    @hash = { :one => 'ONE', 'two' => 'TWO', 3 => 'THREE' } 
     223  end 
     224 
     225  it "should return a hash without only the given key(s)" do 
     226    @hash.except(:one).should == { 'two' => 'TWO', 3 => 'THREE' } 
     227    @hash.except(:one, 3).should == { 'two' => 'TWO' } 
     228  end 
     229end 
     230 
     231describe Hash, "symbolize_keys!" do 
     232 
     233  before do 
     234    @hash = { 'one' => 1, 'two' => 2 } 
     235    @hash_with_another_hash = { 'a' => 'A', 'prefs' => { 'private' => true, 'sex' => 'yes please' } } 
     236    @hash_with_non_string_keys = { 'one' => 1, 2 => 'TWO', '' => 'blank' } 
     237  end 
     238 
     239  it "should convert all keys to symbols" do 
     240    @hash.symbolize_keys! 
     241    @hash.should == { :one => 1, :two => 2 } 
     242  end 
     243 
     244  it "should recursively convert all keys to symbols" do 
     245    @hash_with_another_hash.symbolize_keys! 
     246    @hash_with_another_hash.should == { :a => 'A', :prefs => { :private => true, :sex => 'yes please' } } 
    204247  end 
    205248end 
     
    379422                                    } 
    380423  end 
    381    
    382    
    383    
     424end 
     425 
     426describe Hash, 'to_params' do 
     427 
     428  before do 
     429    @hash = { :name => 'Bob', :address => { :street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222'] } } 
     430  end 
     431 
     432  it "should convert correctly into query parameters" do 
     433    @hash.to_params.split('&').sort.should == 'name=Bob&address[city]=Ruby Central&address[phones]=111-111-1111222-222-2222&address[street]=111 Ruby Ave.'.split('&').sort 
     434  end 
    384435end 
    385436