nested 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ece57cb831e671f0e3db5c785867a19aef488531
4
- data.tar.gz: d2a6be9a7a66705051d3bdbe502aafe8cf8b86b7
3
+ metadata.gz: 49bb517e6971a0a86442877831128da5bba7655d
4
+ data.tar.gz: 02518d037ff5a282f2617d78aed2c6cdf93b7624
5
5
  SHA512:
6
- metadata.gz: fa90d6d5af153bd20d31c805c5b6efb407a7fd00670f0d6fa475b9074faf89531b9500d1c92890a78dd1b9f0965373bb4c0434f5c1eceaf5bc86b9941b9e23a5
7
- data.tar.gz: 559040efaf939783b885922b6003017fe58907060e03c79c65f159135f567ecdb40f5082c63a7d7cb629070c16f5e85865b85b34d34e5488c1c401b9f89d24b0
6
+ metadata.gz: f9aa03054fc3fcb2dd7cfbe8ac8f91442b37dde54976e865ca43ddc05681117a70cf9b24fd0eb254b9ffd652267c8f180619a839173cb1315df4c4a5dbd58637
7
+ data.tar.gz: 41ce15bbd4beb3000ce43bb56172f20843a9509872d0b1b0b9b8c65b23b3938228c4f1a91de01a5b094aa8a60e296eed39e3cf3450b8f90ba94be059fba05626
data/lib/nested.rb CHANGED
@@ -15,7 +15,7 @@ module Nested
15
15
  end
16
16
 
17
17
  class Resource
18
- SERIALIZE = ->(obj, ctrl, resource) do
18
+ SERIALIZE = ->(obj, sinatra, resource) do
19
19
  obj
20
20
  end
21
21
 
@@ -47,6 +47,13 @@ module Nested
47
47
  @parent = parent
48
48
  @resources = []
49
49
  @actions = []
50
+
51
+ @__init = ->(resource) do
52
+ fetched = FETCH.call(resource, self)
53
+
54
+ puts "set @#{resource.instance_variable_name} to #{fetched.inspect} for #{self}"
55
+ self.instance_variable_set("@#{resource.instance_variable_name}", fetched)
56
+ end
50
57
  end
51
58
 
52
59
  def singleton?
@@ -61,10 +68,36 @@ module Nested
61
68
  @collection == true
62
69
  end
63
70
 
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
71
+ def serialize(*args, &block)
72
+ raise "pass either *args or &block" if args.empty? && !block
73
+
74
+ @__serialize = ->(obj, sinatra, resource) do
75
+ obj = block.call(obj) if block
76
+ obj = obj.attributes if obj.is_a?(ActiveRecord::Base)
77
+ obj = obj.symbolize_keys.slice(*args) unless args.empty?
78
+ obj
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
+ end
97
+
98
+ def init(&block)
99
+ @__init = block
100
+ end
68
101
 
69
102
  def route(args={}, action=nil)
70
103
  "".tap do |r|
@@ -146,10 +179,6 @@ module Nested
146
179
  (self.parents + [self]).reverse
147
180
  end
148
181
 
149
- def fetcher
150
- @__fetch || FETCH
151
- end
152
-
153
182
  def serializer
154
183
  @__serialize || SERIALIZE
155
184
  end
@@ -157,13 +186,7 @@ module Nested
157
186
  # --------------------------
158
187
 
159
188
  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
189
+ sinatra.instance_exec(self, &@__init)
167
190
  end
168
191
 
169
192
  def sinatra_exec_get_block(sinatra, &block)
data/nested.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "nested"
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
 
5
5
  s.authors = ["Jan Zimmek"]
6
6
  s.email = %q{jan.zimmek@web.de}
data/test/nested_test.rb CHANGED
@@ -2,6 +2,7 @@ require "test/unit"
2
2
  require "mocha/setup"
3
3
  require "active_support/all"
4
4
  require "active_model/errors"
5
+ require "active_record"
5
6
  require "nested"
6
7
 
7
8
  class NestedTest < Test::Unit::TestCase
@@ -59,50 +60,23 @@ class NestedTest < Test::Unit::TestCase
59
60
  assert_equal false, @r.member?
60
61
  end
61
62
 
62
- def test_fetch
63
+ def test_init
63
64
  singleton!
64
65
 
65
- fetch = -> { }
66
- @r.fetch &fetch
66
+ init = -> { }
67
+ @r.init &init
67
68
 
68
- assert_equal(fetch, @r.instance_variable_get("@__fetch"))
69
+ assert_equal init, @r.instance_variable_get("@__init")
69
70
  end
70
71
 
71
72
  def test_serialize
72
73
  singleton!
73
74
 
74
- serialize = -> { }
75
- @r.serialize &serialize
75
+ @r.serialize :name
76
76
 
77
- assert_equal(serialize, @r.instance_variable_get("@__serialize"))
77
+ assert_equal({name: :joe}, @r.instance_variable_get("@__serialize").call({name: :joe, test: true}, {}, {}))
78
78
  end
79
79
 
80
- # def test_fetch_object
81
- # singleton!
82
-
83
- # Nested::Resource::FETCH.expects(:call).with(@r, {})
84
- # @r.fetch_object({})
85
-
86
- # fetch = -> { }
87
- # @r.fetch &fetch
88
-
89
- # fetch.expects(:call).with(@r, {})
90
- # @r.fetch_object({})
91
- # end
92
-
93
- # def test_serialize_object
94
- # singleton!
95
-
96
- # Nested::Resource::SERIALIZE.expects(:call).with({name: :joe}, {}, @r)
97
- # @r.serialize_object({name: :joe}, {})
98
-
99
- # serialize = -> { }
100
- # @r.serialize &serialize
101
-
102
- # serialize.expects(:call).with({name: :joe}, {}, @r)
103
- # @r.serialize_object({name: :joe}, {})
104
- # end
105
-
106
80
  def test_route
107
81
  # no parent
108
82
  singleton!
@@ -336,13 +310,6 @@ class NestedTest < Test::Unit::TestCase
336
310
  assert_equal [{method: :post, action: :action}], @r.actions
337
311
  end
338
312
 
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
313
  def test_serializer
347
314
  singleton!
348
315
  assert_equal(@r.serializer, Nested::Resource::SERIALIZE)
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.5
4
+ version: 0.0.6
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-04 00:00:00.000000000 Z
11
+ date: 2013-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport