active_redis_orm 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/active_redis_orm.gemspec +6 -1
- data/lib/active_redis/base_extensions.rb +13 -0
- data/lib/active_redis/version.rb +2 -2
- data/lib/active_redis_orm.rb +5 -0
- data/spec/lib/active_redis/validation_spec.rb +42 -0
- data/spec/lib/active_redis_spec.rb +309 -0
- metadata +77 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adaa34fbf9013daff3c9f27b6100337ca8e38feb
|
4
|
+
data.tar.gz: bd45e099bdfb924174f2f70f118655c828009d9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 229aca3fa5864cdf82b40fdd2ed1d544ea95146ccdd465fb3c2b13dead651965bab35690612f5a53127dc6a76b1dce7efd30cb28e73611f4ddd794e0e2cdd7f4
|
7
|
+
data.tar.gz: 2ad20a7d9ef6114328530a0ea0cfeb166d952c614b0e224ad336598d65ef1a187fca789db877bdd1d1992ae87a4b405e44dd835af2793626e77b50790bd5be5e
|
data/active_redis_orm.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'active_redis/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "active_redis_orm"
|
8
|
-
spec.version =
|
8
|
+
spec.version = ActiveRedis::VERSION
|
9
9
|
spec.authors = ["Tom Caspy"]
|
10
10
|
spec.email = ["tom@tikalk.com"]
|
11
11
|
spec.summary = %q{ActiveRedis is an ORM for redis written in Ruby.}
|
@@ -20,4 +20,9 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.5"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_dependency "redis"
|
25
|
+
spec.add_dependency "redis-objects"
|
26
|
+
spec.add_dependency "activemodel"
|
27
|
+
spec.add_dependency "activesupport"
|
23
28
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Object
|
2
|
+
def boolean?
|
3
|
+
self.is_a?(TrueClass) || self.is_a?(FalseClass)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class String
|
8
|
+
def to_bool
|
9
|
+
return true if ['true', '1', 'yes', 'on', 't'].include? self
|
10
|
+
return false if ['false', '0', 'no', 'off', 'f', 'nil', ''].include? self
|
11
|
+
return nil
|
12
|
+
end
|
13
|
+
end
|
data/lib/active_redis/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.0.
|
1
|
+
module ActiveRedis
|
2
|
+
VERSION = "0.0.2"
|
3
3
|
end
|
data/lib/active_redis_orm.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "active_redis_orm"
|
2
|
+
|
3
|
+
class ARValidations < ActiveRedis::Base
|
4
|
+
field :foo
|
5
|
+
field :short
|
6
|
+
|
7
|
+
validates :short, length: { minimum: 5 }
|
8
|
+
validates :foo, presence: true
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "ActiveRedis validations" do
|
13
|
+
it "isn't valid in case foo and short are't present" do
|
14
|
+
foo = ARValidations.new
|
15
|
+
foo.should_not be_valid
|
16
|
+
foo.errors.messages.keys.should =~ [:short, :foo]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "is valid when valid" do
|
20
|
+
foo = ARValidations.new(foo: "bla", short: "I am long enough")
|
21
|
+
foo.should be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
it "does not save if invalid" do
|
25
|
+
foo = ARValidations.new(foo: "bla", short: "abc")
|
26
|
+
foo.should_not be_valid
|
27
|
+
foo.save.should == false
|
28
|
+
foo.should be_new_record
|
29
|
+
end
|
30
|
+
|
31
|
+
it "saves when valid" do
|
32
|
+
foo = ARValidations.new(foo: "bla", short: "I am long enough")
|
33
|
+
foo.save.should == true
|
34
|
+
foo.should_not be_new_record
|
35
|
+
end
|
36
|
+
|
37
|
+
it "saves when invalid, if we tell it" do
|
38
|
+
foo = ARValidations.new(foo: "bla")
|
39
|
+
foo.save(validate: false).should == true
|
40
|
+
foo.should_not be_new_record
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,309 @@
|
|
1
|
+
require "active_redis_orm"
|
2
|
+
|
3
|
+
describe ActiveRedis do
|
4
|
+
class ActiveRedisObject < ActiveRedis::Base
|
5
|
+
attr_accessible :foo, :bar, :list, :set, :sorted_set, :hash, :expired_field, :date, :time
|
6
|
+
|
7
|
+
field :foo, type: :string
|
8
|
+
field :bar, finder_field: true
|
9
|
+
field :list, type: :list
|
10
|
+
field :set, type: :set
|
11
|
+
field :sorted_set, type: :sorted_set
|
12
|
+
field :hash, type: :hash
|
13
|
+
field :expired_field, expires_in: 1
|
14
|
+
field :bool, type: :boolean
|
15
|
+
field :date, type: :date
|
16
|
+
field :time, type: :time
|
17
|
+
|
18
|
+
list :all
|
19
|
+
list :all_with_bar, if: lambda{|object| object.bar.present? }
|
20
|
+
|
21
|
+
after_create :afterz_create
|
22
|
+
after_update :afterz_update
|
23
|
+
after_save :afterz_save
|
24
|
+
|
25
|
+
def afterz_save; end
|
26
|
+
def afterz_update; end
|
27
|
+
def afterz_create; end
|
28
|
+
end
|
29
|
+
|
30
|
+
before do
|
31
|
+
ActiveRedis.redis.flushdb
|
32
|
+
end
|
33
|
+
|
34
|
+
context "basics" do
|
35
|
+
|
36
|
+
it "has foo defined" do
|
37
|
+
ActiveRedisObject.attribute_definitions[:foo].should == {type: :string}
|
38
|
+
end
|
39
|
+
|
40
|
+
it "has a redis_namespace" do
|
41
|
+
ActiveRedisObject.redis_namespace.should == "active_redis_object"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "has the foo getter and setter" do
|
45
|
+
ActiveRedisObject.new.should respond_to(:foo)
|
46
|
+
ActiveRedisObject.new.should respond_to(:foo=)
|
47
|
+
ActiveRedisObject.new.should respond_to(:bar)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "gets saved" do
|
51
|
+
id = "12341543154342"
|
52
|
+
object = ActiveRedisObject.new(id)
|
53
|
+
object.foo = "I am foo"
|
54
|
+
Redis.current.get("#{ActiveRedisObject.redis_namespace}:#{id}:foo").should be_blank
|
55
|
+
object.save
|
56
|
+
Redis.current.get("#{ActiveRedisObject.redis_namespace}:#{id}:foo").should == "I am foo"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "setters" do
|
61
|
+
it "works" do
|
62
|
+
object = ActiveRedisObject.create(bar: "wagamama")
|
63
|
+
object.foo = "bar"
|
64
|
+
object.save
|
65
|
+
object.foo.should == "bar"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "works for boolean" do
|
69
|
+
object = ActiveRedisObject.create(bar: "wagamama")
|
70
|
+
object.bool = false
|
71
|
+
object.bool.should == false
|
72
|
+
object.save
|
73
|
+
object.bool.should == false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "finder fields" do
|
78
|
+
it "can be found by bar" do
|
79
|
+
object = ActiveRedisObject.new
|
80
|
+
object.bar = "barbar"
|
81
|
+
object.save
|
82
|
+
|
83
|
+
ActiveRedisObject.find_by_bar("barbar").should == object
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should remove old reference to the object when finder field is changed" do
|
87
|
+
object = ActiveRedisObject.create(bar: "barbar")
|
88
|
+
object.bar = "baz"
|
89
|
+
object.save
|
90
|
+
ActiveRedisObject.find_by_bar("barbar").should be_blank
|
91
|
+
ActiveRedisObject.find_by_bar("baz").should == object
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "#dirty?" do
|
96
|
+
it "calls #check_for_changes" do
|
97
|
+
object = ActiveRedisObject.new
|
98
|
+
object.should_receive(:check_for_changes)
|
99
|
+
object.dirty?
|
100
|
+
end
|
101
|
+
|
102
|
+
it "is not dirty if not changed" do
|
103
|
+
object = ActiveRedisObject.new
|
104
|
+
object.bar = "barbar"
|
105
|
+
object.save
|
106
|
+
object.should_not be_dirty
|
107
|
+
end
|
108
|
+
|
109
|
+
it "is dirty when changed" do
|
110
|
+
object = ActiveRedisObject.new
|
111
|
+
object.bar = "barbar"
|
112
|
+
object.should be_dirty
|
113
|
+
end
|
114
|
+
|
115
|
+
it "is dirty when changing a custom dirty object" do
|
116
|
+
object = ActiveRedisObject.new
|
117
|
+
object.hash[:foo] = "bar"
|
118
|
+
object.should be_dirty
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "#save" do
|
123
|
+
it "only saves dirty attributes" do
|
124
|
+
object = ActiveRedisObject.new
|
125
|
+
object.bar = "barbar"
|
126
|
+
object.save
|
127
|
+
object.should_receive(:set_foo).and_call_original
|
128
|
+
object.should_not_receive(:set_bar)
|
129
|
+
object.foo = "foo"
|
130
|
+
object.save
|
131
|
+
object.reload!
|
132
|
+
object.foo.should == "foo"
|
133
|
+
object.bar.should == "barbar"
|
134
|
+
end
|
135
|
+
|
136
|
+
it "saves fields like list, set, etc" do
|
137
|
+
object = ActiveRedisObject.new
|
138
|
+
object.foo = "foo"
|
139
|
+
object.list = [1,2,3]
|
140
|
+
object.set = [1,2,3]
|
141
|
+
object.sorted_set[1] = 1
|
142
|
+
object.hash = {foo: :bar}
|
143
|
+
object.save
|
144
|
+
object.reload!
|
145
|
+
object.list.should == ["1","2","3"]
|
146
|
+
object.set.should =~ ["1","2","3"]
|
147
|
+
object.sorted_set.should == ["1"]
|
148
|
+
object.hash.should == {"foo" => "bar"}
|
149
|
+
end
|
150
|
+
|
151
|
+
it "tracks changes well" do
|
152
|
+
object = ActiveRedisObject.new
|
153
|
+
object.hash = {}
|
154
|
+
object.save
|
155
|
+
object.hash[:foo] = "1"
|
156
|
+
object.save
|
157
|
+
object.reload!
|
158
|
+
object.hash.should == {"foo" => "1"}
|
159
|
+
end
|
160
|
+
|
161
|
+
it "does not keep changes if not saved" do
|
162
|
+
object = ActiveRedisObject.new
|
163
|
+
object.list = [1,2,3]
|
164
|
+
object.save
|
165
|
+
object.reload!
|
166
|
+
object.list.push("4")
|
167
|
+
object.list.should == ["1", "2", "3", "4"]
|
168
|
+
object.reload!
|
169
|
+
object.list.should == ["1", "2", "3"]
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context "#new_record?" do
|
174
|
+
it "is a new record when initialized empty" do
|
175
|
+
object = ActiveRedisObject.new
|
176
|
+
object.should be_new_record
|
177
|
+
end
|
178
|
+
|
179
|
+
it "stops being new record when saved" do
|
180
|
+
object = ActiveRedisObject.new
|
181
|
+
object.foo = "foo"
|
182
|
+
object.save
|
183
|
+
object.should_not be_new_record
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
context "callbacks" do
|
188
|
+
it "calls callbacks on their appropriate times" do
|
189
|
+
object = ActiveRedisObject.new(foo: :foo)
|
190
|
+
object.should_receive(:afterz_save).twice
|
191
|
+
object.should_receive :afterz_create
|
192
|
+
object.save
|
193
|
+
object.foo = "bar"
|
194
|
+
object.should_not_receive :afterz_create
|
195
|
+
object.should_receive :afterz_update
|
196
|
+
object.save
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context "#destroy" do
|
201
|
+
it "removes all data" do
|
202
|
+
object = ActiveRedisObject.new(foo: "foo")
|
203
|
+
object.save
|
204
|
+
object.foo_object.get.should == "foo"
|
205
|
+
|
206
|
+
object.destroy
|
207
|
+
object.foo_object.get.should be_nil
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
context "field expiry" do
|
212
|
+
it "gets expired" do
|
213
|
+
object = ActiveRedisObject.create(expired_field: "foo")
|
214
|
+
object.expired_field.should == "foo"
|
215
|
+
Redis.current.ttl(object.expired_field_redis_key).should > 0
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
context "list of items" do
|
220
|
+
it "puts the item ids in a list" do
|
221
|
+
object = ActiveRedisObject.create(expired_field: "foo")
|
222
|
+
ActiveRedisObject.all_ids.members.should == [object.id]
|
223
|
+
end
|
224
|
+
|
225
|
+
it "puts the item ids in a list according to the 'if' option" do
|
226
|
+
object = ActiveRedisObject.create(bar: "foo")
|
227
|
+
non_added = ActiveRedisObject.create(foo: "foo")
|
228
|
+
ActiveRedisObject.all_ids.members.should == [object.id, non_added.id]
|
229
|
+
ActiveRedisObject.all_with_bar_ids.members.should == [object.id]
|
230
|
+
end
|
231
|
+
|
232
|
+
it "removes the item on destroy" do
|
233
|
+
object = ActiveRedisObject.create(bar: "foo")
|
234
|
+
non_added = ActiveRedisObject.create(foo: "foo")
|
235
|
+
object.destroy
|
236
|
+
non_added.destroy
|
237
|
+
ActiveRedisObject.all_ids.members.should == []
|
238
|
+
ActiveRedisObject.all_with_bar_ids.members.should == []
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
context "date and time" do
|
243
|
+
it "saves date" do
|
244
|
+
object = ActiveRedisObject.create(date: Date.today)
|
245
|
+
object.date.should == Date.today
|
246
|
+
object.reload!
|
247
|
+
object.date.should == Date.today
|
248
|
+
object.date=Date.yesterday
|
249
|
+
object.date.should == Date.yesterday
|
250
|
+
object.save
|
251
|
+
object.reload!
|
252
|
+
object.date.should == Date.yesterday
|
253
|
+
end
|
254
|
+
|
255
|
+
it "saves time" do
|
256
|
+
#because we're using timestamps, we need to round the time to the nearest second for the comparison to work
|
257
|
+
time1 = Time.at(Time.now.to_i)
|
258
|
+
time2 = Time.at(10.minutes.ago.to_i)
|
259
|
+
object = ActiveRedisObject.create(time: time1)
|
260
|
+
object.time.should == time1
|
261
|
+
object.reload!
|
262
|
+
object.time.should == time1
|
263
|
+
object.time=time2
|
264
|
+
object.time.should == time2
|
265
|
+
object.save
|
266
|
+
object.reload!
|
267
|
+
object.time.should == time2
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
context "inheritence" do
|
272
|
+
class InheritedActiveRedis < ActiveRedisObject
|
273
|
+
field :inherited_field
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should have access to all its attribute definitions" do
|
277
|
+
InheritedActiveRedis.attribute_options.keys.should include(:inherited_field)
|
278
|
+
InheritedActiveRedis.attribute_options.keys.should include(:foo)
|
279
|
+
ActiveRedisObject.attribute_options.keys.should_not include(:inherited_field)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
context "timestamps" do
|
284
|
+
class TimestampedRedis < ActiveRedis::Base
|
285
|
+
include ActiveRedis::Timestamps
|
286
|
+
|
287
|
+
field :foo
|
288
|
+
end
|
289
|
+
|
290
|
+
it "gets timestamped on creation" do
|
291
|
+
object = TimestampedRedis.create(foo: "foo")
|
292
|
+
(1.second.ago..Time.now).should cover(object.created_at)
|
293
|
+
(1.second.ago..Time.now).should cover(object.updated_at)
|
294
|
+
end
|
295
|
+
|
296
|
+
it "updated the updated_at field on update, does not change the created_at" do
|
297
|
+
created_time = Time.at(12234)
|
298
|
+
updated_at = Time.at(1232356)
|
299
|
+
Time.stub(:now).and_return(created_time)
|
300
|
+
object = TimestampedRedis.create(foo: "foo")
|
301
|
+
object.created_at.should == created_time
|
302
|
+
Time.stub(:now).and_return(updated_at)
|
303
|
+
object.update_attributes(foo: "bar")
|
304
|
+
object.updated_at.should == updated_at
|
305
|
+
object.created_at.should == created_time
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_redis_orm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Caspy
|
@@ -38,6 +38,76 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: redis-objects
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activemodel
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
41
111
|
description: ActiveRedis is a Ruby ORM for Redis, using ActiveModel, heavily influenced
|
42
112
|
by the ActiveRecord and Mongoid gems
|
43
113
|
email:
|
@@ -54,6 +124,7 @@ files:
|
|
54
124
|
- active_redis_orm.gemspec
|
55
125
|
- lib/active_redis/all_list.rb
|
56
126
|
- lib/active_redis/attributes.rb
|
127
|
+
- lib/active_redis/base_extensions.rb
|
57
128
|
- lib/active_redis/dirty_attributes.rb
|
58
129
|
- lib/active_redis/dirty_objects/array.rb
|
59
130
|
- lib/active_redis/dirty_objects/hash.rb
|
@@ -62,6 +133,8 @@ files:
|
|
62
133
|
- lib/active_redis/timestamps.rb
|
63
134
|
- lib/active_redis/version.rb
|
64
135
|
- lib/active_redis_orm.rb
|
136
|
+
- spec/lib/active_redis/validation_spec.rb
|
137
|
+
- spec/lib/active_redis_spec.rb
|
65
138
|
homepage: https://github.com/SpotIM/active_redis_orm
|
66
139
|
licenses:
|
67
140
|
- MIT
|
@@ -86,4 +159,6 @@ rubygems_version: 2.2.2
|
|
86
159
|
signing_key:
|
87
160
|
specification_version: 4
|
88
161
|
summary: ActiveRedis is an ORM for redis written in Ruby.
|
89
|
-
test_files:
|
162
|
+
test_files:
|
163
|
+
- spec/lib/active_redis/validation_spec.rb
|
164
|
+
- spec/lib/active_redis_spec.rb
|