remodel 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/remodel.rb +26 -1
- data/remodel.gemspec +2 -2
- data/test/test_entity.rb +45 -15
- data/test/test_many_to_many.rb +24 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/remodel.rb
CHANGED
@@ -109,6 +109,11 @@ module Remodel
|
|
109
109
|
_add_to_reverse_association_of(entity) if @reverse
|
110
110
|
_add(entity)
|
111
111
|
end
|
112
|
+
|
113
|
+
def remove(entity)
|
114
|
+
_remove_from_reverse_association_of(entity) if @reverse
|
115
|
+
_remove(entity)
|
116
|
+
end
|
112
117
|
|
113
118
|
private
|
114
119
|
|
@@ -119,8 +124,9 @@ module Remodel
|
|
119
124
|
end
|
120
125
|
|
121
126
|
def _remove(entity)
|
122
|
-
delete_if { |x| x.key
|
127
|
+
delete_if { |x| x.key == entity.key }
|
123
128
|
Remodel.redis.lrem(@key, 0, entity.key)
|
129
|
+
entity
|
124
130
|
end
|
125
131
|
|
126
132
|
def _add_to_reverse_association_of(entity)
|
@@ -130,6 +136,14 @@ module Remodel
|
|
130
136
|
entity.send("_#{@reverse}=", @this)
|
131
137
|
end
|
132
138
|
end
|
139
|
+
|
140
|
+
def _remove_from_reverse_association_of(entity)
|
141
|
+
if entity.send(@reverse).is_a? HasMany
|
142
|
+
entity.send(@reverse).send(:_remove, @this)
|
143
|
+
else
|
144
|
+
entity.send("_#{@reverse}=", nil)
|
145
|
+
end
|
146
|
+
end
|
133
147
|
|
134
148
|
def _fetch(clazz, key)
|
135
149
|
keys = Remodel.redis.lrange(key, 0, -1)
|
@@ -149,6 +163,7 @@ module Remodel
|
|
149
163
|
def initialize(attributes = {}, key = nil)
|
150
164
|
@attributes = {}
|
151
165
|
@key = key
|
166
|
+
attributes = self.class.default_values.merge(attributes) if key.nil?
|
152
167
|
attributes.each do |name, value|
|
153
168
|
send("#{name}=", value) if respond_to? "#{name}="
|
154
169
|
end
|
@@ -183,6 +198,10 @@ module Remodel
|
|
183
198
|
Remodel.redis.del(@key)
|
184
199
|
end
|
185
200
|
|
201
|
+
def as_json
|
202
|
+
{ :id => id }.merge(@attributes)
|
203
|
+
end
|
204
|
+
|
186
205
|
def to_json
|
187
206
|
JSON.generate(self.class.pack(@attributes))
|
188
207
|
end
|
@@ -225,6 +244,7 @@ module Remodel
|
|
225
244
|
def self.property(name, options = {})
|
226
245
|
name = name.to_sym
|
227
246
|
mapper[name] = Remodel.mapper_for(options[:class])
|
247
|
+
default_values[name] = options[:default] if options.has_key?(:default)
|
228
248
|
define_method(name) { @attributes[name] }
|
229
249
|
define_method("#{name}=") { |value| @attributes[name] = value }
|
230
250
|
end
|
@@ -337,6 +357,11 @@ module Remodel
|
|
337
357
|
def self.mapper
|
338
358
|
@mapper ||= {}
|
339
359
|
end
|
360
|
+
|
361
|
+
def self.default_values
|
362
|
+
@default_values ||= {}
|
363
|
+
end
|
364
|
+
|
340
365
|
end
|
341
366
|
|
342
367
|
end
|
data/remodel.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{remodel}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Tim Lossen"]
|
12
|
-
s.date = %q{2010-04-
|
12
|
+
s.date = %q{2010-04-30}
|
13
13
|
s.default_executable = %q{redis-monitor.rb}
|
14
14
|
s.description = %q{build your domain model in ruby, persist your objects to redis.}
|
15
15
|
s.email = %q{tim@lossen.de}
|
data/test/test_entity.rb
CHANGED
@@ -5,7 +5,9 @@ class Foo < Remodel::Entity
|
|
5
5
|
property :y
|
6
6
|
end
|
7
7
|
|
8
|
-
class Bar < Remodel::Entity
|
8
|
+
class Bar < Remodel::Entity
|
9
|
+
property :d, :default => 123
|
10
|
+
end
|
9
11
|
|
10
12
|
class TestEntity < Test::Unit::TestCase
|
11
13
|
|
@@ -28,7 +30,17 @@ class TestEntity < Test::Unit::TestCase
|
|
28
30
|
|
29
31
|
should "not set the id" do
|
30
32
|
foo = Foo.new :x => 23
|
31
|
-
assert_equal nil, foo.id
|
33
|
+
assert_equal nil, foo.id
|
34
|
+
end
|
35
|
+
|
36
|
+
should "use default values for missing properties" do
|
37
|
+
bar = Bar.new
|
38
|
+
assert_equal 123, bar.d
|
39
|
+
end
|
40
|
+
|
41
|
+
should "not use default values for given properties" do
|
42
|
+
bar = Bar.new :d => 'cool'
|
43
|
+
assert_equal 'cool', bar.d
|
32
44
|
end
|
33
45
|
end
|
34
46
|
|
@@ -69,7 +81,17 @@ class TestEntity < Test::Unit::TestCase
|
|
69
81
|
should "not store the key as a property" do
|
70
82
|
foo = Foo.create :x => 'hello', :y => false
|
71
83
|
assert !(/f:1/ =~ redis.get(foo.key))
|
72
|
-
end
|
84
|
+
end
|
85
|
+
|
86
|
+
should "use default values for missing properties" do
|
87
|
+
bar = Bar.create
|
88
|
+
assert_equal 123, bar.d
|
89
|
+
end
|
90
|
+
|
91
|
+
should "not use default values for given properties" do
|
92
|
+
bar = Bar.create :d => 'cool'
|
93
|
+
assert_equal 'cool', bar.d
|
94
|
+
end
|
73
95
|
end
|
74
96
|
|
75
97
|
context "save" do
|
@@ -167,6 +189,22 @@ class TestEntity < Test::Unit::TestCase
|
|
167
189
|
end
|
168
190
|
end
|
169
191
|
|
192
|
+
context "to_json" do
|
193
|
+
should "serialize to json" do
|
194
|
+
foo = Foo.new :x => 42, :y => true
|
195
|
+
assert_match /"x":42/, foo.to_json
|
196
|
+
assert_match /"y":true/, foo.to_json
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context "as_json" do
|
201
|
+
should "serialize into a hash" do
|
202
|
+
foo = Foo.create :x => 42, :y => true
|
203
|
+
expected = { :id => foo.id, :x => 42, :y => true }
|
204
|
+
assert_equal expected, foo.as_json
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
170
208
|
context "#set_key_prefix" do
|
171
209
|
should "use the given key prefix" do
|
172
210
|
class Custom < Remodel::Entity; set_key_prefix 'my'; end
|
@@ -180,11 +218,11 @@ class TestEntity < Test::Unit::TestCase
|
|
180
218
|
end
|
181
219
|
end
|
182
220
|
|
183
|
-
context "find" do
|
221
|
+
context "#find" do
|
184
222
|
setup do
|
185
223
|
redis.flushdb
|
186
224
|
@foo = Foo.create :x => 'hello', :y => 123
|
187
|
-
|
225
|
+
Foo.create :x => 'hallo', :y => 124
|
188
226
|
end
|
189
227
|
|
190
228
|
should "find and load an entity by key" do
|
@@ -208,7 +246,7 @@ class TestEntity < Test::Unit::TestCase
|
|
208
246
|
end
|
209
247
|
end
|
210
248
|
|
211
|
-
context "all" do
|
249
|
+
context "#all" do
|
212
250
|
setup do
|
213
251
|
redis.flushdb
|
214
252
|
17.times { |i| Foo.create :x => 'hello', :y => i }
|
@@ -275,15 +313,7 @@ class TestEntity < Test::Unit::TestCase
|
|
275
313
|
end
|
276
314
|
end
|
277
315
|
|
278
|
-
context "
|
279
|
-
should "serialize to json" do
|
280
|
-
foo = Foo.new :x => 42, :y => true
|
281
|
-
assert_match /"x":42/, foo.to_json
|
282
|
-
assert_match /"y":true/, foo.to_json
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
context "restore" do
|
316
|
+
context "#restore" do
|
287
317
|
should "restore an entity from json" do
|
288
318
|
before = Foo.create :x => 42, :y => true
|
289
319
|
after = Foo.restore(before.key, before.to_json)
|
data/test/test_many_to_many.rb
CHANGED
@@ -41,13 +41,37 @@ class TestManyToMany < Test::Unit::TestCase
|
|
41
41
|
should "add a new group to both associations" do
|
42
42
|
@tim.groups.add(@rugb)
|
43
43
|
assert_equal [@tim], @rugb.members
|
44
|
+
assert_equal [@rugb], @tim.groups
|
44
45
|
end
|
45
46
|
|
46
47
|
should "add a new person to both associations" do
|
47
48
|
@rugb.members.add(@tim)
|
49
|
+
assert_equal [@tim], @rugb.members
|
48
50
|
assert_equal [@rugb], @tim.groups
|
49
51
|
end
|
50
52
|
end
|
53
|
+
|
54
|
+
context "remove" do
|
55
|
+
setup do
|
56
|
+
@tim = Person.create :name => 'tim'
|
57
|
+
@rugb = @tim.groups.create(:name => 'rug-b')
|
58
|
+
@erlang = @tim.groups.create(:name => 'erlang')
|
59
|
+
@aws = @tim.groups.create(:name => 'aws')
|
60
|
+
end
|
61
|
+
|
62
|
+
should "remove a group from both associations" do
|
63
|
+
@tim.groups.remove(@erlang)
|
64
|
+
assert_equal [@rugb, @aws], @tim.groups
|
65
|
+
assert_equal [], @erlang.members
|
66
|
+
end
|
67
|
+
|
68
|
+
should "remove a person from both associations" do
|
69
|
+
@erlang.members.remove(@tim)
|
70
|
+
assert_equal [@rugb, @aws], @tim.groups
|
71
|
+
assert_equal [], @erlang.members
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
51
75
|
end
|
52
76
|
|
53
77
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Lossen
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-04-
|
12
|
+
date: 2010-04-30 00:00:00 +02:00
|
13
13
|
default_executable: redis-monitor.rb
|
14
14
|
dependencies: []
|
15
15
|
|