map 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -8
- data/lib/map.rb +1 -1
- data/lib/map/struct.rb +1 -1
- data/test/lib/testing.rb +10 -4
- data/test/map_test.rb +1 -1
- metadata +2 -2
data/README
CHANGED
@@ -6,10 +6,10 @@ SYNOPSIS
|
|
6
6
|
hash that works in all rubies
|
7
7
|
|
8
8
|
maps are rad ordered hashes that are both ordered, string/symbol
|
9
|
-
indifferent, and have all sorts of sweetness like recursive
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
indifferent, and have all sorts of sweetness like recursive conversion, more
|
10
|
+
robust implementation than HashWithIndifferentAccess, support for struct
|
11
|
+
like (map.foo) access, and support for option/keyword access which avoids
|
12
|
+
several nasty classes of errors in many ruby libraries
|
13
13
|
|
14
14
|
INSTALL
|
15
15
|
gem install map
|
@@ -24,9 +24,11 @@ DESCRIPTION
|
|
24
24
|
#
|
25
25
|
m = Map[:k, :v, :key, :val]
|
26
26
|
m = Map(:k, :v, :key, :val)
|
27
|
+
m = Map.new(:k, :v, :key, :val)
|
27
28
|
|
28
29
|
m = Map[[:k, :v], [:key, :val]]
|
29
|
-
m = Map
|
30
|
+
m = Map(:k => :v, :key => :val) # ruh-oh, the input hash loses order!
|
31
|
+
m = Map.new(:k => :v, :key => :val) # ruh-oh, the input hash loses order!
|
30
32
|
|
31
33
|
|
32
34
|
m = Map.new
|
@@ -37,16 +39,23 @@ DESCRIPTION
|
|
37
39
|
p m.keys #=> ['a','b','c'] ### always ordered!
|
38
40
|
p m.keys #=> [0,1,2] ### always ordered!
|
39
41
|
|
40
|
-
# maps don't care
|
42
|
+
# maps don't care about symbol vs.string keys
|
41
43
|
#
|
42
44
|
p m[:a] #=> 0
|
43
45
|
p m["a"] #=> 0
|
44
46
|
|
47
|
+
# even via deep nesting
|
48
|
+
#
|
49
|
+
p m[:foo]['bar'][:baz] #=> 42
|
50
|
+
|
45
51
|
# many functions operate in a way one would expect from an ordered container
|
46
52
|
#
|
47
53
|
m.update(:k2 => :v2)
|
48
54
|
m.update(:k2, :v2)
|
49
55
|
|
56
|
+
key_val_pair = m.shift
|
57
|
+
key_val_pair = m.pop
|
58
|
+
|
50
59
|
# maps keep mapiness for even deep operations
|
51
60
|
#
|
52
61
|
m.update :nested => {:hashes => {:are => :converted}}
|
@@ -58,7 +67,7 @@ DESCRIPTION
|
|
58
67
|
p s.foo.bar #=> 42
|
59
68
|
|
60
69
|
# because option parsing is such a common use case for needing string/symbol
|
61
|
-
# indifference map.rb comes out of the box
|
70
|
+
# indifference map.rb comes out of the box loaded with option support
|
62
71
|
#
|
63
72
|
def foo(*args, &block)
|
64
73
|
opts = Map.options(args)
|
@@ -76,7 +85,7 @@ DESCRIPTION
|
|
76
85
|
|
77
86
|
# this avoids such bugs as
|
78
87
|
#
|
79
|
-
options = {:read_only =>
|
88
|
+
options = {:read_only => false}
|
80
89
|
read_only = options[:read_only] || true # should be false but is true
|
81
90
|
|
82
91
|
# with options this becomes
|
data/lib/map.rb
CHANGED
data/lib/map/struct.rb
CHANGED
data/test/lib/testing.rb
CHANGED
@@ -4,7 +4,9 @@
|
|
4
4
|
|
5
5
|
def Testing(*args, &block)
|
6
6
|
Class.new(Test::Unit::TestCase) do
|
7
|
-
|
7
|
+
eval("This=self")
|
8
|
+
|
9
|
+
def This.slug_for(*args)
|
8
10
|
string = args.flatten.compact.join('-')
|
9
11
|
words = string.to_s.scan(%r/\w+/)
|
10
12
|
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
|
@@ -12,10 +14,14 @@
|
|
12
14
|
words.join('-').downcase
|
13
15
|
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
def This.testing_subclass_count
|
18
|
+
@testing_subclass_count ||= 1
|
19
|
+
ensure
|
20
|
+
@testing_subclass_count += 1
|
21
|
+
end
|
22
|
+
|
17
23
|
slug = slug_for(*args).gsub(%r/-/,'_')
|
18
|
-
name = ['TESTING', '%03d' %
|
24
|
+
name = ['TESTING', '%03d' % This.testing_subclass_count, slug].delete_if{|part| part.empty?}.join('_')
|
19
25
|
name = name.upcase!
|
20
26
|
const_set(:Name, name)
|
21
27
|
def self.name() const_get(:Name) end
|
data/test/map_test.rb
CHANGED
@@ -106,7 +106,7 @@ Testing Map do
|
|
106
106
|
|
107
107
|
testing 'that maps are string/symbol indifferent for recursive look-ups' do
|
108
108
|
map = assert{ Map(:a => {:b => {:c => 42}}) }
|
109
|
-
assert{ map[:a] = {:b => {:c
|
109
|
+
assert{ map[:a] = {:b => {:c => 42}} }
|
110
110
|
assert{ map[:a][:b][:c] == 42 }
|
111
111
|
assert{ map['a'][:b][:c] == 42 }
|
112
112
|
assert{ map['a']['b'][:c] == 42 }
|