ohm 0.0.29 → 0.0.30
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ohm.rb +38 -45
- data/lib/ohm/redis.rb +11 -8
- data/test/model_test.rb +57 -0
- data/test/redis_test.rb +2 -2
- metadata +1 -1
data/lib/ohm.rb
CHANGED
@@ -352,6 +352,16 @@ module Ohm
|
|
352
352
|
end
|
353
353
|
end
|
354
354
|
|
355
|
+
class RedefinitionError < Error
|
356
|
+
def initialize(att)
|
357
|
+
@att = att
|
358
|
+
end
|
359
|
+
|
360
|
+
def message
|
361
|
+
"Cannot redefine #{@att.inspect}"
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
355
365
|
@@attributes = Hash.new { |hash, key| hash[key] = [] }
|
356
366
|
@@collections = Hash.new { |hash, key| hash[key] = [] }
|
357
367
|
@@counters = Hash.new { |hash, key| hash[key] = [] }
|
@@ -368,18 +378,17 @@ module Ohm
|
|
368
378
|
#
|
369
379
|
# @param name [Symbol] Name of the attribute.
|
370
380
|
def self.attribute(name)
|
371
|
-
|
381
|
+
raise RedefinitionError, name if attributes.include?(name)
|
372
382
|
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
define_method(:"#{name}=") do |value|
|
378
|
-
write_local(name, value)
|
379
|
-
end
|
383
|
+
define_method(name) do
|
384
|
+
read_local(name)
|
385
|
+
end
|
380
386
|
|
381
|
-
|
387
|
+
define_method(:"#{name}=") do |value|
|
388
|
+
write_local(name, value)
|
382
389
|
end
|
390
|
+
|
391
|
+
attributes << name
|
383
392
|
end
|
384
393
|
|
385
394
|
# Defines a counter attribute for the model. This attribute can't be assigned, only incremented
|
@@ -387,14 +396,13 @@ module Ohm
|
|
387
396
|
#
|
388
397
|
# @param name [Symbol] Name of the counter.
|
389
398
|
def self.counter(name)
|
390
|
-
|
399
|
+
raise RedefinitionError, name if counters.include?(name)
|
391
400
|
|
392
|
-
|
393
|
-
|
394
|
-
end
|
395
|
-
|
396
|
-
counters << name
|
401
|
+
define_method(name) do
|
402
|
+
read_local(name).to_i
|
397
403
|
end
|
404
|
+
|
405
|
+
counters << name
|
398
406
|
end
|
399
407
|
|
400
408
|
# Defines a list attribute for the model. It can be accessed only after the model instance
|
@@ -402,10 +410,10 @@ module Ohm
|
|
402
410
|
#
|
403
411
|
# @param name [Symbol] Name of the list.
|
404
412
|
def self.list(name, model = nil)
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
413
|
+
raise RedefinitionError, name if collections.include?(name)
|
414
|
+
|
415
|
+
attr_list_reader(name, model)
|
416
|
+
collections << name
|
409
417
|
end
|
410
418
|
|
411
419
|
# Defines a set attribute for the model. It can be accessed only after the model instance
|
@@ -414,10 +422,10 @@ module Ohm
|
|
414
422
|
#
|
415
423
|
# @param name [Symbol] Name of the set.
|
416
424
|
def self.set(name, model = nil)
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
425
|
+
raise RedefinitionError, name if collections.include?(name)
|
426
|
+
|
427
|
+
attr_set_reader(name, model)
|
428
|
+
collections << name
|
421
429
|
end
|
422
430
|
|
423
431
|
# Creates an index (a set) that will be used for finding instances.
|
@@ -436,9 +444,9 @@ module Ohm
|
|
436
444
|
#
|
437
445
|
# @param name [Symbol] Name of the attribute to be indexed.
|
438
446
|
def self.index(att)
|
439
|
-
|
440
|
-
|
441
|
-
|
447
|
+
raise RedefinitionError, att if indices.include?(att)
|
448
|
+
|
449
|
+
indices << att
|
442
450
|
end
|
443
451
|
|
444
452
|
def self.attr_list_reader(name, model = nil)
|
@@ -621,19 +629,11 @@ module Ohm
|
|
621
629
|
self.class.key(id, *args)
|
622
630
|
end
|
623
631
|
|
624
|
-
#
|
625
|
-
# Redis. The idea is to use MSET when possible.
|
632
|
+
# Use MSET if possible, SET otherwise.
|
626
633
|
def write
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
end
|
631
|
-
else
|
632
|
-
def write
|
633
|
-
write_with_mset
|
634
|
-
end
|
635
|
-
end
|
636
|
-
write
|
634
|
+
db.support_mset? ?
|
635
|
+
write_with_mset :
|
636
|
+
write_with_set
|
637
637
|
end
|
638
638
|
|
639
639
|
# Write attributes using SET
|
@@ -659,13 +659,6 @@ module Ohm
|
|
659
659
|
|
660
660
|
private
|
661
661
|
|
662
|
-
# Determine if MSET is available. This method
|
663
|
-
# and the branch at #write will be deprecated
|
664
|
-
# once Redis 1.1 becomes the recommended version.
|
665
|
-
def legacy_redis_version?
|
666
|
-
db.info[:redis_version] <= "1.02"
|
667
|
-
end
|
668
|
-
|
669
662
|
def self.db
|
670
663
|
Ohm.redis
|
671
664
|
end
|
data/lib/ohm/redis.rb
CHANGED
@@ -54,14 +54,7 @@ module Ohm
|
|
54
54
|
PROCESSOR_IDENTITY = lambda { |reply| reply }
|
55
55
|
PROCESSOR_CONVERT_TO_BOOL = lambda { |reply| reply == 0 ? false : reply }
|
56
56
|
PROCESSOR_SPLIT_KEYS = lambda { |reply| reply.split(" ") }
|
57
|
-
PROCESSOR_INFO = lambda
|
58
|
-
info = Hash.new
|
59
|
-
reply.each_line do |line|
|
60
|
-
key, value = line.split(":", 2).map { |part| part.chomp }
|
61
|
-
info[key.to_sym] = value
|
62
|
-
end
|
63
|
-
info
|
64
|
-
end
|
57
|
+
PROCESSOR_INFO = lambda { |reply| Hash[*(reply.lines.map { |l| l.chomp.split(":") }.flatten)] }
|
65
58
|
|
66
59
|
REPLY_PROCESSOR = {
|
67
60
|
:exists => PROCESSOR_CONVERT_TO_BOOL,
|
@@ -93,6 +86,16 @@ module Ohm
|
|
93
86
|
connect
|
94
87
|
end
|
95
88
|
|
89
|
+
def version
|
90
|
+
@version ||= info["redis_version"]
|
91
|
+
end
|
92
|
+
|
93
|
+
def support_mset?
|
94
|
+
@support_mset.nil? ?
|
95
|
+
@support_mset = version >= "1.05" :
|
96
|
+
@support_mset
|
97
|
+
end
|
98
|
+
|
96
99
|
def to_s
|
97
100
|
"Redis Client connected to #{@host}:#{@port} against DB #{@db}"
|
98
101
|
end
|
data/test/model_test.rb
CHANGED
@@ -98,6 +98,63 @@ class TestRedis < Test::Unit::TestCase
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
context "Model definition" do
|
102
|
+
should "raise if an attribute is redefined" do
|
103
|
+
assert_raise Ohm::Model::RedefinitionError do
|
104
|
+
class RedefinedModel < Ohm::Model
|
105
|
+
attribute :name
|
106
|
+
attribute :name
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
should "raise if a counter is redefined" do
|
112
|
+
assert_raise Ohm::Model::RedefinitionError do
|
113
|
+
class RedefinedModel < Ohm::Model
|
114
|
+
counter :age
|
115
|
+
counter :age
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
should "raise if a list is redefined" do
|
121
|
+
assert_raise Ohm::Model::RedefinitionError do
|
122
|
+
class RedefinedModel < Ohm::Model
|
123
|
+
list :todo
|
124
|
+
list :todo
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
should "raise if a set is redefined" do
|
130
|
+
assert_raise Ohm::Model::RedefinitionError do
|
131
|
+
class RedefinedModel < Ohm::Model
|
132
|
+
set :friends
|
133
|
+
set :friends
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
should "raise if a collection is redefined" do
|
139
|
+
assert_raise Ohm::Model::RedefinitionError do
|
140
|
+
class RedefinedModel < Ohm::Model
|
141
|
+
list :toys
|
142
|
+
set :toys
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
should "raise if a index is redefined" do
|
148
|
+
assert_raise Ohm::Model::RedefinitionError do
|
149
|
+
class RedefinedModel < Ohm::Model
|
150
|
+
attribute :color
|
151
|
+
index :color
|
152
|
+
index :color
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
101
158
|
context "Finding an event" do
|
102
159
|
setup do
|
103
160
|
Ohm.redis.sadd("Event:all", 1)
|
data/test/redis_test.rb
CHANGED
@@ -16,7 +16,7 @@ end
|
|
16
16
|
class RedisTest < Test::Unit::TestCase
|
17
17
|
setup do
|
18
18
|
@legacy ||= begin
|
19
|
-
Ohm.redis.info[
|
19
|
+
Ohm.redis.info["redis_version"] <= "1.02"
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -387,7 +387,7 @@ class RedisTest < Test::Unit::TestCase
|
|
387
387
|
end
|
388
388
|
|
389
389
|
should "provide info" do
|
390
|
-
|
390
|
+
%w(last_save_time redis_version total_connections_received connected_clients total_commands_processed connected_slaves uptime_in_seconds used_memory uptime_in_days changes_since_last_save).each do |x|
|
391
391
|
assert @r.info.keys.include?(x)
|
392
392
|
end
|
393
393
|
end
|