nested 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/lib/nested.rb +86 -52
- data/nested.gemspec +1 -1
- data/test/nested_test.rb +50 -18
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed79d5543f77277d339837d0746299ca1f718593
|
4
|
+
data.tar.gz: 62e5017a5bc17b1196060c7442c6cc16c5992c73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4e50463dd6b7ccaa6d04a6b70ac150b9c4a39c2191f900568630629d7c0a7b59c6fdb7c7bb817ce223bea86c20f7a48fc4ff90a145a3ee3edeabb83b43ab28c
|
7
|
+
data.tar.gz: 77d2faef6c33c182a3a1a7f626fb34269694107a1e7f74dad68b69b7ea232d91f9c370161170558e25927f0b801221ef2b857f73aae832aaa90e760b91ce2532
|
data/.gitignore
CHANGED
data/lib/nested.rb
CHANGED
@@ -61,21 +61,10 @@ module Nested
|
|
61
61
|
@collection == true
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
def serialize(&block)
|
69
|
-
@__serialize = block
|
70
|
-
end
|
71
|
-
|
72
|
-
def serialize_object(obj, ctrl)
|
73
|
-
(@__serialize || SERIALIZE).call(obj, ctrl, self)
|
74
|
-
end
|
75
|
-
|
76
|
-
def fetch_object(ctrl)
|
77
|
-
(@__fetch || FETCH).call(self, ctrl)
|
78
|
-
end
|
64
|
+
def before_fetch(&block); @__before_fetch = block; end
|
65
|
+
def fetch(&block); @__fetch = block; end
|
66
|
+
def after_fetch(&block); @__after_fetch = block; end
|
67
|
+
def serialize(&block); @__serialize = block; end
|
79
68
|
|
80
69
|
def route(args={}, action=nil)
|
81
70
|
"".tap do |r|
|
@@ -157,6 +146,85 @@ module Nested
|
|
157
146
|
(self.parents + [self]).reverse
|
158
147
|
end
|
159
148
|
|
149
|
+
def fetcher
|
150
|
+
@__fetch || FETCH
|
151
|
+
end
|
152
|
+
|
153
|
+
def serializer
|
154
|
+
@__serialize || SERIALIZE
|
155
|
+
end
|
156
|
+
|
157
|
+
# --------------------------
|
158
|
+
|
159
|
+
def sinatra_init(sinatra)
|
160
|
+
@__before_fetch.call(self, sinatra) if @__before_fetch
|
161
|
+
resource_obj = fetcher.call(self, sinatra)
|
162
|
+
|
163
|
+
puts "set @#{self.instance_variable_name} to #{resource_obj.inspect} for #{sinatra}"
|
164
|
+
sinatra.instance_variable_set("@#{self.instance_variable_name}", resource_obj)
|
165
|
+
|
166
|
+
@__after_fetch.call(self, sinatra) if @__after_fetch
|
167
|
+
end
|
168
|
+
|
169
|
+
def sinatra_exec_get_block(sinatra, &block)
|
170
|
+
sinatra.instance_exec(self, &block)
|
171
|
+
end
|
172
|
+
|
173
|
+
def sinatra_exec_delete_block(sinatra, &block)
|
174
|
+
sinatra.instance_exec(self, &block)
|
175
|
+
end
|
176
|
+
|
177
|
+
def sinatra_read_json_body(sinatra)
|
178
|
+
sinatra.request.body.rewind
|
179
|
+
HashWithIndifferentAccess.new JSON.parse(sinatra.request.body.read)
|
180
|
+
end
|
181
|
+
|
182
|
+
def sinatra_exec_put_block(sinatra, &block)
|
183
|
+
data = sinatra_read_json_body(sinatra)
|
184
|
+
instance_exec(data, self, &block)
|
185
|
+
end
|
186
|
+
|
187
|
+
def sinatra_exec_post_block(sinatra, &block)
|
188
|
+
data = sinatra_read_json_body(sinatra)
|
189
|
+
res = instance_exec(data, self, &block)
|
190
|
+
sinatra.instance_variable_set("@#{self.instance_variable_name}", res)
|
191
|
+
end
|
192
|
+
|
193
|
+
def sinatra_response_type(response)
|
194
|
+
(response.is_a?(ActiveModel::Errors) || (response.respond_to?(:errors) && !response.errors.empty?)) ? :error : :data
|
195
|
+
end
|
196
|
+
|
197
|
+
def sinatra_response(sinatra)
|
198
|
+
response = sinatra.instance_variable_get("@#{self.instance_variable_name}")
|
199
|
+
response = self.send(:"sinatra_response_create_#{sinatra_response_type(response)}", sinatra, response)
|
200
|
+
|
201
|
+
case response
|
202
|
+
when String then response
|
203
|
+
else response.to_json
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def sinatra_response_create_data(sinatra, response)
|
208
|
+
data = if response.respond_to?(:to_a)
|
209
|
+
response.to_a.map{|e| serializer.call(e, sinatra, self)}
|
210
|
+
else
|
211
|
+
serializer(response, sinatra, self)
|
212
|
+
end
|
213
|
+
|
214
|
+
{data: data, ok: true}
|
215
|
+
end
|
216
|
+
|
217
|
+
def sinatra_response_create_error(sinatra, response)
|
218
|
+
errors = response.is_a?(ActiveModel::Errors) ? response : response.errors
|
219
|
+
|
220
|
+
data = errors.to_hash.inject({}) do |memo, e|
|
221
|
+
memo[e[0]] = e[1][0]
|
222
|
+
memo
|
223
|
+
end
|
224
|
+
|
225
|
+
{data: data, ok: false}
|
226
|
+
end
|
227
|
+
|
160
228
|
def create_sinatra_route(method, action, &block)
|
161
229
|
@actions << {method: method, action: action}
|
162
230
|
|
@@ -169,46 +237,12 @@ module Nested
|
|
169
237
|
content_type :json
|
170
238
|
|
171
239
|
resource.self_and_parents.reverse.each do |res|
|
172
|
-
|
173
|
-
|
174
|
-
puts "set @#{res.instance_variable_name} to #{resource_obj.inspect} for #{self}"
|
175
|
-
instance_variable_set("@#{res.instance_variable_name}", resource_obj)
|
176
|
-
end
|
177
|
-
|
178
|
-
if [:get, :delete].include?(method)
|
179
|
-
instance_exec(resource, &block)
|
180
|
-
else [:put, :post].include?(method)
|
181
|
-
request.body.rewind
|
182
|
-
data = HashWithIndifferentAccess.new JSON.parse(request.body.read)
|
183
|
-
instance_exec(data, resource, &block) if method == :put
|
184
|
-
instance_variable_set("@#{resource.instance_variable_name}", instance_exec(data, resource, &block)) if method == :post
|
240
|
+
res.sinatra_init(self)
|
185
241
|
end
|
186
242
|
|
187
|
-
|
188
|
-
|
189
|
-
if response.is_a?(ActiveModel::Errors) || response.respond_to?(:errors) && !response.errors.empty?
|
190
|
-
errors = response.is_a?(ActiveModel::Errors) ? response : response.errors
|
191
|
-
|
192
|
-
data = errors.to_hash.inject({}) do |memo, e|
|
193
|
-
memo[e[0]] = e[1][0]
|
194
|
-
memo
|
195
|
-
end
|
196
|
-
|
197
|
-
response = {data: data, ok: false}
|
198
|
-
else
|
199
|
-
data = if response.respond_to?(:to_a)
|
200
|
-
response.to_a.map{|e| resource.serialize_object(e, self)}
|
201
|
-
else
|
202
|
-
resource.serialize_object(response, self)
|
203
|
-
end
|
243
|
+
resource.send(:"sinatra_exec_#{method}_block", self, &block)
|
204
244
|
|
205
|
-
|
206
|
-
end
|
207
|
-
|
208
|
-
case response
|
209
|
-
when String then response
|
210
|
-
else response.to_json
|
211
|
-
end
|
245
|
+
resource.sinatra_response(self)
|
212
246
|
end
|
213
247
|
end
|
214
248
|
end
|
data/nested.gemspec
CHANGED
data/test/nested_test.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
require "mocha/setup"
|
3
3
|
require "active_support/all"
|
4
|
+
require "active_model/errors"
|
4
5
|
require "nested"
|
5
6
|
|
6
7
|
class NestedTest < Test::Unit::TestCase
|
@@ -76,31 +77,31 @@ class NestedTest < Test::Unit::TestCase
|
|
76
77
|
assert_equal(serialize, @r.instance_variable_get("@__serialize"))
|
77
78
|
end
|
78
79
|
|
79
|
-
def test_fetch_object
|
80
|
-
|
80
|
+
# def test_fetch_object
|
81
|
+
# singleton!
|
81
82
|
|
82
|
-
|
83
|
-
|
83
|
+
# Nested::Resource::FETCH.expects(:call).with(@r, {})
|
84
|
+
# @r.fetch_object({})
|
84
85
|
|
85
|
-
|
86
|
-
|
86
|
+
# fetch = -> { }
|
87
|
+
# @r.fetch &fetch
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
end
|
89
|
+
# fetch.expects(:call).with(@r, {})
|
90
|
+
# @r.fetch_object({})
|
91
|
+
# end
|
91
92
|
|
92
|
-
def test_serialize_object
|
93
|
-
|
93
|
+
# def test_serialize_object
|
94
|
+
# singleton!
|
94
95
|
|
95
|
-
|
96
|
-
|
96
|
+
# Nested::Resource::SERIALIZE.expects(:call).with({name: :joe}, {}, @r)
|
97
|
+
# @r.serialize_object({name: :joe}, {})
|
97
98
|
|
98
|
-
|
99
|
-
|
99
|
+
# serialize = -> { }
|
100
|
+
# @r.serialize &serialize
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
end
|
102
|
+
# serialize.expects(:call).with({name: :joe}, {}, @r)
|
103
|
+
# @r.serialize_object({name: :joe}, {})
|
104
|
+
# end
|
104
105
|
|
105
106
|
def test_route
|
106
107
|
# no parent
|
@@ -335,6 +336,37 @@ class NestedTest < Test::Unit::TestCase
|
|
335
336
|
assert_equal [{method: :post, action: :action}], @r.actions
|
336
337
|
end
|
337
338
|
|
339
|
+
def test_fetcher
|
340
|
+
singleton!
|
341
|
+
assert_equal(@r.fetcher, Nested::Resource::FETCH)
|
342
|
+
@r.instance_variable_set("@__fetch", 123)
|
343
|
+
assert_equal(@r.fetcher, 123)
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_serializer
|
347
|
+
singleton!
|
348
|
+
assert_equal(@r.serializer, Nested::Resource::SERIALIZE)
|
349
|
+
@r.instance_variable_set("@__serialize", 123)
|
350
|
+
assert_equal(@r.serializer, 123)
|
351
|
+
end
|
352
|
+
|
353
|
+
# ----
|
354
|
+
|
355
|
+
def test_sinatra_response_type
|
356
|
+
singleton!
|
357
|
+
assert_equal :error, @r.sinatra_response_type(ActiveModel::Errors.new({}))
|
358
|
+
|
359
|
+
obj = OpenStruct.new(errors: ActiveModel::Errors.new({}))
|
360
|
+
assert_equal :data, @r.sinatra_response_type(obj)
|
361
|
+
|
362
|
+
obj.errors.add(:somefield, "some error")
|
363
|
+
assert_equal :error, @r.sinatra_response_type(obj)
|
364
|
+
|
365
|
+
assert_equal :data, @r.sinatra_response_type(nil)
|
366
|
+
assert_equal :data, @r.sinatra_response_type(123)
|
367
|
+
end
|
368
|
+
|
369
|
+
|
338
370
|
# ----
|
339
371
|
|
340
372
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Zimmek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|