hashery 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ Thomas Sawyer
2
+ Jan Molic
3
+ George Moschovitis
@@ -0,0 +1 @@
1
+ 2010-04-21
@@ -0,0 +1,5 @@
1
+ The Facets Hashery is collection of Hash-like classes, spun-off
2
+ from the original Ruby Facets library. Included are the widely
3
+ used OrderedHash, the related but more featured Dictionary
4
+ class, a number of _open_ classes, similiar to the standard
5
+ OpenStruct and variations on the standard Hash.
@@ -0,0 +1 @@
1
+ http://rubyworks.github.com/hashery
@@ -0,0 +1 @@
1
+ MIT
@@ -0,0 +1 @@
1
+ hashery
@@ -0,0 +1 @@
1
+ 2009-06-26
@@ -0,0 +1 @@
1
+ git://github.com/rubyworks/hashery.git
@@ -0,0 +1 @@
1
+ rubyworks
@@ -0,0 +1 @@
1
+ Facets bread collection of Hash-like classes.
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,142 @@
1
+ require 'hashery/dictionary.rb'
2
+ require 'ae/legacy'
3
+
4
+ TestCase Dictionary do
5
+
6
+ Unit :create do
7
+ d = Dictionary['z', 1, 'a', 2, 'c', 3]
8
+ assert_equal( ['z','a','c'], d.keys )
9
+ end
10
+
11
+ Unit :[]= do
12
+ d = Dictionary.new
13
+ d['z'] = 1
14
+ d['a'] = 2
15
+ d['c'] = 3
16
+ assert_equal( ['z','a','c'], d.keys )
17
+ end
18
+
19
+ Unit :push do
20
+ d = Dictionary['a', 1, 'c', 2, 'z', 3]
21
+ assert( d.push('end', 15) )
22
+ assert_equal( 15, d['end'] )
23
+ assert( ! d.push('end', 30) )
24
+ assert( d.unshift('begin', 50) )
25
+ assert_equal( 50, d['begin'] )
26
+ assert( ! d.unshift('begin', 60) )
27
+ assert_equal( ["begin", "a", "c", "z", "end"], d.keys )
28
+ assert_equal( ["end", 15], d.pop )
29
+ assert_equal( ["begin", "a", "c", "z"], d.keys )
30
+ assert_equal( ["begin", 50], d.shift )
31
+ end
32
+
33
+ Unit :insert do
34
+ # front
35
+ d = Dictionary['a', 1, 'b', 2, 'c', 3]
36
+ r = Dictionary['d', 4, 'a', 1, 'b', 2, 'c', 3]
37
+ assert_equal( 4, d.insert(0,'d',4) )
38
+ assert_equal( r, d )
39
+ # back
40
+ d = Dictionary['a', 1, 'b', 2, 'c', 3]
41
+ r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
42
+ assert_equal( 4, d.insert(-1,'d',4) )
43
+ assert_equal( r, d )
44
+ end
45
+
46
+ Unit :update do
47
+ # with other orderred hash
48
+ d = Dictionary['a', 1, 'b', 2, 'c', 3]
49
+ c = Dictionary['d', 4]
50
+ r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
51
+ assert_equal( r, d.update(c) )
52
+ assert_equal( r, d )
53
+ # with other hash
54
+ d = Dictionary['a', 1, 'b', 2, 'c', 3]
55
+ c = { 'd' => 4 }
56
+ r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
57
+ assert_equal( r, d.update(c) )
58
+ assert_equal( r, d )
59
+ end
60
+
61
+ Unit :merge do
62
+ # with other orderred hash
63
+ d = Dictionary['a', 1, 'b', 2, 'c', 3]
64
+ c = Dictionary['d', 4]
65
+ r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
66
+ assert_equal( r, d.merge(c) )
67
+ # with other hash
68
+ d = Dictionary['a', 1, 'b', 2, 'c', 3]
69
+ c = { 'd' => 4 }
70
+ r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
71
+ assert_equal( r, d.merge(c) )
72
+ end
73
+
74
+ Unit :order_by do
75
+ d = Dictionary['a', 3, 'b', 2, 'c', 1]
76
+ d.order_by{ |k,v| v }
77
+ assert_equal( [1,2,3], d.values )
78
+ assert_equal( ['c','b','a'], d.keys )
79
+ end
80
+
81
+ Unit :[]= do
82
+ d = Dictionary[]
83
+ d[:a] = 1
84
+ d[:c] = 3
85
+ assert_equal( [1,3], d.values )
86
+ d[:b,1] = 2
87
+ assert_equal( [1,2,3], d.values )
88
+ assert_equal( [:a,:b,:c], d.keys )
89
+ end
90
+
91
+ Unit :reverse! do
92
+ d = Dictionary['z', 1, 'a', 2, 'c', 3]
93
+ d.reverse!
94
+ assert_equal( ['c','a','z'], d.keys )
95
+ end
96
+
97
+ Unit :enumerable do
98
+ d = Dictionary[]
99
+ d[:a] = "a"
100
+ d[:c] = "b"
101
+ assert_equal( ["A","B"], d.collect{|k,v| v.capitalize} )
102
+ end
103
+
104
+ Unit :autohash do
105
+ d = Dictionary.new{ |hash,key| hash[key] = 0 }
106
+ d[:a] = 0
107
+ d[:b] += 1
108
+ assert_equal([0, 1], d.values)
109
+ assert_equal([:a,:b], d.keys)
110
+ end
111
+
112
+ Unit :dup => "with array values" do
113
+ d = Dictionary.new
114
+ d.dup
115
+ d[:a]=['t',5]
116
+ assert_equal(d, d.dup)
117
+ end
118
+
119
+ Unit :first do
120
+ d = Dictionary[]
121
+ d[:a] = "a"
122
+ d[:b] = "b"
123
+ d[:c] = "c"
124
+ assert_equal( "a" , d.first )
125
+ assert_equal( [] , d.first(0) )
126
+ assert_equal( ["a"] , d.first(1) )
127
+ assert_equal( ["a", "b"] , d.first(2) )
128
+ end
129
+
130
+ Unit :last do
131
+ d = Dictionary[]
132
+ d[:a] = "a"
133
+ d[:b] = "b"
134
+ d[:c] = "c"
135
+ assert_equal( "c" , d.last )
136
+ assert_equal( [] , d.last(0) )
137
+ assert_equal( ["c"] , d.last(1) )
138
+ assert_equal( ["b", "c"] , d.last(2) )
139
+ end
140
+
141
+ end
142
+
@@ -0,0 +1,68 @@
1
+ require 'hashery/opencascade'
2
+ require 'ae/legacy' # bacause imitation BasicObject sucks
3
+
4
+ TestCase OpenCascade do
5
+
6
+ Unit :new do
7
+ o = OpenCascade.new(:a=>1)
8
+ assert_equal(OpenCascade.new, o.assert)
9
+ end
10
+
11
+ Unit :[] => "initialization" do
12
+ o = OpenCascade[:a=>1,:b=>2]
13
+ assert_equal(1, o.a)
14
+ assert_equal(2, o.b)
15
+ end
16
+
17
+ Unit :[] => "mutlt-depth lookup" do
18
+ o = OpenCascade[:a=>1,:b=>2,:c=>{:x=>9}]
19
+ assert_equal(9, o.c.x)
20
+ end
21
+
22
+ Unit :[] => "basic assignment" do
23
+ o = OpenCascade.new
24
+ o[:a] = 1
25
+ assert_equal(1, o.a)
26
+ end
27
+
28
+ Unit :[] => "basic assignment with primed OpenCascade" do
29
+ o = OpenCascade[:a=>1]
30
+ o[:b] = 2
31
+ assert_equal({:a=>1,:b=>2}, o.to_h)
32
+ end
33
+
34
+ Unit :to_a do
35
+ o = OpenCascade[:a=>1,:b=>2]
36
+ assert_equal([[:a,1], [:b,2]], o.to_a)
37
+ end
38
+
39
+ Unit :to_h do
40
+ o = OpenCascade[:a=>1]
41
+ assert_equal({:a=>1}, o.to_h)
42
+ end
43
+
44
+ Unit :merge! do
45
+ o = OpenCascade[:f0=>"f0"]
46
+ h = { :h0=>"h0" }
47
+ r = OpenCascade[:f0=>"f0", :h0=>"h0"]
48
+ assert_equal(r, o.merge!(h))
49
+ assert_equal({:f0=>"f0", :h0=>"h0"}, h.merge(o))
50
+ end
51
+
52
+ Unit :update! do
53
+ o = OpenCascade[:f1=>"f1"]
54
+ h = { :h1=>"h1" }
55
+ o.update!(h)
56
+ h.update(o)
57
+ assert_equal(OpenCascade[:f1=>"f1", :h1=>"h1"], o)
58
+ assert_equal({:f1=>"f1", :h1=>"h1"}, h)
59
+ end
60
+
61
+ Unit :method_missing => "writer and reader" do
62
+ o = OpenCascade.new
63
+ 10.times{ |i| o.__send__("n#{i}=", 1 ) }
64
+ 10.times{ |i| assert_equal(1, o.__send__("n#{i}")) }
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,18 @@
1
+ require 'hashery/openhash'
2
+
3
+ TestCase OpenHash do
4
+
5
+ Unit :new do
6
+ o = OpenHash.new(:a=>1, :b=>2)
7
+ o.a.assert == 1
8
+ o.b.assert == 2
9
+ end
10
+
11
+ Unit :new do
12
+ o = OpenHash.new(:a=>1, :b=>2)
13
+ o.a.assert == 1
14
+ o.b.assert == 2
15
+ end
16
+
17
+ end
18
+
@@ -0,0 +1,130 @@
1
+ require 'hashery/openobject'
2
+
3
+ TestCase OpenObject do
4
+
5
+ Unit :respond_to? do
6
+ o = OpenObject.new
7
+ assert{ o.respond_to?(:key?) }
8
+ end
9
+
10
+ Unit :is_a? do
11
+ assert OpenObject[{}].is_a?(Hash)
12
+ assert OpenObject[{}].is_a?(OpenObject)
13
+ end
14
+
15
+ Unit :[] => "subhash access" do
16
+ o = OpenObject[:a=>1,:b=>{:x=>9}]
17
+ assert{ o[:b][:x] == 9 }
18
+ assert{ o.b[:x] == 9 }
19
+ end
20
+
21
+ Unit :[] => "indifferent key access" do
22
+ o = OpenObject["a"=>1,"b"=>{:x=>9}]
23
+ assert{ o["a"] == 1 }
24
+ assert{ o[:a] == 1 }
25
+ assert{ o["b"] == {:x=>9} }
26
+ assert{ o[:b] == {:x=>9} }
27
+ assert{ o["b"][:x] == 9 }
28
+ assert{ o[:b]["x"] == nil }
29
+ end
30
+
31
+ Unit :[]= => "setting first entry" do
32
+ f0 = OpenObject.new
33
+ f0[:a] = 1
34
+ assert{ f0.to_h == {:a=>1} }
35
+ end
36
+
37
+ Unit :[]= => "setting an additional entry" do
38
+ f0 = OpenObject[:a=>1]
39
+ f0[:b] = 2
40
+ assert{ f0.to_h == {:a=>1,:b=>2} }
41
+ end
42
+
43
+ Unit :method_missing => "reading entries" do
44
+ f0 = OpenObject[:class=>1]
45
+ assert{ f0.class == 1 }
46
+ end
47
+
48
+ Unit :method_missing => "setting entries" do
49
+ fo = OpenObject.new
50
+ 9.times{ |i| fo.__send__("n#{i}=", 1) }
51
+ 9.times{ |i|
52
+ assert( fo.__send__("n#{i}") == 1 )
53
+ }
54
+ end
55
+
56
+ Unit :method_missing => "using bang" do
57
+ o = OpenObject.new
58
+ o.a = 10
59
+ o.b = 20
60
+ h = {}
61
+ o.each!{ |k,v| h[k] = v + 10 }
62
+ assert( h == {:a=>20, :b=>30} )
63
+ end
64
+
65
+ #Unit :as_hash do
66
+ # f0 = OpenObject[:f0=>"f0"]
67
+ # h0 = { :h0=>"h0" }
68
+ # assert{ OpenObject[:f0=>"f0", :h0=>"h0"] == f0.as_hash.merge(h0) }
69
+ # assert{ {:f0=>"f0", :h0=>"h0"} == h0.merge(f0) }
70
+ #end
71
+
72
+ Unit :as_hash do
73
+ f1 = OpenObject[:f1=>"f1"]
74
+ h1 = { :h1=>"h1" }
75
+ f1.as_hash.update(h1)
76
+ h1.update(f1)
77
+ assert{ f1 == OpenObject[:f1=>"f1", :h1=>"h1"] }
78
+ assert{ h1 == {:f1=>"f1", :h1=>"h1"} }
79
+ end
80
+
81
+ Unit :<< => "passing a hash" do
82
+ fo = OpenObject.new
83
+ fo << {:a=>1,:b=>2}
84
+ assert{ fo.to_h == {:a=>1, :b=>2} }
85
+ end
86
+
87
+ Unit :<< => "passing a pair" do
88
+ fo = OpenObject.new
89
+ fo << [:a, 1]
90
+ fo << [:b, 2]
91
+ assert( fo.to_h == {:a=>1, :b=>2} )
92
+ end
93
+
94
+ Unit :to_h do
95
+ ho = {}
96
+ fo = OpenObject.new
97
+ 5.times{ |i| ho["n#{i}".to_sym] = 1 }
98
+ 5.times{ |i| fo.__send__("n#{i}=", 1) }
99
+ assert{ fo.to_h == ho }
100
+ end
101
+
102
+ Unit :to_h => "OpenObject within OpenObject" do
103
+ o = OpenObject.new
104
+ o.a = 10
105
+ o.b = 20
106
+ o.x = OpenObject.new
107
+ o.x.a = 100
108
+ o.x.b = 200
109
+ o.x.c = 300
110
+ assert{ o.to_h == {:a=>10, :b=>20, :x=>{:a=>100, :b=>200, :c=>300}} }
111
+ end
112
+
113
+ Unit :to_proc do
114
+ p = lambda { |x| x.word = "Hello" }
115
+ o = OpenObject[:a=>1,:b=>2]
116
+ assert{ Proc === o.to_proc }
117
+ end
118
+
119
+ end
120
+
121
+ TestCase Proc do
122
+
123
+ Unit :to_openobject do
124
+ p = lambda { |x| x.word = "Hello" }
125
+ o = p.to_openobject
126
+ assert{ o.word == "Hello" }
127
+ end
128
+
129
+ end
130
+
@@ -0,0 +1,131 @@
1
+ require 'hashery/stash'
2
+
3
+ TestCase Stash do
4
+
5
+ Unit :[] => 'class level fetch' do
6
+ s = Stash[:a=>1, :b=>2]
7
+ assert(s)
8
+ end
9
+
10
+ Unit :[] => 'instance level fetch' do
11
+ s = Stash[:a=>1, :b=>2]
12
+ s[:a].assert == 1
13
+ s[:b].assert == 2
14
+ s['a'].assert == 1
15
+ s['b'].assert == 2
16
+ end
17
+
18
+ Unit :[]= => '' do
19
+ s = Stash.new
20
+ s[:a] = 1
21
+ s[:b] = 2
22
+ s[:a].assert == 1
23
+ s[:b].assert == 2
24
+ s['a'].assert == 1
25
+ s['b'].assert == 2
26
+ end
27
+
28
+ Unit :initialize do
29
+ s = Stash.new
30
+ assert(s)
31
+ end
32
+
33
+ Unit :to_hash do
34
+ s = Stash[:a=>1, :b=>2]
35
+ s.to_hash.assert == {'a'=>1, 'b'=>2}
36
+ end
37
+
38
+ Unit :to_h do
39
+ s = Stash[:a=>1, :b=>2]
40
+ s.to_h.assert == {'a'=>1, 'b'=>2}
41
+ end
42
+
43
+ Unit :replace do
44
+ s = Stash.new
45
+ s.replace(:a=>1, :b=>2)
46
+ s.to_h.assert == {'a'=>1, 'b'=>2}
47
+ end
48
+
49
+ Unit :delete do
50
+ s = Stash[:a=>1, :b=>2]
51
+ s.delete(:a)
52
+ s.to_h.assert == {'b'=>2}
53
+ end
54
+
55
+ Unit :each do
56
+ s = Stash[:a=>1, :b=>2]
57
+ s.each do |k,v|
58
+ String.assert === k
59
+ end
60
+ end
61
+
62
+ Unit :store => '' do
63
+ s = Stash.new
64
+ s.store(:a, 1)
65
+ s.to_h.assert == {'a'=>1}
66
+ end
67
+
68
+ Unit :update => '' do
69
+ s1 = Stash[:a=>1,:b=>2]
70
+ s2 = Stash[:c=>3,:d=>4]
71
+ s1.update(s2)
72
+ s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
73
+ end
74
+
75
+ Unit :rekey => '' do
76
+ s = Stash[:a=>1,:b=>2,:c=>3]
77
+ x = s.rekey{ |k| k.upcase }
78
+ x.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
79
+ end
80
+
81
+ Unit :rekey! => '' do
82
+ s = Stash[:a=>1,:b=>2,:c=>3]
83
+ s.rekey!{ |k| k.upcase }
84
+ s.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
85
+ end
86
+
87
+ Unit :key? => '' do
88
+ s = Stash[:a=>1]
89
+ s.assert.key?(:a)
90
+ s.assert.key?('a')
91
+ end
92
+
93
+ Unit :has_key? => '' do
94
+ s = Stash[:a=>1]
95
+ s.assert.has_key?(:a)
96
+ s.assert.has_key?('a')
97
+ end
98
+
99
+ Unit :<< => '' do
100
+ s = Stash.new
101
+ s << [:a, 1]
102
+ s << [:b, 2]
103
+ s.to_h.assert == {'a'=>1, 'b'=>2}
104
+ end
105
+
106
+ Unit :merge! => '' do
107
+ s1 = Stash[:a=>1,:b=>2]
108
+ s2 = Stash[:c=>3,:d=>4]
109
+ s1.merge!(s2)
110
+ s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
111
+ end
112
+
113
+ Unit :values_at => '' do
114
+ s = Stash[:a=>1,:b=>2,:c=>3]
115
+ s.values_at(:a, :b).assert == [1,2]
116
+ s.values_at('a','b').assert == [1,2]
117
+ end
118
+
119
+ Unit :fetch => '' do
120
+ s = Stash[:a=>1,:b=>2,:c=>3]
121
+ s.fetch(:a).assert == 1
122
+ s.fetch('a').assert == 1
123
+ end
124
+
125
+ Unit :convert_key => '' do
126
+ s = Stash.new
127
+ s.pry.convert_key(:a).assert == 'a'
128
+ end
129
+
130
+ end
131
+