goz 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/goz/cache.rb DELETED
@@ -1,136 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'goz/cache/hash_store'
4
-
5
- module Goz # :nodoc:
6
-
7
- #
8
- # = Goz::Cache - Generic cache
9
- #
10
- # == Usage
11
- #
12
- # require 'goz/cache'
13
- #
14
- # cache = Goz::Cache.new do |cache|
15
- # # block initialization with default cache
16
- # end
17
- #
18
- # cache = Goz::Cache.new(ActiveSupport::Cache::MemoryStore.new) do |cache|
19
- # # block initialization with custom cache
20
- # end
21
- #
22
- # # initialization with default cache
23
- # cache = Goz::Cache.new
24
- #
25
- # # initialization with custom cache
26
- # cache = Goz::Cache.new(ActiveSupport::Cache::MemoryStore.new)
27
- #
28
- # # Write to cache
29
- # cache.write(key, value)
30
- #
31
- # # Read from cache
32
- # cache.read(key)
33
- #
34
- # # Delete from cache if key exists.
35
- # cache.delete(key) if cache.exist?(key)
36
- #
37
- # # Clear the cache
38
- # cache.clear
39
- #
40
- # == Author
41
- #
42
- # blair christensen. <mailto:blair.christensen@gmail.com>
43
- #
44
- # == Homepage
45
- #
46
- # https://github.com/blairc/goz/
47
- #
48
- class Cache
49
-
50
- #
51
- # Default cache implementation to use.
52
- #
53
- DEFAULT_CACHE = Goz::Cache::HashStore.new
54
-
55
-
56
- @@caches = {}
57
-
58
-
59
- #
60
- # Create (if necessary) and return Goz::Cache instance.
61
- #
62
- # Params:
63
- # +cache+:: (optional) Any +ActiveSupport::Cache::Store+ compatible class. Defaults to +DEFAULT_CACHE+.
64
- #
65
- def self.instance( cache = DEFAULT_CACHE )
66
- unless @@caches.key?(cache)
67
- @@caches[cache] = self.new(cache)
68
- end
69
- yield @@caches[cache] if block_given?
70
- @@caches[cache]
71
- end
72
-
73
- #
74
- # Initialize Goz::Cache
75
- #
76
- # Params:
77
- # +cache+:: (optional) Any +ActiveSupport::Cache::Store+ compatible class. Defaults to +DEFAULT_CACHE+.
78
- #
79
- def initialize( cache = DEFAULT_CACHE )
80
- @cache = cache
81
- yield self if block_given?
82
- self
83
- end
84
-
85
- #
86
- # Empty cache. Returns +true+ if successful.
87
- #
88
- def clear
89
- @cache.clear
90
- end
91
-
92
- #
93
- # Return true if +key+ deleted from cache.
94
- #
95
- # Params:
96
- # +key+:: Name of key to delete.
97
- #
98
- def delete(key)
99
- @cache.delete(key)
100
- end
101
-
102
- #
103
- # Return true if +key+ exists in cache.
104
- #
105
- # Params:
106
- # +key+:: Name of key
107
- #
108
- def exist?(key)
109
- @cache.exist?(key)
110
- end
111
-
112
- #
113
- # Return value from cache or +nil+.
114
- #
115
- # Params:
116
- # +key+:: Get value for this key.
117
- #
118
- def read(key)
119
- @cache.read(key)
120
- end
121
-
122
- #
123
- # Add +value+ to cache with +key+. Returns value added to cache.
124
- #
125
- # Params:
126
- # +key+:: Associate value with this this key.
127
- # +value+:: Value to add to cache.
128
- #
129
- def write(key, value)
130
- @cache.write(key, value) ? value : nil
131
- end
132
-
133
- end # class Cache
134
-
135
- end # module Goz
136
-
@@ -1,75 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Goz # :nodoc:
4
- class Cache # :nodoc:
5
-
6
- #
7
- # = Goz::Cache::HashStore - Hash-based cache implementation.
8
- #
9
- # == See Also
10
- #
11
- # Goz::Cache, ActiveSupport::Cache::Store
12
- #
13
- # == Author
14
- #
15
- # blair christensen. <mailto:blair.christensen@gmail.com>
16
- #
17
- # == Homepage
18
- #
19
- # https://github.com/blairc/goz/
20
- #
21
- class HashStore < Hash
22
-
23
- #
24
- # Empty cache. Returns +true+ if successful.
25
- #
26
- def clear
27
- super ? true : false
28
- end
29
-
30
- #
31
- # Return true if +key+ deleted from cache.
32
- #
33
- # Params:
34
- # +key+:: Name of key to delete.
35
- #
36
- def delete(key)
37
- super ? true : false
38
- end
39
-
40
- #
41
- # Return true if +key+ exists in cache.
42
- #
43
- # Params:
44
- # +key+:: Name of key
45
- #
46
- def exist?(key)
47
- key?(key)
48
- end
49
-
50
- #
51
- # Return value from cache or +nil+.
52
- #
53
- # Params:
54
- # +key+:: Get value for this key.
55
- #
56
- def read(key)
57
- self[key]
58
- end
59
-
60
- #
61
- # Add +value+ to cache with +key+. Returns value added to cache.
62
- #
63
- # Params:
64
- # +key+:: Associate value with this this key.
65
- # +value+:: Value to add to cache.
66
- #
67
- def write(key, value)
68
- self[key] = value
69
- end
70
-
71
- end # class HashStore
72
-
73
- end # class Cache
74
- end # module Goz
75
-
data/test/test_admins.rb DELETED
@@ -1,189 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'simplecov'
4
- SimpleCov.start
5
-
6
- require 'goz/group'
7
- require 'goz/test_case'
8
- require 'goz/user'
9
-
10
-
11
- class TestAdmins < Goz::TestCase
12
-
13
- def setup
14
- super
15
-
16
- @u1 = Goz::User.create( :email => 'u1', :login => 'u1', :identifier => 'u1', :name => 'u1' )
17
- @u2 = Goz::User.create( :email => 'u2', :login => 'u2', :identifier => 'u2', :name => 'u2' )
18
- @g1 = Goz::Group.create( :display_name => 'g1', :identifier => 'g1', :name => 'g1' )
19
- @g2 = Goz::Group.create( :display_name => 'g2', :identifier => 'g2', :name => 'g2' )
20
- end
21
-
22
-
23
- def test_no_admins_or_groups
24
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
25
- [ @u1, @u2 ].each do |u|
26
- assert_not_nil u.groups
27
- assert_kind_of Array, u.groups
28
- assert_equal 0, u.groups.size
29
- end
30
- [ @g1, @g2 ].each do |g|
31
- assert_not_nil g.admins
32
- assert_kind_of Array, g.admins
33
- assert_equal 0, g.admins.size
34
- end
35
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
36
- end
37
-
38
- def test_add_admin_to_group
39
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
40
- @g1.add_admin(@u1)
41
- @g1.save
42
- assert_equal 1, @g1.admins.size
43
- assert_equal 0, @g2.admins.size
44
- assert_equal @u1, @g1.admins.first
45
- assert_nil @g2.admins.first
46
- assert_equal 1, @u1.groups.size
47
- assert_equal 0, @u2.groups.size
48
- assert_equal @g1, @u1.groups.first
49
- assert_nil @u2.groups.first
50
- Goz::Database.instance do |db|
51
- assert_equal 1, db[:admins].count
52
- first = db[:admins].first
53
- assert_equal @g1.id, first[:group_id]
54
- assert_equal @u1.id, first[:user_id]
55
- end
56
- end
57
-
58
- def test_add_group_to_admin
59
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
60
- @u1.add_group(@g1)
61
- @u1.save
62
- assert_equal 1, @g1.admins.size
63
- assert_equal 0, @g2.admins.size
64
- assert_equal @u1, @g1.admins.first
65
- assert_nil @g2.admins.first
66
- assert_equal 1, @u1.groups.size
67
- assert_equal 0, @u2.groups.size
68
- assert_equal @g1, @u1.groups.first
69
- assert_nil @u2.groups.first
70
- Goz::Database.instance do |db|
71
- assert_equal 1, db[:admins].count
72
- first = db[:admins].first
73
- assert_equal @g1.id, first[:group_id]
74
- assert_equal @u1.id, first[:user_id]
75
- end
76
- end
77
-
78
- def test_remove_admin_from_group
79
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
80
- @g1.add_admin(@u1)
81
- @g1.add_admin(@u2)
82
- @g1.save
83
- assert_equal 2, @g1.admins.size
84
- assert_equal [ @u1, @u2 ], @g1.admins
85
-
86
- @g1.remove_admin(@u2)
87
- @g1.save
88
- assert_equal 1, @g1.admins.size
89
- assert_equal [ @u1 ], @g1.admins
90
-
91
- Goz::Database.instance do |db|
92
- assert_equal 1, db[:admins].count
93
- first = db[:admins].first
94
- assert_equal @g1.id, first[:group_id]
95
- assert_equal @u1.id, first[:user_id]
96
- end
97
- end
98
-
99
- def test_remove_group_from_admin
100
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
101
- @u1.add_group(@g1)
102
- @u1.add_group(@g2)
103
- @u1.save
104
- assert_equal 2, @u1.groups.size
105
- assert_equal [ @g1, @g2 ], @u1.groups
106
-
107
- @u1.remove_group(@g2)
108
- @u1.save
109
- assert_equal 1, @u1.groups.size
110
- assert_equal [ @g1 ], @u1.groups
111
-
112
- Goz::Database.instance do |db|
113
- assert_equal 1, db[:admins].count
114
- first = db[:admins].first
115
- assert_equal @g1.id, first[:group_id]
116
- assert_equal @u1.id, first[:user_id]
117
- end
118
- end
119
-
120
- def test_add_duplicate_admin_to_group
121
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
122
- @g1.add_admin(@u1)
123
- @g1.save
124
- assert_equal 1, @g1.admins.size
125
- assert_equal [ @u1 ], @g1.admins
126
-
127
- assert_raise(Sequel::DatabaseError) { @g1.add_admin(@u1) }
128
- assert_equal 1, @g1.admins.size
129
- assert_equal [ @u1 ], @g1.admins
130
-
131
- Goz::Database.instance do |db|
132
- assert_equal 1, db[:admins].count
133
- first = db[:admins].first
134
- assert_equal @g1.id, first[:group_id]
135
- assert_equal @u1.id, first[:user_id]
136
- end
137
- end
138
-
139
- def test_add_duplicate_group_to_admin
140
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
141
- @u1.add_group(@g1)
142
- @u1.save
143
- assert_equal 1, @u1.groups.size
144
- assert_equal [ @g1 ], @u1.groups
145
-
146
- assert_raise(Sequel::DatabaseError) { @u1.add_group(@g1) }
147
- assert_equal 1, @u1.groups.size
148
- assert_equal [ @g1 ], @u1.groups
149
-
150
- Goz::Database.instance do |db|
151
- assert_equal 1, db[:admins].count
152
- first = db[:admins].first
153
- assert_equal @g1.id, first[:group_id]
154
- assert_equal @u1.id, first[:user_id]
155
- end
156
- end
157
-
158
- def test_delete_group
159
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
160
- @g1.add_admin(@u1)
161
- @g1.save
162
- assert_equal 1, @g1.admins.size
163
- assert_equal [ @u1 ], @g1.admins
164
-
165
- @g1.destroy
166
- assert_nil Goz::Group.find_by_name( @g1.name )
167
-
168
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
169
-
170
- assert_equal [], @u1.groups
171
- end
172
-
173
- def test_delete_user
174
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
175
- @u1.add_group(@g1)
176
- @u1.save
177
- assert_equal 1, @u1.groups.size
178
- assert_equal [ @g1 ], @u1.groups
179
-
180
- @u1.destroy
181
- assert_nil Goz::User.find_by_login( @u1.login )
182
-
183
- Goz::Database.instance { |db| assert_equal 0, db[:admins].count }
184
-
185
- assert_equal [], @g1.admins
186
- end
187
-
188
- end
189
-
data/test/test_cache.rb DELETED
@@ -1,90 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'simplecov'
4
- SimpleCov.start
5
-
6
- require 'goz/cache'
7
- require 'goz/test_case'
8
-
9
-
10
- class TestCache < Goz::TestCase
11
-
12
- def setup
13
- super
14
- @cache = Goz::Cache.new
15
- end
16
-
17
-
18
- def test_default_initialization
19
- blockable = false
20
- cache = Goz::Cache.new do |cache|
21
- assert_kind_of Goz::Cache, cache
22
- blockable = true
23
- end
24
- assert blockable, 'works as block'
25
- assert_kind_of Goz::Cache, cache
26
- assert_kind_of Goz::Cache, Goz::Cache.new
27
- end
28
-
29
- def test_initialization
30
- blockable = false
31
- cache = Goz::Cache.new(@cache) do |cache|
32
- assert_kind_of Goz::Cache, cache
33
- blockable = true
34
- end
35
- assert blockable, 'works as block'
36
- assert_kind_of Goz::Cache, cache
37
- assert_kind_of Goz::Cache, Goz::Cache.new(@cache)
38
- end
39
-
40
- def test_default_instance
41
- blockable = false
42
- cache = Goz::Cache.instance do |cache|
43
- assert_kind_of Goz::Cache, cache
44
- blockable = true
45
- end
46
- assert blockable, 'works as block'
47
- assert_kind_of Goz::Cache, cache
48
- assert_kind_of Goz::Cache, Goz::Cache.instance
49
- assert_equal cache, Goz::Cache.instance
50
- assert_not_equal cache, Goz::Cache.instance(Array.new)
51
- end
52
-
53
- def test_instance
54
- blockable = false
55
- cache = Goz::Cache.instance(@cache) do |cache|
56
- assert_kind_of Goz::Cache, cache
57
- blockable = true
58
- end
59
- assert blockable, 'works as block'
60
- assert_kind_of Goz::Cache, cache
61
- assert_kind_of Goz::Cache, Goz::Cache.instance(@cache)
62
- assert_equal cache, Goz::Cache.instance(@cache)
63
- assert_not_equal cache, Goz::Cache.instance(Array.new)
64
- end
65
-
66
- def test_default_cache
67
- assert_kind_of Goz::Cache::HashStore, Goz::Cache::DEFAULT_CACHE
68
- end
69
-
70
- def test_caching
71
- key, value = :foo, :bar
72
-
73
- assert_equal false, @cache.exist?(key)
74
- assert_equal false, @cache.delete(key)
75
-
76
- assert_equal value, @cache.write(key, value)
77
- assert_equal value, @cache.read(key)
78
- assert_equal true, @cache.exist?(key)
79
- assert_equal true, @cache.delete(key)
80
- assert_equal false, @cache.exist?(key)
81
- assert_equal nil, @cache.read(key)
82
-
83
- assert_equal value, @cache.write(key, value)
84
- assert_equal true, @cache.exist?(key)
85
- assert_equal true, @cache.clear
86
- assert_equal false, @cache.exist?(key)
87
- end
88
-
89
- end
90
-