ohm 0.0.14 → 0.0.15

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.
data/lib/ohm.rb CHANGED
@@ -6,7 +6,7 @@ module Ohm
6
6
 
7
7
  # Provides access to the Redis database. This is shared accross all models and instances.
8
8
  def redis
9
- Thread.current[:redis]
9
+ Thread.current[:redis] ||= Ohm::Redis.new(*options)
10
10
  end
11
11
 
12
12
  def redis=(connection)
@@ -23,7 +23,12 @@ module Ohm
23
23
  # @example Connect to a database in port 6380.
24
24
  # Ohm.connect(:port => 6380)
25
25
  def connect(*options)
26
- self.redis = Ohm::Redis.new(*options)
26
+ self.redis = nil
27
+ @options = options
28
+ end
29
+
30
+ def options
31
+ @options
27
32
  end
28
33
 
29
34
  # Clear the database.
@@ -36,7 +41,7 @@ module Ohm
36
41
  args.join(":")
37
42
  end
38
43
 
39
- module_function :key, :connect, :flush, :redis, :redis=
44
+ module_function :key, :connect, :flush, :redis, :redis=, :options
40
45
 
41
46
  module Attributes
42
47
  class Collection
@@ -97,6 +102,11 @@ module Ohm
97
102
  to_ary == other
98
103
  end
99
104
 
105
+ # @return [true, false] Returns whether or not the collection is empty.
106
+ def empty?
107
+ size.zero?
108
+ end
109
+
100
110
  private
101
111
 
102
112
  def instantiate(raw)
@@ -119,18 +129,35 @@ module Ohm
119
129
  # event.participants.all #=> ["Albert", "Benoit"]
120
130
  class List < Collection
121
131
 
122
- # @param value [#to_s] Pushes value to the list.
132
+ # @param value [#to_s] Pushes value to the tail of the list.
123
133
  def << value
124
134
  db.rpush(key, value)
125
135
  end
126
136
 
127
- def empty?
128
- db.llen(key).zero?
137
+ # @return [String] Return and remove the last element of the list.
138
+ def pop
139
+ db.rpop(key)
140
+ end
141
+
142
+ # @return [String] Return and remove the first element of the list.
143
+ def shift
144
+ db.lpop(key)
129
145
  end
130
146
 
147
+ # @param value [#to_s] Pushes value to the head of the list.
148
+ def unshift(value)
149
+ db.lpush(key, value)
150
+ end
151
+
152
+ # @return [Array] Elements of the list.
131
153
  def raw
132
154
  db.list(key)
133
155
  end
156
+
157
+ # @return [Integer] Returns the number of elements in the list.
158
+ def size
159
+ db.llen(key)
160
+ end
134
161
  end
135
162
 
136
163
  # Represents a Redis set.
@@ -165,10 +192,6 @@ module Ohm
165
192
  db.srem(key, value)
166
193
  end
167
194
 
168
- def empty?
169
- db.scard(key).zero?
170
- end
171
-
172
195
  def include?(value)
173
196
  db.sismember(key, value)
174
197
  end
@@ -176,6 +199,11 @@ module Ohm
176
199
  def raw
177
200
  db.smembers(key)
178
201
  end
202
+
203
+ # @return [Integer] Returns the number of elements in the set.
204
+ def size
205
+ db.scard(key)
206
+ end
179
207
  end
180
208
  end
181
209
 
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class ConnectionTest < Test::Unit::TestCase
4
+ setup do
5
+ @options = Ohm.options
6
+ end
7
+
8
+ test "connects lazily" do
9
+ assert_nothing_raised do
10
+ Ohm.connect(:port => 1234567)
11
+ end
12
+
13
+ assert_raises(Errno::ECONNREFUSED) do
14
+ Ohm.redis.get "foo"
15
+ end
16
+ end
17
+
18
+ test "provides a separate connection for each thread" do
19
+ assert Ohm.redis == Ohm.redis
20
+
21
+ conn1, conn2 = nil
22
+
23
+ threads = []
24
+
25
+ threads << Thread.new do
26
+ conn1 = Ohm.redis
27
+ end
28
+
29
+ threads << Thread.new do
30
+ conn2 = Ohm.redis
31
+ end
32
+
33
+ threads.each { |t| t.join }
34
+
35
+ assert (conn1 != conn2)
36
+ end
37
+
38
+ teardown do
39
+ Ohm.connect(*@options)
40
+ end
41
+ end
@@ -255,6 +255,14 @@ class TestRedis < Test::Unit::TestCase
255
255
 
256
256
  assert_equal [@person.id.to_s], @event.attendees.raw
257
257
  end
258
+
259
+ should "return the size of the set" do
260
+ @event.create
261
+ @event.attendees << "1"
262
+ @event.attendees << "2"
263
+ @event.attendees << "3"
264
+ assert_equal 3, @event.attendees.size
265
+ end
258
266
  end
259
267
 
260
268
  context "Attributes of type List" do
@@ -294,6 +302,37 @@ class TestRedis < Test::Unit::TestCase
294
302
  i += 1
295
303
  end
296
304
  end
305
+
306
+ should "return the size of the list" do
307
+ @post.comments << "1"
308
+ @post.comments << "2"
309
+ @post.comments << "3"
310
+ assert_equal 3, @post.comments.size
311
+ end
312
+
313
+ should "return the last element with pop" do
314
+ @post.comments << "1"
315
+ @post.comments << "2"
316
+ assert_equal "2", @post.comments.pop
317
+ assert_equal "1", @post.comments.pop
318
+ assert @post.comments.empty?
319
+ end
320
+
321
+ should "return the first element with shift" do
322
+ @post.comments << "1"
323
+ @post.comments << "2"
324
+ assert_equal "1", @post.comments.shift
325
+ assert_equal "2", @post.comments.shift
326
+ assert @post.comments.empty?
327
+ end
328
+
329
+ should "push to the head of the list with unshift" do
330
+ @post.comments.unshift "1"
331
+ @post.comments.unshift "2"
332
+ assert_equal "1", @post.comments.pop
333
+ assert_equal "2", @post.comments.pop
334
+ assert @post.comments.empty?
335
+ end
297
336
  end
298
337
 
299
338
  context "Applying arbitrary transformations" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-07-28 00:00:00 -03:00
13
+ date: 2009-07-30 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -33,6 +33,7 @@ files:
33
33
  - Rakefile
34
34
  - test/all_tests.rb
35
35
  - test/benchmarks.rb
36
+ - test/connection_test.rb
36
37
  - test/db/dump.rdb
37
38
  - test/db/redis.pid
38
39
  - test/errors_test.rb