map 1.0.0 → 1.1.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 (5) hide show
  1. data/README +26 -0
  2. data/a.rb +6 -0
  3. data/lib/map.rb +10 -7
  4. data/test/{map.rb → map_test.rb} +24 -4
  5. metadata +9 -7
data/README ADDED
@@ -0,0 +1,26 @@
1
+
2
+ m = Map.new
3
+
4
+ m[:a] = :b
5
+ m[:b] = :b
6
+ m[:c] = :c
7
+
8
+ p m.keys #=> [:a, :b, :c] ### always ordered!
9
+
10
+
11
+ p m[:a] #=> :a
12
+ p m["a"] #=> :a
13
+
14
+
15
+ m = Map[:k, :v, :key, :val]
16
+ m = Map[[:k, :v], [:key, :val]]
17
+ m = Map[{:k => :v, :key => :val}]
18
+
19
+ m = Map(:k, :v, :key, :val)
20
+ m = Map([:k, :v], [:key, :val])
21
+ m = Map(:k => :v, :key => :val)
22
+
23
+
24
+ m.update(:k2, :v2)
25
+ m.update(:k2 => :v2)
26
+
data/a.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+ map = Map(:a => [{:b => 42}])
3
+
4
+ p map['a']
5
+ p map['a'][0]
6
+ p map['a'][0]['b']
data/lib/map.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Map < Hash
2
- Version = '1.0.0' unless defined?(Version)
2
+ Version = '1.1.0' unless defined?(Version)
3
3
  Load = Kernel.method(:load) unless defined?(Load)
4
4
 
5
5
  class << Map
@@ -32,6 +32,7 @@ class Map < Hash
32
32
  def new_from_hash(hash)
33
33
  map = new
34
34
  map.update(hash)
35
+ map.default = hash.default
35
36
  map
36
37
  end
37
38
 
@@ -121,19 +122,21 @@ class Map < Hash
121
122
  key.kind_of?(Symbol) ? key.to_s : key
122
123
  end
123
124
 
124
- def convert_val(val)
125
- case val
125
+ def convert_value(value)
126
+ return value.to_map if value.respond_to?(:to_map)
127
+ case value
126
128
  when Hash
127
- map_for(val)
129
+ Map.for(value)
128
130
  when Array
129
- val.collect{|v| Hash === v ? map_for(v) : v}
131
+ value.map{|v| convert_value(v)}
130
132
  else
131
- val
133
+ value
132
134
  end
133
135
  end
136
+ alias_method('convert_val', 'convert_value')
134
137
 
135
138
  def convert(key, val)
136
- [convert_key(key), convert_val(val)]
139
+ [convert_key(key), convert_value(val)]
137
140
  end
138
141
 
139
142
  # maps are aggressive with copy operations. they are all deep copies. make a
@@ -10,6 +10,14 @@ Testing Map do
10
10
  assert{ Map.new(hash = {}) }
11
11
  end
12
12
 
13
+ testing 'that the contructor accepts a hash and preserves the default value' do
14
+ hash = {}
15
+ hash.default = 42
16
+ assert{ hash[:missing] == 42 }
17
+ map = assert{ Map.new(hash) }
18
+ assert{ map[:missing] == 42 }
19
+ end
20
+
13
21
  testing 'that the constructor accepts the empty array' do
14
22
  array = []
15
23
  assert{ Map.new(array) }
@@ -97,9 +105,8 @@ Testing Map do
97
105
  end
98
106
 
99
107
  testing 'that maps are string/symbol indifferent for recursive look-ups' do
100
- map = Map.new
101
- assert{ map[:a] = Map[:b, 42] }
102
- assert{ map[:a][:b] = Map[:c, 42] }
108
+ map = assert{ Map(:a => {:b => {:c => 42}}) }
109
+ assert{ map[:a] = {:b => {:c, 42}} }
103
110
  assert{ map[:a][:b][:c] == 42 }
104
111
  assert{ map['a'][:b][:c] == 42 }
105
112
  assert{ map['a']['b'][:c] == 42 }
@@ -108,6 +115,18 @@ Testing Map do
108
115
  assert{ map[:a]['b']['c'] == 42 }
109
116
  assert{ map[:a][:b]['c'] == 42 }
110
117
  assert{ map['a'][:b]['c'] == 42 }
118
+
119
+ map = assert{ Map(:a => [{:b => 42}]) }
120
+ assert{ map['a'].is_a?(Array) }
121
+ assert{ map['a'][0].is_a?(Map) }
122
+ assert{ map['a'][0]['b'] == 42 }
123
+
124
+ map = assert{ Map(:a => [ {:b => 42}, [{:c => 'forty-two'}] ]) }
125
+ assert{ map['a'].is_a?(Array) }
126
+ assert{ map['a'][0].is_a?(Map) }
127
+ assert{ map['a'][1].is_a?(Array) }
128
+ assert{ map['a'][0]['b'] == 42 }
129
+ assert{ map['a'][1][0]['c'] == 'forty-two' }
111
130
  end
112
131
 
113
132
  testing 'that maps support shift like a good ordered container' do
@@ -146,12 +165,13 @@ end
146
165
 
147
166
 
148
167
 
168
+
169
+
149
170
  BEGIN {
150
171
  testdir = File.dirname(File.expand_path(__FILE__))
151
172
  testlibdir = File.join(testdir, 'lib')
152
173
  rootdir = File.dirname(testdir)
153
174
  libdir = File.join(rootdir, 'lib')
154
-
155
175
  $LOAD_PATH.push(libdir)
156
176
  $LOAD_PATH.push(testlibdir)
157
177
  }
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: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.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: 2010-10-03 00:00:00 -06:00
18
+ date: 2010-10-05 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -28,10 +28,12 @@ extensions: []
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
+ - a.rb
31
32
  - lib/map.rb
32
33
  - Rakefile
34
+ - README
33
35
  - test/lib/testing.rb
34
- - test/map.rb
36
+ - test/map_test.rb
35
37
  has_rdoc: true
36
38
  homepage: http://github.com/ahoward/map/tree/master
37
39
  licenses: []
@@ -66,5 +68,5 @@ rubygems_version: 1.3.7
66
68
  signing_key:
67
69
  specification_version: 3
68
70
  summary: map
69
- test_files:
70
- - test/map.rb
71
+ test_files: []
72
+