map 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README +5 -0
  2. data/lib/map.rb +12 -1
  3. data/test/map_test.rb +26 -0
  4. metadata +6 -6
data/README CHANGED
@@ -106,6 +106,11 @@ DESCRIPTION
106
106
  m.get(:x, :y, :z) #=> nil
107
107
  m[:x][:y][:z] #=> raises exception!
108
108
 
109
+ m = Map.new(:array => [0,1])
110
+ defaults = {:array => [nil, nil, 2]}
111
+ m.apply!(defaults)
112
+ p m[:array] #=> [0,1,2]
113
+
109
114
  # they also support some different iteration styles
110
115
  #
111
116
  m = Map.new
data/lib/map.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Map < Hash
2
- Version = '2.1.0' unless defined?(Version)
2
+ Version = '2.2.0' unless defined?(Version)
3
3
  Load = Kernel.method(:load) unless defined?(Load)
4
4
 
5
5
  class << Map
@@ -606,6 +606,17 @@ class Map < Hash
606
606
  return options.values
607
607
  end
608
608
 
609
+ def apply(other)
610
+ dup.apply!(other)
611
+ end
612
+
613
+ def apply!(other)
614
+ Map.for(other).depth_first_each do |keys, value|
615
+ set(keys => value) unless !get(keys).nil?
616
+ end
617
+ self
618
+ end
619
+
609
620
  def Map.alphanumeric_key_for(key)
610
621
  return key if Numeric===key
611
622
  key.to_s =~ %r/^\d+$/ ? Integer(key) : key
data/test/map_test.rb CHANGED
@@ -282,6 +282,32 @@ Testing Map do
282
282
  assert{ m[:x][:y][:z] == 42.0 }
283
283
  end
284
284
 
285
+ testing 'that setting a sub-container does not eff up the container values' do
286
+ m = Map.new
287
+ assert{ m.set(:array => [0,1,2]) }
288
+ assert{ m.get(:array, 0) == 0 }
289
+ assert{ m.get(:array, 1) == 1 }
290
+ assert{ m.get(:array, 2) == 2 }
291
+
292
+ assert{ m.set(:array, 2, 42) }
293
+ assert{ m.get(:array, 0) == 0 }
294
+ assert{ m.get(:array, 1) == 1 }
295
+ assert{ m.get(:array, 2) == 42 }
296
+ end
297
+
298
+ testing 'that #apply selectively merges non-nil values' do
299
+ m = Map.new(:array => [0, 1], :hash => {:a => false, :b => nil, :c => 42})
300
+ defaults = Map.new(:array => [nil, nil, 2], :hash => {:b => true})
301
+
302
+ applied = assert{ m.apply(defaults) }
303
+ assert{ applied[:array] == [0,1,2] }
304
+ assert{ applied[:hash] =~ {:a => false, :b => true, :c => 42} }
305
+
306
+ assert{ m.apply!(defaults) }
307
+ assert{ m[:array] == [0,1,2] }
308
+ assert{ m[:hash] =~ {:a => false, :b => true, :c => 42} }
309
+ end
310
+
285
311
  testing 'that maps support depth_first_each' do
286
312
  m = Map.new
287
313
  prefix = %w[ a b c ]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: map
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
5
- prerelease: false
4
+ hash: 7
5
+ prerelease:
6
6
  segments:
7
7
  - 2
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 2.1.0
10
+ version: 2.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ara T. Howard
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-05 00:00:00 -07:00
18
+ date: 2011-01-19 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements: []
68
68
 
69
69
  rubyforge_project: codeforpeople
70
- rubygems_version: 1.3.7
70
+ rubygems_version: 1.4.2
71
71
  signing_key:
72
72
  specification_version: 3
73
73
  summary: map