Changeset 9209
- Timestamp:
- 04/02/08 11:45:03 (8 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/hash/slice.rb (modified) (2 diffs)
- trunk/activesupport/test/core_ext/hash_ext_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r9208 r9209 1 1 *SVN* 2 3 * Adding Hash#without Closes #7369 [eventualbuddha] 2 4 3 5 * 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 12 12 # 13 13 # 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)) 14 19 module Slice 15 20 # Returns a new hash with only the given keys. … … 23 28 replace(slice(*keys)) 24 29 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 25 41 end 26 42 end trunk/activesupport/test/core_ext/hash_ext_test.rb
r8579 r9209 310 310 assert_equal expected, original.except!(:c) 311 311 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 312 344 end 313 345 end