map 2.4.0 → 2.4.1

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/lib/map.rb +25 -20
  2. data/map.gemspec +1 -1
  3. data/test/map_test.rb +12 -26
  4. metadata +4 -4
data/lib/map.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Map < Hash
2
- Version = '2.4.0' unless defined?(Version)
2
+ Version = '2.4.1' unless defined?(Version)
3
3
  Load = Kernel.method(:load) unless defined?(Load)
4
4
 
5
5
  class << Map
@@ -97,31 +97,28 @@ class Map < Hash
97
97
  result = args
98
98
  end
99
99
 
100
- return result if size == 0
100
+ return args if size == 0
101
101
 
102
102
  if size == 1
103
+ conversion_methods.each do |method|
104
+ if first.respond_to?(method)
105
+ first = first.send(method)
106
+ break
107
+ end
108
+ end
109
+
103
110
  if first.respond_to?(:each_pair)
104
111
  first.each_pair do |key, val|
105
112
  block.call(key, val)
106
113
  end
107
- return result
114
+ return args
108
115
  end
109
116
 
110
117
  if first.respond_to?(:each_slice)
111
118
  first.each_slice(2) do |key, val|
112
119
  block.call(key, val)
113
120
  end
114
- return result
115
- end
116
-
117
- conversion_methods.each do |method|
118
- if first.respond_to?(method)
119
- first = first.send(method)
120
- first.each_pair do |key, val|
121
- block.call(key, val)
122
- end
123
- return result
124
- end
121
+ return args
125
122
  end
126
123
 
127
124
  raise(ArgumentError, 'odd number of arguments for Map')
@@ -210,14 +207,18 @@ class Map < Hash
210
207
  klass.map_for(hash)
211
208
  end
212
209
 
213
- def Map.convert_key(key)
210
+ def self.convert_key(key)
214
211
  key.kind_of?(Symbol) ? key.to_s : key
215
212
  end
216
213
  def convert_key(key)
217
- klass.convert_key(key)
214
+ if klass.respond_to?(:convert_key)
215
+ klass.convert_key(key)
216
+ else
217
+ Map.convert_key(key)
218
+ end
218
219
  end
219
220
 
220
- def Map.convert_value(value)
221
+ def self.convert_value(value)
221
222
  conversion_methods.each do |method|
222
223
  return value.send(method) if value.respond_to?(method)
223
224
  end
@@ -232,7 +233,11 @@ class Map < Hash
232
233
  end
233
234
  end
234
235
  def convert_value(value)
235
- klass.convert_value(value)
236
+ if klass.respond_to?(:convert_value)
237
+ klass.convert_value(value)
238
+ else
239
+ Map.convert_value(value)
240
+ end
236
241
  end
237
242
  alias_method('convert_val', 'convert_value')
238
243
 
@@ -375,9 +380,9 @@ class Map < Hash
375
380
  self
376
381
  end
377
382
 
378
- def replace(hash)
383
+ def replace(*args)
379
384
  clear
380
- update(hash)
385
+ update(*args)
381
386
  end
382
387
 
383
388
  # ordered container specific methods
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "map"
6
- spec.version = "2.4.0"
6
+ spec.version = "2.4.1"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "map"
9
9
  spec.description = "description: map kicks the ass"
@@ -331,35 +331,21 @@ Testing Map do
331
331
  end
332
332
 
333
333
  testing 'that Map.each_pair works on arrays' do
334
+ each = []
334
335
  array = %w( a b c )
335
- each_pair = Map.each_pair(array)
336
- assert{ each_pair == [['a', 'b'], ['c', nil]] }
336
+ Map.each_pair(array){|k,v| each.push(k,v)}
337
+ assert{ each_pair = ['a', 'b', 'c', nil] }
337
338
  end
338
339
 
339
- testing 'that Map.each_pair works on hashes' do
340
- hash = {'a' => 'b', 'c' => nil}
341
- each_pair = Map.each_pair(hash)
342
- assert{ each_pair == [['a', 'b'], ['c', nil]] }
343
- end
344
-
345
- testing 'that Map.each_pair works on things which respond_to conversion methods' do
346
- object = Object.new
347
- def object.to_map
348
- {'a' => 'b', 'c' => nil}
349
- end
350
- each_pair = Map.each_pair(object)
351
- assert{ each_pair == [['a', 'b'], ['c', nil]] }
352
- end
353
-
354
- testing 'that Map.each_pair blows up on non-hash, non-array, non-convertable objects' do
355
- object = Object.new
356
- result =
357
- begin
358
- Map.each_pair(object)
359
- rescue => e
360
- e
361
- end
362
- assert{ result.is_a?(Exception) }
340
+ testing 'that #update and #replace accept map-ish objects' do
341
+ o = Object.new
342
+ def o.to_map() {:k => :v} end
343
+ m = Map.new
344
+ assert{ m.update(o) }
345
+ assert{ m =~ {:k => :v} }
346
+ m[:a] = :b
347
+ assert{ m.replace(o) }
348
+ assert{ m =~ {:k => :v} }
363
349
  end
364
350
 
365
351
  protected
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: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 4
9
- - 0
10
- version: 2.4.0
9
+ - 1
10
+ version: 2.4.1
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-02-09 00:00:00 -07:00
18
+ date: 2011-02-10 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21