nested 0.0.27 → 0.0.28
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -3
- data/lib/nested/app.rb +2 -2
- data/lib/nested/many.rb +12 -6
- data/lib/nested/one.rb +13 -10
- data/lib/nested/resource.rb +34 -12
- data/lib/nested/serializer.rb +13 -9
- data/lib/nested/singleton.rb +9 -4
- data/lib/nested/with_many.rb +4 -4
- data/lib/nested/with_model_block.rb +9 -0
- data/lib/nested/with_singleton.rb +4 -4
- data/lib/nested.rb +2 -0
- data/nested.gemspec +1 -1
- data/test/nested_test.rb +136 -24
- data/test/serializer_test.rb +2 -0
- data/test/test_helper.rb +4 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 530eebbefcb6b3078c4e10362b44bb5f86a18d3a
|
4
|
+
data.tar.gz: 782cad01102d3b0af2baf4f63ce0413228afec18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7895a40cc84bc8068daea8d4c01b253dea34aac7e84bb0b94519003e9aa5c537261c7cdcfc28df839a8f55c674276076f89a62bf43b44ebde52af096196c591c
|
7
|
+
data.tar.gz: 55aa216d43349384134b38cbe7a6087610ca6508c3621d0075e40c2e1406bd801eac897104721048606f1857fbce6c8f6131e87895cdf82771954f013e19d0cb
|
data/README.md
CHANGED
@@ -47,7 +47,7 @@ Congrats! You just created your first nested API. Checkout the other sections to
|
|
47
47
|
|
48
48
|
## Nested:App
|
49
49
|
|
50
|
-
In nested you implement your API as a normal ruby class which
|
50
|
+
In nested you implement your API as a normal ruby class which inherits from Nested::App
|
51
51
|
|
52
52
|
```
|
53
53
|
class MyApi < Nested::App
|
@@ -170,7 +170,7 @@ class MyApi < Nested::App
|
|
170
170
|
end
|
171
171
|
```
|
172
172
|
|
173
|
-
The model is available in all http verb
|
173
|
+
The model is available in all http verb blocks. Except in post. The post block has to return a new model.
|
174
174
|
|
175
175
|
```
|
176
176
|
class MyApi < Nested::App
|
@@ -182,7 +182,7 @@ class MyApi < Nested::App
|
|
182
182
|
end
|
183
183
|
```
|
184
184
|
|
185
|
-
Nested gives you access to http parameters through the params method as known from other frameworks and addition to this a useful shorthand.
|
185
|
+
Nested gives you access to http parameters through the params method as known from other frameworks and in addition to this a useful shorthand.
|
186
186
|
|
187
187
|
```
|
188
188
|
class MyApi < Nested::App
|
@@ -201,3 +201,52 @@ class MyApi < Nested::App
|
|
201
201
|
end
|
202
202
|
end
|
203
203
|
```
|
204
|
+
## Serialize
|
205
|
+
|
206
|
+
Nested does not automatically serialize and expose a attributes of the underlying model as resource fields. You have to list them explicitly. This allows you fine grain control over what and when something is exposed and gets you a decoupling from your model layer as well.
|
207
|
+
|
208
|
+
Serialize a single field
|
209
|
+
|
210
|
+
```
|
211
|
+
class MyApi < Nested::App
|
212
|
+
singleton :user do
|
213
|
+
serialize :email
|
214
|
+
get
|
215
|
+
end
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
Serialize multiple fields
|
220
|
+
|
221
|
+
```
|
222
|
+
class MyApi < Nested::App
|
223
|
+
singleton :user do
|
224
|
+
serialize :id, :email, :username
|
225
|
+
get
|
226
|
+
end
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
230
|
+
You can invoke serialize multiple times.
|
231
|
+
|
232
|
+
```
|
233
|
+
class MyApi < Nested::App
|
234
|
+
singleton :user do
|
235
|
+
serialize :id, :email
|
236
|
+
serialize :username
|
237
|
+
get
|
238
|
+
end
|
239
|
+
end
|
240
|
+
```
|
241
|
+
|
242
|
+
In the previous examples we always serialize a 1:1 field value from the model. Sometimes you want to transform the model value or serialize some completly synthetic fields. This can be easily accomplished by passing a one entry Hash to serialize. The key will be used as serialized field name. The value of the hash is expected to be a block which gets invoked with the model as argument. The return value of the block will be used a serialization value.
|
243
|
+
|
244
|
+
```
|
245
|
+
class MyApi < Nested::App
|
246
|
+
singleton :user do
|
247
|
+
serialize :id, :email
|
248
|
+
serialize username: ->(user){ "*** #{user.username} ***" }
|
249
|
+
get
|
250
|
+
end
|
251
|
+
end
|
252
|
+
```
|
data/lib/nested/app.rb
CHANGED
@@ -8,8 +8,8 @@ module Nested
|
|
8
8
|
clazz.instance_variable_set("@config", {})
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.child_resource(name, clazz, resource_if_block,
|
12
|
-
clazz.new(sinatra, name, nil, resource_if_block,
|
11
|
+
def self.child_resource(name, clazz, resource_if_block, model_block, &block)
|
12
|
+
clazz.new(sinatra, name, nil, resource_if_block, model_block)
|
13
13
|
.tap{|r| r.instance_eval(&(block||Proc.new{ }))}
|
14
14
|
end
|
15
15
|
|
data/lib/nested/many.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
module Nested
|
2
2
|
class Many < Resource
|
3
|
+
include WithModelBlock
|
4
|
+
|
5
|
+
MODEL_BLOCK = Proc.new do
|
6
|
+
if @__resource.parent
|
7
|
+
instance_variable_get("@#{@__resource.parent.instance_variable_name}").send(@__resource.name)
|
8
|
+
else
|
9
|
+
nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
def one(&block)
|
4
14
|
one_if(PROC_TRUE, &block)
|
5
15
|
end
|
@@ -8,12 +18,8 @@ module Nested
|
|
8
18
|
child_resource(self.name.to_s.singularize.to_sym, One, resource_if_block, nil, &block)
|
9
19
|
end
|
10
20
|
|
11
|
-
def
|
12
|
-
|
13
|
-
Proc.new{ instance_variable_get("@#{@__resource.parent.instance_variable_name}").send(@__resource.name) }
|
14
|
-
else
|
15
|
-
Proc.new { nil }
|
16
|
-
end
|
21
|
+
def default_model_block
|
22
|
+
MODEL_BLOCK
|
17
23
|
end
|
18
24
|
end
|
19
25
|
end
|
data/lib/nested/one.rb
CHANGED
@@ -2,20 +2,23 @@ module Nested
|
|
2
2
|
class One < Resource
|
3
3
|
include WithMany
|
4
4
|
|
5
|
+
MODEL_BLOCK = Proc.new do
|
6
|
+
if @__resource.parent
|
7
|
+
instance_variable_get("@#{@__resource.parent.instance_variable_name}")
|
8
|
+
.where(id: params[:"#{@__resource.parent.name.to_s.singularize.to_sym}_id"])
|
9
|
+
.first
|
10
|
+
else
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
5
16
|
def initialize_serializer_factory
|
6
17
|
Serializer.new(parent.serializer.includes)
|
7
18
|
end
|
8
19
|
|
9
|
-
def
|
10
|
-
|
11
|
-
Proc.new do
|
12
|
-
instance_variable_get("@#{@__resource.parent.instance_variable_name}")
|
13
|
-
.where(id: params[:"#{@__resource.parent.name.to_s.singularize.to_sym}_id"])
|
14
|
-
.first
|
15
|
-
end
|
16
|
-
else
|
17
|
-
Proc.new { nil }
|
18
|
-
end
|
20
|
+
def default_model_block
|
21
|
+
MODEL_BLOCK
|
19
22
|
end
|
20
23
|
|
21
24
|
def to_route_part
|
data/lib/nested/resource.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Nested
|
2
2
|
class Resource
|
3
|
-
attr_reader :name, :parent, :actions, :resources, :serializer, :
|
3
|
+
attr_reader :name, :parent, :actions, :resources, :serializer, :model_block, :sinatra, :before_blocks, :after_blocks
|
4
4
|
|
5
5
|
include WithSingleton
|
6
6
|
|
7
|
-
def initialize(sinatra, name, parent, resource_if_block,
|
7
|
+
def initialize(sinatra, name, parent, resource_if_block, model_block)
|
8
8
|
raise "resource must be given a name" unless name
|
9
9
|
|
10
10
|
@sinatra = sinatra
|
@@ -16,15 +16,15 @@ module Nested
|
|
16
16
|
raise "resource_if_block is nil, pass Nested::PROC_TRUE instead" unless resource_if_block
|
17
17
|
@resource_if_block = resource_if_block
|
18
18
|
|
19
|
-
unless @
|
19
|
+
unless @model_block = model_block
|
20
20
|
if is_a?(One)
|
21
|
-
@
|
21
|
+
@model_block = default_model_block
|
22
22
|
else
|
23
|
-
@
|
23
|
+
@model_block = resource_if_block != PROC_TRUE ? parent.try(:model_block) : default_model_block
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
raise "no
|
27
|
+
raise "no model_block passed and could not lookup any parent or default model_block" unless @model_block
|
28
28
|
|
29
29
|
@before_blocks = []
|
30
30
|
@after_blocks = []
|
@@ -36,12 +36,16 @@ module Nested
|
|
36
36
|
Serializer.new([])
|
37
37
|
end
|
38
38
|
|
39
|
-
def child_resource(name, clazz, resource_if_block,
|
40
|
-
clazz.new(@sinatra, name, self, resource_if_block,
|
39
|
+
def child_resource(name, clazz, resource_if_block, model_block, &block)
|
40
|
+
clazz.new(@sinatra, name, self, resource_if_block, model_block)
|
41
41
|
.tap{|r| r.instance_eval(&(block||Proc.new{ }))}
|
42
42
|
.tap{|r| @resources << r}
|
43
43
|
end
|
44
44
|
|
45
|
+
def default_model_block
|
46
|
+
raise "implement me"
|
47
|
+
end
|
48
|
+
|
45
49
|
def to_route_part
|
46
50
|
"/#{@name}"
|
47
51
|
end
|
@@ -58,15 +62,17 @@ module Nested
|
|
58
62
|
|
59
63
|
def serialize(*args)
|
60
64
|
args.each {|arg| serializer + arg }
|
61
|
-
|
65
|
+
self
|
62
66
|
end
|
63
67
|
|
64
68
|
def serialize_include_if(condition, *args)
|
65
69
|
args.each {|arg| @serializer + SerializerField.new(arg, condition) }
|
70
|
+
self
|
66
71
|
end
|
67
72
|
|
68
73
|
def serialize_exclude_if(condition, *args)
|
69
74
|
args.each {|arg| @serializer - SerializerField.new(arg, condition) }
|
75
|
+
self
|
70
76
|
end
|
71
77
|
|
72
78
|
def route_replace(route, args)
|
@@ -88,10 +94,12 @@ module Nested
|
|
88
94
|
instance_eval do
|
89
95
|
define_method method do |action=nil, &block|
|
90
96
|
send(:"#{method}_if", PROC_TRUE, action, &block)
|
97
|
+
self
|
91
98
|
end
|
92
99
|
|
93
100
|
define_method :"#{method}_if" do |method_if_block, action=nil, &block|
|
94
101
|
create_sinatra_route method, action, method_if_block, &(block||proc {})
|
102
|
+
self
|
95
103
|
end
|
96
104
|
end
|
97
105
|
end
|
@@ -116,14 +124,28 @@ module Nested
|
|
116
124
|
end
|
117
125
|
|
118
126
|
def sinatra_init(sinatra)
|
119
|
-
sinatra
|
127
|
+
sinatra_init_set_resource(sinatra)
|
120
128
|
|
121
129
|
raise "resource_if is false for resource: #{self.name} " unless sinatra.instance_exec(&@resource_if_block)
|
122
130
|
|
123
|
-
|
131
|
+
sinatra_init_before(sinatra)
|
132
|
+
sinatra_init_set_model(sinatra)
|
133
|
+
sinatra_init_after(sinatra)
|
134
|
+
end
|
124
135
|
|
125
|
-
|
136
|
+
def sinatra_init_set_resource(sinatra)
|
137
|
+
sinatra.instance_variable_set("@__resource", self)
|
138
|
+
end
|
139
|
+
|
140
|
+
def sinatra_init_set_model(sinatra)
|
141
|
+
sinatra.instance_variable_set("@#{self.instance_variable_name}", sinatra.instance_exec(*(@model_block.parameters.empty? ? [] : [sinatra.instance_exec(&default_model_block)]), &@model_block))
|
142
|
+
end
|
143
|
+
|
144
|
+
def sinatra_init_before(sinatra)
|
145
|
+
@before_blocks.each{|e| sinatra.instance_exec(&e)}
|
146
|
+
end
|
126
147
|
|
148
|
+
def sinatra_init_after(sinatra)
|
127
149
|
@after_blocks.each{|e| sinatra.instance_exec(&e)}
|
128
150
|
end
|
129
151
|
|
data/lib/nested/serializer.rb
CHANGED
@@ -29,17 +29,21 @@ module Nested
|
|
29
29
|
|
30
30
|
excludes = this.excludes.select{|e| instance_exec(&e.condition)}
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
if obj
|
33
|
+
this.includes.reject{|e| excludes.detect{|e2| e2.name == e.name}}.inject({}) do |memo, field|
|
34
|
+
if instance_exec(&field.condition)
|
35
|
+
case field.name
|
36
|
+
when Symbol
|
37
|
+
memo[field.name] = obj.is_a?(Hash) ? obj[field.name] : obj.send(field.name)
|
38
|
+
when Hash
|
39
|
+
field_name, proc = field.name.to_a.first
|
40
|
+
memo[field_name] = instance_exec(obj, &proc)
|
41
|
+
end
|
40
42
|
end
|
43
|
+
memo
|
41
44
|
end
|
42
|
-
|
45
|
+
else
|
46
|
+
nil
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
data/lib/nested/singleton.rb
CHANGED
@@ -1,13 +1,18 @@
|
|
1
1
|
module Nested
|
2
2
|
class Singleton < Resource
|
3
3
|
include WithMany
|
4
|
+
include WithModelBlock
|
4
5
|
|
5
|
-
|
6
|
-
if parent
|
7
|
-
|
6
|
+
MODEL_BLOCK = Proc.new do
|
7
|
+
if @__resource.parent
|
8
|
+
instance_variable_get("@#{@__resource.parent.instance_variable_name}").send(@__resource.name)
|
8
9
|
else
|
9
|
-
|
10
|
+
nil
|
10
11
|
end
|
11
12
|
end
|
13
|
+
|
14
|
+
def default_model_block
|
15
|
+
MODEL_BLOCK
|
16
|
+
end
|
12
17
|
end
|
13
18
|
end
|
data/lib/nested/with_many.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module Nested
|
2
2
|
module WithMany
|
3
|
-
def many(name,
|
4
|
-
many_if(PROC_TRUE, name,
|
3
|
+
def many(name, model_block=nil, &block)
|
4
|
+
many_if(PROC_TRUE, name, model_block, &block)
|
5
5
|
end
|
6
6
|
|
7
|
-
def many_if(resource_if_block, name,
|
8
|
-
child_resource(name, Many, resource_if_block,
|
7
|
+
def many_if(resource_if_block, name, model_block=nil, &block)
|
8
|
+
child_resource(name, Many, resource_if_block, model_block, &block)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Nested
|
2
2
|
module WithSingleton
|
3
|
-
def singleton(name,
|
4
|
-
singleton_if(PROC_TRUE, name,
|
3
|
+
def singleton(name, model_block=nil, &block)
|
4
|
+
singleton_if(PROC_TRUE, name, model_block, &block)
|
5
5
|
end
|
6
6
|
|
7
|
-
def singleton_if(resource_if_block, name,
|
8
|
-
child_resource(name, Singleton, resource_if_block,
|
7
|
+
def singleton_if(resource_if_block, name, model_block=nil, &block)
|
8
|
+
child_resource(name, Singleton, resource_if_block, model_block, &block)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
data/lib/nested.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Nested
|
2
2
|
PROC_TRUE = Proc.new{ true }
|
3
|
+
PROC_NIL = Proc.new{ nil }
|
3
4
|
end
|
4
5
|
|
5
6
|
require "json"
|
@@ -11,6 +12,7 @@ require "nested/redirect"
|
|
11
12
|
|
12
13
|
require "nested/with_many"
|
13
14
|
require "nested/with_singleton"
|
15
|
+
require "nested/with_model_block"
|
14
16
|
|
15
17
|
require "nested/resource"
|
16
18
|
|
data/nested.gemspec
CHANGED
data/test/nested_test.rb
CHANGED
@@ -14,18 +14,57 @@ class NestedTest < Test::Unit::TestCase
|
|
14
14
|
@sinatra = mock
|
15
15
|
end
|
16
16
|
|
17
|
+
def test_initialize
|
18
|
+
r = singleton(:project)
|
19
|
+
|
20
|
+
assert_equal :project, r.name
|
21
|
+
assert_equal nil, r.parent
|
22
|
+
assert_equal [], r.resources
|
23
|
+
assert_equal [], r.actions
|
24
|
+
assert_equal Nested::PROC_NIL, r.model_block
|
25
|
+
|
26
|
+
|
27
|
+
assert_equal [], r.before_blocks
|
28
|
+
assert_equal [], r.after_blocks
|
29
|
+
|
30
|
+
assert_equal Nested::Singleton::MODEL_BLOCK, singleton(:project, nil).model_block
|
31
|
+
assert_equal Nested::Many::MODEL_BLOCK, many(:projects, nil).model_block
|
32
|
+
assert_equal Nested::One::MODEL_BLOCK, many(:projects, nil).one.model_block
|
33
|
+
end
|
34
|
+
|
17
35
|
def test_serialize
|
18
36
|
serializer = mock()
|
19
37
|
|
20
38
|
r = singleton(:project)
|
21
39
|
r.stubs(:serializer).returns(serializer)
|
22
40
|
|
23
|
-
assert_equal
|
41
|
+
assert_equal r, r.serialize
|
24
42
|
|
25
43
|
serializer.expects(:+).with(:name)
|
26
44
|
r.serialize :name
|
27
45
|
end
|
28
46
|
|
47
|
+
def test_before
|
48
|
+
b = ->{}
|
49
|
+
r = singleton(:project)
|
50
|
+
assert_equal r, r.before(&b)
|
51
|
+
assert_equal [b], r.before_blocks
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_after
|
55
|
+
b = ->{}
|
56
|
+
r = singleton(:project)
|
57
|
+
assert_equal r, r.after(&b)
|
58
|
+
assert_equal [b], r.after_blocks
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_model_block
|
62
|
+
model_block = ->{ }
|
63
|
+
r = singleton(:project)
|
64
|
+
assert_equal r, r.model(&model_block)
|
65
|
+
assert_equal model_block, r.model_block
|
66
|
+
end
|
67
|
+
|
29
68
|
def test_route_replace
|
30
69
|
resource = many(:projects).one
|
31
70
|
|
@@ -177,52 +216,45 @@ class NestedTest < Test::Unit::TestCase
|
|
177
216
|
# get
|
178
217
|
|
179
218
|
@sinatra.expects(:send).with(:get, "/project")
|
180
|
-
singleton(:project)
|
219
|
+
r = singleton(:project)
|
220
|
+
assert_equal r, r.get
|
181
221
|
|
182
222
|
@sinatra.expects(:send).with(:get, "/project/action")
|
183
|
-
singleton(:project)
|
223
|
+
r = singleton(:project)
|
224
|
+
assert_equal r, r.get(:action)
|
184
225
|
|
185
226
|
# post
|
186
227
|
|
187
228
|
@sinatra.expects(:send).with(:post, "/project")
|
188
|
-
singleton(:project)
|
229
|
+
r = singleton(:project)
|
230
|
+
assert_equal r, r.post
|
189
231
|
|
190
232
|
@sinatra.expects(:send).with(:post, "/project/action")
|
191
|
-
singleton(:project)
|
233
|
+
r = singleton(:project)
|
234
|
+
assert_equal r, r.post(:action)
|
192
235
|
|
193
236
|
# put
|
194
237
|
|
195
238
|
@sinatra.expects(:send).with(:put, "/project")
|
196
|
-
singleton(:project)
|
239
|
+
r = singleton(:project)
|
240
|
+
assert_equal r, r.put
|
197
241
|
|
198
242
|
@sinatra.expects(:send).with(:put, "/project/action")
|
199
|
-
singleton(:project)
|
243
|
+
r = singleton(:project)
|
244
|
+
assert_equal r, r.put(:action)
|
200
245
|
|
201
246
|
# delete
|
202
247
|
|
203
248
|
@sinatra.expects(:send).with(:delete, "/project")
|
204
|
-
singleton(:project)
|
249
|
+
r = singleton(:project)
|
250
|
+
assert_equal r, r.delete
|
205
251
|
|
206
252
|
@sinatra.expects(:send).with(:delete, "/project/action")
|
207
|
-
singleton(:project)
|
253
|
+
r = singleton(:project)
|
254
|
+
assert_equal r, r.delete(:action)
|
208
255
|
end
|
209
256
|
|
210
257
|
|
211
|
-
# def test_serializer
|
212
|
-
# singleton!
|
213
|
-
|
214
|
-
# @r.serialize :name
|
215
|
-
|
216
|
-
# assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
|
217
|
-
# assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe", boss: true}))
|
218
|
-
# assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call(OpenStruct.new({name: "joe"})))
|
219
|
-
|
220
|
-
# @r.serialize :name, virtual: ->(o){ o[:name] + "!!" }
|
221
|
-
# assert_equal({name: "joe", virtual: "joe!!"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
|
222
|
-
# end
|
223
|
-
|
224
|
-
# # ----
|
225
|
-
|
226
258
|
def test_sinatra_response_type
|
227
259
|
assert_equal :error, singleton(:project).sinatra_response_type(ActiveModel::Errors.new({}))
|
228
260
|
|
@@ -319,4 +351,84 @@ class NestedTest < Test::Unit::TestCase
|
|
319
351
|
assert_equal ["project", "entry"], Nested::Js.function_arguments(many(:projects).one.many(:entries).one)
|
320
352
|
end
|
321
353
|
|
354
|
+
def test_sinatra_init
|
355
|
+
@sinatra.stubs(:get)
|
356
|
+
r = singleton(:project, ->{ {name: "joe"} }).get
|
357
|
+
|
358
|
+
r.expects(:sinatra_init_set_resource).with(@sinatra)
|
359
|
+
r.expects(:sinatra_init_before).with(@sinatra)
|
360
|
+
r.expects(:sinatra_init_set_model).with(@sinatra)
|
361
|
+
r.expects(:sinatra_init_after).with(@sinatra)
|
362
|
+
|
363
|
+
r.sinatra_init(@sinatra)
|
364
|
+
|
365
|
+
r.expects(:sinatra_init_set_resource).with(@sinatra)
|
366
|
+
|
367
|
+
assert_raise RuntimeError do
|
368
|
+
r.instance_variable_set("@resource_if_block", ->{ false })
|
369
|
+
r.sinatra_init(@sinatra)
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
def test_sinatra_init_set_resource
|
374
|
+
@sinatra.stubs(:get)
|
375
|
+
r = singleton(:project, ->{ {name: "joe"} }).get
|
376
|
+
r.sinatra_init_set_resource(@sinatra)
|
377
|
+
assert_equal r, @sinatra.instance_variable_get("@__resource")
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_sinatra_init_set_model
|
381
|
+
@sinatra.stubs(:get)
|
382
|
+
@sinatra.instance_variable_set("@some_value", 10)
|
383
|
+
r = singleton(:project, ->{ {name: "joe", some_value: @some_value} }).get
|
384
|
+
r.sinatra_init_set_model(@sinatra)
|
385
|
+
assert_equal ({name: "joe", some_value: 10}), @sinatra.instance_variable_get("@project")
|
386
|
+
|
387
|
+
@sinatra.stubs(:get)
|
388
|
+
@sinatra.instance_variable_set("@some_value", 10)
|
389
|
+
@sinatra.instance_variable_set("@other_value", 2)
|
390
|
+
|
391
|
+
r = singleton(:project, ->(default){ {name: "joe", some_value: @some_value * default} }).get
|
392
|
+
r.stubs(:default_model_block).returns(->{ @other_value })
|
393
|
+
r.sinatra_init_set_model(@sinatra)
|
394
|
+
assert_equal ({name: "joe", some_value: 20}), @sinatra.instance_variable_get("@project")
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_sinatra_init_before
|
398
|
+
@sinatra.stubs(:get)
|
399
|
+
before_called = false
|
400
|
+
|
401
|
+
r = singleton(:project, ->{ {name: "joe"} }).get.before(&->{ before_called = true })
|
402
|
+
r.sinatra_init_before(@sinatra)
|
403
|
+
|
404
|
+
assert_equal true, before_called
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_sinatra_init_after
|
408
|
+
@sinatra.stubs(:get)
|
409
|
+
after_called = false
|
410
|
+
|
411
|
+
r = singleton(:project, ->{ {name: "joe"} }).get.after(&->{ after_called = true })
|
412
|
+
r.sinatra_init_after(@sinatra)
|
413
|
+
|
414
|
+
assert_equal true, after_called
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_default_model_block
|
418
|
+
assert_equal Nested::Singleton::MODEL_BLOCK, singleton(:project).default_model_block
|
419
|
+
assert_equal Nested::Many::MODEL_BLOCK, many(:projects).default_model_block
|
420
|
+
assert_equal Nested::One::MODEL_BLOCK, many(:projects).one.default_model_block
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_model
|
424
|
+
model_block = ->{}
|
425
|
+
r = singleton(:project)
|
426
|
+
assert_equal r, r.model(&model_block)
|
427
|
+
assert_equal model_block, r.instance_variable_get("@model_block")
|
428
|
+
|
429
|
+
assert_raise RuntimeError do
|
430
|
+
singleton(:project, ->{ 10 }).model(&->{ 20 })
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
322
434
|
end
|
data/test/serializer_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module TestHelper
|
2
|
-
def singleton(name)
|
3
|
-
Nested::Singleton.new(@sinatra, name, nil, Nested::PROC_TRUE,
|
2
|
+
def singleton(name, model_block=Nested::PROC_NIL)
|
3
|
+
Nested::Singleton.new(@sinatra, name, nil, Nested::PROC_TRUE, model_block)
|
4
4
|
end
|
5
5
|
|
6
|
-
def many(name)
|
7
|
-
Nested::Many.new(@sinatra, name, nil, Nested::PROC_TRUE,
|
6
|
+
def many(name, model_block=Nested::PROC_NIL)
|
7
|
+
Nested::Many.new(@sinatra, name, nil, Nested::PROC_TRUE, model_block)
|
8
8
|
end
|
9
9
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.28
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Zimmek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/nested/serializer_field.rb
|
91
91
|
- lib/nested/singleton.rb
|
92
92
|
- lib/nested/with_many.rb
|
93
|
+
- lib/nested/with_model_block.rb
|
93
94
|
- lib/nested/with_singleton.rb
|
94
95
|
- nested.gemspec
|
95
96
|
- test/nested_test.rb
|