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

Changeset 9209

Show
Ignore:
Timestamp:
04/02/08 11:45:03 (3 months ago)
Author:
pratik
Message:

Adding Hash#without Closes #7369 [eventualbuddha]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r9208 r9209  
    11*SVN* 
     2 
     3* Adding Hash#without Closes #7369 [eventualbuddha] 
    24 
    35* TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/hash/slice.rb

    r5726 r9209  
    1212      # 
    1313      #   search(options.slice(:mass, :velocity, :time)) 
     14      # 
     15      # Also allows leaving out certain keys. This is useful when duplicating 
     16      # a hash but omitting a certain subset: 
     17      # 
     18      #   Event.new(event.attributes.without(:id, :user_id)) 
    1419      module Slice 
    1520        # Returns a new hash with only the given keys. 
     
    2328          replace(slice(*keys)) 
    2429        end 
     30 
     31        # Returns a new hash without the given keys. 
     32        def without(*keys) 
     33          allowed = self.keys - (respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) 
     34          slice(*allowed) 
     35        end 
     36 
     37        # Replaces the hash without the given keys. 
     38        def without!(*keys) 
     39          replace(without(*keys)) 
     40        end 
    2541      end 
    2642    end 
  • trunk/activesupport/test/core_ext/hash_ext_test.rb

    r8579 r9209  
    310310    assert_equal expected, original.except!(:c) 
    311311    assert_equal expected, original 
     312  end 
     313 
     314  def test_without 
     315    original = { :a => 'x', :b => 'y', :c => 10 } 
     316    expected = { :a => 'x' } 
     317 
     318    # Should return a hash without the given keys. 
     319    assert_equal expected, original.without(:b, :c) 
     320    assert_not_equal expected, original 
     321 
     322    # Should ignore non-existant keys. 
     323    assert_equal expected, original.without(:b, :c, :d) 
     324 
     325    # Should replace the hash with the given keys taken away. 
     326    assert_equal expected, original.without!(:b, :c) 
     327    assert_equal expected, original 
     328  end 
     329 
     330  def test_indifferent_without 
     331    original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access 
     332    expected = { :c => 10 }.with_indifferent_access 
     333 
     334    [['a', 'b'], [:a, :b]].each do |keys| 
     335      # Should return a new hash without the given keys. 
     336      assert_equal expected, original.without(*keys), keys.inspect 
     337      assert_not_equal expected, original 
     338 
     339      # Should replace the hash without the given keys. 
     340      copy = original.dup 
     341      assert_equal expected, copy.without!(*keys) 
     342      assert_equal expected, copy 
     343    end 
    312344  end 
    313345end