map 2.9.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/map.rb +21 -19
- data/map.gemspec +1 -1
- data/test/map_test.rb +5 -5
- metadata +6 -6
data/lib/map.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Map < Hash
|
2
|
-
Version = '
|
2
|
+
Version = '3.0.0' unless defined?(Version)
|
3
3
|
Load = Kernel.method(:load) unless defined?(Load)
|
4
4
|
|
5
5
|
class << Map
|
@@ -63,11 +63,13 @@ class Map < Hash
|
|
63
63
|
next if name.empty?
|
64
64
|
name.downcase.gsub(/::/, '_')
|
65
65
|
end.compact
|
66
|
-
type_names.map{|type_name| "to_#{ type_name }"}
|
66
|
+
list = type_names.map{|type_name| "to_#{ type_name }"}
|
67
|
+
list.each{|method| define_conversion_method!(method)}
|
68
|
+
list
|
67
69
|
)
|
68
70
|
end
|
69
71
|
|
70
|
-
def
|
72
|
+
def define_conversion_method!(method)
|
71
73
|
method = method.to_s.strip
|
72
74
|
raise ArguementError if method.empty?
|
73
75
|
module_eval(<<-__, __FILE__, __LINE__)
|
@@ -75,16 +77,26 @@ class Map < Hash
|
|
75
77
|
def #{ method }
|
76
78
|
self
|
77
79
|
end
|
78
|
-
|
79
|
-
|
80
|
-
|
80
|
+
true
|
81
|
+
else
|
82
|
+
false
|
81
83
|
end
|
82
84
|
__
|
83
85
|
end
|
84
86
|
|
85
|
-
def
|
86
|
-
|
87
|
-
|
87
|
+
def add_conversion_method!(method)
|
88
|
+
if define_conversion_method!(method)
|
89
|
+
method = method.to_s.strip
|
90
|
+
raise ArguementError if method.empty?
|
91
|
+
module_eval(<<-__, __FILE__, __LINE__)
|
92
|
+
unless conversion_methods.include?(#{ method.inspect })
|
93
|
+
conversion_methods.unshift(#{ method.inspect })
|
94
|
+
end
|
95
|
+
__
|
96
|
+
true
|
97
|
+
else
|
98
|
+
false
|
99
|
+
end
|
88
100
|
end
|
89
101
|
|
90
102
|
# iterate over arguments in pairs smartly.
|
@@ -158,16 +170,6 @@ class Map < Hash
|
|
158
170
|
end
|
159
171
|
end
|
160
172
|
|
161
|
-
unless defined?(Dynamic)
|
162
|
-
Dynamic = lambda do
|
163
|
-
conversion_methods.reverse_each do |method|
|
164
|
-
add_conversion_method!(method)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
module_eval(&Dynamic)
|
168
|
-
end
|
169
|
-
|
170
|
-
|
171
173
|
# instance constructor
|
172
174
|
#
|
173
175
|
def keys
|
data/map.gemspec
CHANGED
data/test/map_test.rb
CHANGED
@@ -284,15 +284,15 @@ Testing Map do
|
|
284
284
|
|
285
285
|
testing 'that method missing hacks allow setting values, but not getting them until they are set' do
|
286
286
|
m = Map.new
|
287
|
-
assert{ (m.
|
288
|
-
assert{ m.
|
289
|
-
assert{ m[:
|
290
|
-
assert{ m.
|
287
|
+
assert{ (m.missing rescue $!).is_a?(Exception) }
|
288
|
+
assert{ m.missing = :val }
|
289
|
+
assert{ m[:missing] == :val }
|
290
|
+
assert{ m.missing == :val }
|
291
291
|
end
|
292
292
|
|
293
293
|
testing 'that method missing with a block delegatets to fetch' do
|
294
294
|
m = Map.new
|
295
|
-
assert{ m.
|
295
|
+
assert{ m.missing{ :val } == :val }
|
296
296
|
assert{ !m.has_key?(:key) }
|
297
297
|
end
|
298
298
|
|
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:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
-
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version:
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 3.0.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-04-
|
18
|
+
date: 2011-04-21 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|