nested 0.0.7 → 0.0.8
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/lib/nested.rb +9 -29
- data/nested.gemspec +1 -1
- data/test/nested_test.rb +7 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 166d069fbce22175e7ed0212140ae0c269484e68
|
4
|
+
data.tar.gz: 64e7a84b68750c89bc54ecc827c4ef35d2c48ede
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f7c3dd136ccbfc5a716d68033a87db8a141939779f58b732661e541aaae3a69b29b9952002d4c8106eb0966da4e4e0f9517dfccbd0415262f022cf2875b9a07
|
7
|
+
data.tar.gz: 3ba0d93d82c54adfb4c56b4e09a873c211a95705cacb8bdcf7c396f3625931178255ff2c7800c4081fadf226c859def7b84077d98599f6381544174409b9f5e3
|
data/lib/nested.rb
CHANGED
@@ -15,10 +15,6 @@ module Nested
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class Resource
|
18
|
-
SERIALIZE = ->(obj, sinatra, resource) do
|
19
|
-
obj
|
20
|
-
end
|
21
|
-
|
22
18
|
FETCH = ->(resource, ctrl) do
|
23
19
|
raise "implement fetch for resource #{resource.name}" unless resource.parent
|
24
20
|
raise "implement fetch for singleton #{resource.name}" if resource.singleton?
|
@@ -48,12 +44,16 @@ module Nested
|
|
48
44
|
@resources = []
|
49
45
|
@actions = []
|
50
46
|
|
51
|
-
|
47
|
+
init &->(resource) do
|
52
48
|
fetched = FETCH.call(resource, self)
|
53
49
|
|
54
50
|
puts "set @#{resource.instance_variable_name} to #{fetched.inspect} for #{self}"
|
55
51
|
self.instance_variable_set("@#{resource.instance_variable_name}", fetched)
|
56
52
|
end
|
53
|
+
|
54
|
+
serialize &->(obj, resource) do
|
55
|
+
obj
|
56
|
+
end
|
57
57
|
end
|
58
58
|
|
59
59
|
def singleton?
|
@@ -71,28 +71,12 @@ module Nested
|
|
71
71
|
def serialize(*args, &block)
|
72
72
|
raise "pass either *args or &block" if args.empty? && !block
|
73
73
|
|
74
|
-
@__serialize = ->(obj,
|
75
|
-
obj =
|
74
|
+
@__serialize = ->(obj, resource) do
|
75
|
+
obj = self.instance_exec(obj, resource, &block) if block
|
76
76
|
obj = obj.attributes if obj.is_a?(ActiveRecord::Base)
|
77
77
|
obj = obj.symbolize_keys.slice(*args) unless args.empty?
|
78
78
|
obj
|
79
79
|
end
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
# if block && !args.empty?
|
84
|
-
# @__serialize = ->(obj, sinatra, resource) do
|
85
|
-
# block.call(obj).attributes.symbolize_keys.slice(*args)
|
86
|
-
# end
|
87
|
-
# elsif block && args.empty?
|
88
|
-
# @__serialize = block
|
89
|
-
# elsif !block && !args.empty?
|
90
|
-
# @__serialize = ->(obj, sinatra, resource) do
|
91
|
-
# obj.attributes.symbolize_keys.slice(*args)
|
92
|
-
# end
|
93
|
-
# else
|
94
|
-
# raise "pass either *args or &block"
|
95
|
-
# end
|
96
80
|
end
|
97
81
|
|
98
82
|
def init(&block)
|
@@ -179,10 +163,6 @@ module Nested
|
|
179
163
|
(self.parents + [self]).reverse
|
180
164
|
end
|
181
165
|
|
182
|
-
def serializer
|
183
|
-
@__serialize || SERIALIZE
|
184
|
-
end
|
185
|
-
|
186
166
|
# --------------------------
|
187
167
|
|
188
168
|
def sinatra_init(sinatra)
|
@@ -229,9 +209,9 @@ module Nested
|
|
229
209
|
|
230
210
|
def sinatra_response_create_data(sinatra, response)
|
231
211
|
data = if response && collection?
|
232
|
-
response.to_a.map{|e|
|
212
|
+
response.to_a.map{|e| sinatra.instance_exec(e, self, &@__serialize) }
|
233
213
|
else
|
234
|
-
|
214
|
+
sinatra.instance_exec(response, self, &@__serialize)
|
235
215
|
end
|
236
216
|
|
237
217
|
{data: data, ok: true}
|
data/nested.gemspec
CHANGED
data/test/nested_test.rb
CHANGED
@@ -74,7 +74,7 @@ class NestedTest < Test::Unit::TestCase
|
|
74
74
|
|
75
75
|
@r.serialize :name
|
76
76
|
|
77
|
-
assert_equal({name: :joe}, @r.instance_variable_get("@__serialize").call({name: :joe, test: true}, {}
|
77
|
+
assert_equal({name: :joe}, @r.instance_variable_get("@__serialize").call({name: :joe, test: true}, {}))
|
78
78
|
end
|
79
79
|
|
80
80
|
def test_route
|
@@ -312,9 +312,12 @@ class NestedTest < Test::Unit::TestCase
|
|
312
312
|
|
313
313
|
def test_serializer
|
314
314
|
singleton!
|
315
|
-
assert_equal(@r.serializer, Nested::Resource::SERIALIZE)
|
316
|
-
|
317
|
-
|
315
|
+
# assert_equal(@r.serializer, Nested::Resource::SERIALIZE)
|
316
|
+
|
317
|
+
ser = ->(obj, resource) { [obj, resource] }
|
318
|
+
@r.serialize &ser
|
319
|
+
|
320
|
+
assert_equal [1, @r], @r.instance_variable_get("@__serialize").call(1, @r)
|
318
321
|
end
|
319
322
|
|
320
323
|
# ----
|