nested 0.0.24 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38c9a0d53765eb2f648f9d4f5ef2a9fb5fc4b528
4
- data.tar.gz: 3d8212a2d22886567404470d0b288514a961f2b5
3
+ metadata.gz: da43c009c3ff14c2773df3a11e9ea8d5ae2f8de0
4
+ data.tar.gz: f63ecccb28294309d354362ca5bcff05937dd11a
5
5
  SHA512:
6
- metadata.gz: 9e9ebefde4d1f116da2ceeaa625334d2eba6b1b54dcd45cf25e2c71f32722467b1bfbd9181f634c440704d19251cfe7fdcffd8ba4542011f48e70bde04b05698
7
- data.tar.gz: 2d7284067772373590898e15fc13cfe8df87581c39c4f446360b82199a851e32dd7a788c6f97647067d85059a732f24b328ca3a437cd8c0df49980a541158b60
6
+ metadata.gz: dc57e5a73bea891b475bc0810cecd6b9e3f48f3710d173f6b30c358c5345bf19e5075c9e8424effaf46aa5196b23951b522e09e3c0e717f2c8b314ff3049a469
7
+ data.tar.gz: 3b753f26a8ac5d8d57133b707637e78ce1ce80751ae8132b3979be3037edb1030ab72415a789a74e4409f3ca9021fa158b5da150c6a55e0c5d3e83f14c6389d1
data/lib/nested.rb CHANGED
@@ -2,9 +2,6 @@ require "json"
2
2
 
3
3
  module Nested
4
4
 
5
- class OneWithNameInManyError < StandardError
6
- end
7
-
8
5
  class ManyInManyError < StandardError
9
6
  end
10
7
 
@@ -21,23 +18,60 @@ module Nested
21
18
  end
22
19
  end
23
20
 
24
- class Resource
25
- FETCH = -> do
26
- raise "implement fetch for resource #{@__resource.name}" unless @__resource.parent
27
- raise "implement fetch for singleton #{@__resource.name}" if @__resource.singleton?
21
+ class SerializerField
22
+ attr_accessor :name, :condition
23
+ def initialize(name, condition)
24
+ @name = name
25
+ @condition = condition
26
+ end
27
+ end
28
28
 
29
- parent_resource = @__resource.parent
30
- parent_obj = instance_variable_get("@#{parent_resource.instance_variable_name}")
29
+ class Serializer
30
+ attr_accessor :includes, :excludes
31
31
 
32
- if @__resource.name
33
- scope = parent_obj.send(@__resource.name.to_s.pluralize.to_sym)
34
- @__resource.collection? ? scope : scope.where(id: params["#{@__resource.name.to_s.singularize}_id"]).first
35
- else
36
- parent_obj.where(id: params["#{parent_resource.name.to_s.singularize}_id"]).first
32
+ def initialize(includes=[])
33
+ @includes = includes.clone
34
+ @excludes = []
35
+ end
36
+
37
+ def +(field)
38
+ field = field.is_a?(SerializerField) ? field : SerializerField.new(field, ->{ true })
39
+
40
+ @includes << field unless @includes.detect{|e| e.name == field.name}
41
+ @excludes = @excludes.reject{|e| e.name == field.name}
42
+ self
43
+ end
44
+
45
+ def -(field)
46
+ field = field.is_a?(SerializerField) ? field : SerializerField.new(field, ->{ true })
47
+
48
+ @excludes << field unless @excludes.detect{|e| e.name == field.name}
49
+ self
50
+ end
51
+
52
+ def serialize
53
+ this = self
54
+ ->(obj) do
55
+ excludes = this.excludes.select{|e| instance_exec(&e.condition)}
56
+
57
+ this.includes.reject{|e| excludes.detect{|e2| e2.name == e.name}}.inject({}) do |memo, field|
58
+ if instance_exec(&field.condition)
59
+ case field.name
60
+ when Symbol
61
+ memo[field.name] = obj.is_a?(Hash) ? obj[field.name] : obj.send(field.name)
62
+ when Hash
63
+ field_name, proc = field.name.to_a.first
64
+ memo[field_name] = instance_exec(obj, &proc)
65
+ end
66
+ end
67
+ memo
68
+ end
37
69
  end
38
70
  end
71
+ end
39
72
 
40
- attr_reader :name, :parent, :actions, :resources
73
+ class Resource
74
+ attr_reader :name, :parent, :actions, :resources, :serializer
41
75
 
42
76
  def initialize(sinatra, name, singleton, collection, parent, init_block)
43
77
  raise SingletonAndCollectionError.new if singleton && collection
@@ -50,18 +84,10 @@ module Nested
50
84
  @parent = parent
51
85
  @resources = []
52
86
  @actions = []
87
+ @init_block = init_block
88
+ @run_blocks = []
53
89
 
54
- init &-> do
55
- fetched = instance_exec(&(init_block||FETCH))
56
-
57
- # puts "set @#{@__resource.instance_variable_name} to #{fetched.inspect} for #{self}"
58
- # self.instance_variable_set("@#{@__resource.instance_variable_name}", fetched)
59
- @__resource.sinatra_set_instance_variable(self, @__resource.instance_variable_name, fetched)
60
- end
61
-
62
- if member?
63
- serialize *@parent.instance_variable_get("@__serialize_args")
64
- end
90
+ @serializer = Serializer.new(member? ? parent.serializer.includes : [])
65
91
  end
66
92
 
67
93
  def singleton?
@@ -76,6 +102,11 @@ module Nested
76
102
  @collection == true
77
103
  end
78
104
 
105
+ def run(&block)
106
+ @run_blocks << block
107
+ self
108
+ end
109
+
79
110
  def type
80
111
  if singleton?
81
112
  :singleton
@@ -89,46 +120,34 @@ module Nested
89
120
  end
90
121
 
91
122
  def serialize(*args)
92
- @__serialize_args = args
93
-
94
- @__serialize = ->(obj) do
95
- args.inject({}) do |memo, field|
96
- case field
97
- when Symbol
98
- memo[field] = obj.is_a?(Hash) ? obj[field] : obj.send(field)
99
- when Hash
100
- field, proc = field.to_a.first
101
- memo[field] = self.instance_exec(obj, &proc)
102
- end
103
- memo
104
- end
105
- end
123
+ args.each {|arg| serializer + arg }
124
+ serializer
106
125
  end
107
126
 
108
- def init(&block)
109
- @__init = block
127
+ def serialize_include_if(condition, *args)
128
+ args.each {|arg| @serializer + SerializerField.new(arg, condition) }
110
129
  end
111
130
 
112
- def route(args={}, action=nil)
131
+ def serialize_exclude_if(condition, *args)
132
+ args.each {|arg| @serializer - SerializerField.new(arg, condition) }
133
+ end
134
+
135
+ def route_replace(route, args)
136
+ args.each do |k, v|
137
+ route = route.gsub(":#{k}", "#{v}")
138
+ end
139
+ route
140
+ end
141
+
142
+ def route(action=nil)
113
143
  "".tap do |r|
114
- r << @parent.route(args) if @parent
144
+ r << @parent.route if @parent
115
145
 
116
- if singleton?
117
- r << "/" + @name.to_s.singularize
118
- elsif collection?
119
- r << "/" + @name.to_s.pluralize
120
- else
121
- if @name
122
- r << "/" + @name.to_s.pluralize
123
- end
124
- r << "/"
125
- key = ((@name || @parent.name).to_s.singularize + "_id").to_sym
126
146
 
127
- if args.key?(key)
128
- r << args[key].to_s
129
- else
130
- r << ":#{key}"
131
- end
147
+ if singleton? || collection?
148
+ r << "/#{@name}"
149
+ else
150
+ r << "/:#{@name}_id"
132
151
  end
133
152
 
134
153
  r << "/#{action}" if action
@@ -152,17 +171,45 @@ module Nested
152
171
  end
153
172
 
154
173
  def singleton(name, init_block=nil, &block)
155
- child_resource(name, true, false, init_block, &block)
174
+ child_resource(name, true, false, init_block, &(block||Proc.new{ }))
156
175
  end
157
176
 
158
177
  def many(name, init_block=nil, &block)
159
178
  raise ManyInManyError.new "do not nest many in many" if collection?
160
- child_resource(name, false, true, init_block, &block)
179
+ child_resource(name, false, true, init_block, &(block||Proc.new{ }))
180
+ end
181
+
182
+ def default_init_block_singleton
183
+ if parent
184
+ Proc.new{ instance_variable_get("@#{@__resource.parent.instance_variable_name}").send(@__resource.name) }
185
+ else
186
+ Proc.new { nil }
187
+ end
188
+ end
189
+
190
+ def default_init_block_many
191
+ if parent
192
+ Proc.new{ instance_variable_get("@#{@__resource.parent.instance_variable_name}").send(@__resource.name) }
193
+ else
194
+ Proc.new { nil }
195
+ end
161
196
  end
162
197
 
163
- def one(name=nil, init_block=nil, &block)
164
- raise OneWithNameInManyError.new("call one (#{name}) without name argument when nested in a many (#{@name})") if name && collection?
165
- child_resource(name, false, false, init_block, &block)
198
+ def default_init_block_one
199
+ if parent
200
+ Proc.new do
201
+ instance_variable_get("@#{@__resource.parent.instance_variable_name}")
202
+ .where(id: params[:"#{@__resource.parent.name.to_s.singularize.to_sym}_id"])
203
+ .first
204
+ end
205
+ else
206
+ Proc.new { nil }
207
+ end
208
+ end
209
+
210
+ def one(&block)
211
+ raise "calling one() is only allowed within many() resource" unless collection?
212
+ child_resource(self.name.to_s.singularize.to_sym, false, false, nil, &(block||Proc.new{ }))
166
213
  end
167
214
 
168
215
  def child_resource(name, singleton, collection, init_block, &block)
@@ -172,13 +219,7 @@ module Nested
172
219
  end
173
220
 
174
221
  def instance_variable_name
175
- if @name
176
- @name.to_s.send(collection? ? :pluralize : :singularize).to_sym
177
- elsif member? && @parent
178
- @parent.name.to_s.singularize.to_sym
179
- else
180
- nil
181
- end
222
+ member? ? parent.name.to_s.singularize.to_sym : @name
182
223
  end
183
224
 
184
225
  def parents
@@ -198,7 +239,22 @@ module Nested
198
239
 
199
240
  def sinatra_init(sinatra)
200
241
  sinatra.instance_variable_set("@__resource", self)
201
- sinatra.instance_exec(&@__init)
242
+
243
+ init_block = if @init_block
244
+ @init_block
245
+ else
246
+ if singleton?
247
+ default_init_block_singleton
248
+ elsif collection?
249
+ default_init_block_many
250
+ else
251
+ default_init_block_one
252
+ end
253
+ end
254
+
255
+ sinatra.instance_variable_set("@#{self.instance_variable_name}", sinatra.instance_exec(&init_block))
256
+
257
+ @run_blocks.each{|e| sinatra.instance_exec(&e)}
202
258
  end
203
259
 
204
260
  def sinatra_exec_get_block(sinatra, &block)
@@ -267,9 +323,9 @@ module Nested
267
323
 
268
324
  def sinatra_response_create_data(sinatra, response, method)
269
325
  data = if response && (collection? || response.is_a?(Array)) && method != :post
270
- response.to_a.map{|e| sinatra.instance_exec(e, &@__serialize) }
326
+ response.to_a.map{|e| sinatra.instance_exec(e, &@serializer.serialize) }
271
327
  else
272
- sinatra.instance_exec(response, &@__serialize)
328
+ sinatra.instance_exec(response, &@serializer.serialize)
273
329
  end
274
330
 
275
331
  {data: data, ok: true}
@@ -292,8 +348,7 @@ module Nested
292
348
 
293
349
  resource = self
294
350
 
295
- route = resource.route({}, action)
296
- puts "sinatra router [#{method}] #{@sinatra.nested_config[:prefix]}#{route}"
351
+ route = resource.route(action)
297
352
 
298
353
  @sinatra.send(method, route) do
299
354
  def self.error(message)
@@ -319,10 +374,19 @@ module Nested
319
374
  context_arr << "route: #{route}"
320
375
  context_arr << "method: #{method}"
321
376
  context_arr << "action: #{action}"
322
- context_arr << "resource: #{resource.name} (#{resource.type})"
323
377
 
378
+ context_arr << "resource: #{resource.name} (#{resource.type})"
324
379
  resource_object = instance_variable_get("@#{resource.instance_variable_name}")
325
380
  context_arr << "@#{resource.instance_variable_name}: #{resource_object.inspect}"
381
+
382
+ parent = resource.try(:parent)
383
+
384
+ if parent
385
+ context_arr << "parent: #{parent.try(:name)} (#{parent.try(:type)})"
386
+ parent_object = instance_variable_get("@#{parent.try(:instance_variable_name)}")
387
+ context_arr << "@#{parent.try(:instance_variable_name)}: #{parent_object.inspect}"
388
+ end
389
+
326
390
  puts context_arr.join("\n")
327
391
  raise e
328
392
  end
@@ -365,7 +429,7 @@ module Nested
365
429
  memo[:"#{e}_id"] = "'+(typeof(values[#{idx}]) == 'number' ? values[#{idx}].toString() : (values[#{idx}].id || values[#{idx}]))+'"
366
430
  memo
367
431
  end
368
- route = "#{self.nested_config[:prefix]}" + resource.route(route_args, action)
432
+ route = "#{self.nested_config[:prefix]}" + resource.route_replace(resource.route(action), route_args)
369
433
  when_args = args.map{|a| "$q.when(#{a})"}
370
434
 
371
435
  js << " impl.#{fun_name}Url = function(#{args.join(',')}){"
@@ -457,29 +521,21 @@ module Nested
457
521
  arr << "create" if method == :post
458
522
  arr << "destroy" if method == :delete
459
523
 
460
- parents = resource.parents
461
- parents.each_with_index do |p, idx|
462
- if p.collection? && method != :post && ((parents[idx + 1] && parents[idx + 1].singleton?) || parents.last == p)
463
- arr << p.name.to_s.send(:pluralize)
464
- else
465
- arr << p.name.to_s.send(:singularize)
466
- end
467
- end
524
+ all = resource.self_and_parents.reverse
468
525
 
469
- if resource.member?
470
- if resource.parent
471
- arr = arr.slice(0...-1)
472
- arr << resource.parent.name.to_s.send(:singularize)
473
- else
474
- arr << resource.name.to_s.send(:singularize)
475
- end
476
- elsif resource.singleton?
477
- arr << resource.name.to_s.send(:singularize)
478
- elsif resource.collection?
479
- if method == :post
480
- arr << resource.name.to_s.send(:singularize)
526
+ all.each do |e|
527
+ if e.collection?
528
+ if e == all.last
529
+ if method == :post
530
+ arr << e.name.to_s.singularize.to_sym
531
+ else
532
+ arr << e.name
533
+ end
534
+ else
535
+ arr << e.name unless all[all.index(e) + 1].member?
536
+ end
481
537
  else
482
- arr << resource.name.to_s.send(:pluralize)
538
+ arr << e.name
483
539
  end
484
540
  end
485
541
 
@@ -512,9 +568,6 @@ module Nested
512
568
  def many(name, init_block=nil, &block)
513
569
  create_resource(name, false, true, init_block, &block)
514
570
  end
515
- def one(name, init_block=nil, &block)
516
- create_resource(name, false, false, init_block, &block)
517
- end
518
571
  def create_resource(name, singleton, collection, init_block, &block)
519
572
  ::Nested::Resource.new(self, name, singleton, collection, nil, init_block).tap{|r| r.instance_eval(&block) }
520
573
  end
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.24"
3
+ s.version = "0.0.25"
4
4
 
5
5
  s.authors = ["Jan Zimmek"]
6
6
  s.email = %q{jan.zimmek@web.de}
data/test/nested_test.rb CHANGED
@@ -4,20 +4,11 @@ require "active_support/all"
4
4
  require "active_model/errors"
5
5
  require "active_record"
6
6
  require "nested"
7
+ require "./test/test_helper"
7
8
 
8
9
  class NestedTest < Test::Unit::TestCase
9
10
 
10
- def singleton!
11
- @r = Nested::Resource.new(@sinatra, :project, true, false, nil, nil)
12
- end
13
-
14
- def collection!
15
- @r = Nested::Resource.new(@sinatra, :project, false, true, nil, nil)
16
- end
17
-
18
- def member!
19
- @r = Nested::Resource.new(@sinatra, :project, false, false, nil, nil)
20
- end
11
+ include TestHelper
21
12
 
22
13
  def setup
23
14
  @sinatra = mock
@@ -34,481 +25,334 @@ class NestedTest < Test::Unit::TestCase
34
25
  end
35
26
 
36
27
  def test_is_singleton
37
- singleton!
38
- assert_equal true, @r.singleton?
39
-
40
- collection!
41
- assert_equal false, @r.singleton?
28
+ assert_equal true, singleton(:project).singleton?
29
+ assert_equal false, many(:projects).singleton?
30
+ assert_equal false, many(:projects).one.singleton?
42
31
  end
43
32
 
44
33
  def test_is_collection
45
- collection!
46
- assert_equal true, @r.collection?
47
-
48
- singleton!
49
- assert_equal false, @r.collection?
34
+ assert_equal true, many(:projects).collection?
35
+ assert_equal false, many(:projects).one.collection?
36
+ assert_equal false, singleton(:project).collection?
50
37
  end
51
38
 
52
39
  def test_is_member
53
- member!
54
- assert_equal true, @r.member?
55
-
56
- singleton!
57
- assert_equal false, @r.member?
58
-
59
- collection!
60
- assert_equal false, @r.member?
40
+ assert_equal true, many(:projects).one.member?
41
+ assert_equal false, many(:projects).member?
42
+ assert_equal false, singleton(:project).member?
61
43
  end
62
44
 
63
- def test_init
64
- singleton!
65
-
66
- init = -> { }
67
- @r.init &init
68
-
69
- assert_equal init, @r.instance_variable_get("@__init")
70
- end
45
+ # def test_init
46
+ # init = ->{ }
47
+ # assert_equal init, singleton(:project).init(&init).instance_variable_get("@__init")
48
+ # end
71
49
 
72
50
  def test_serialize
73
- singleton!
51
+ serializer = mock()
74
52
 
75
- @r.serialize :name
53
+ r = singleton(:project)
54
+ r.stubs(:serializer).returns(serializer)
76
55
 
77
- assert_equal({name: :joe}, @r.instance_variable_get("@__serialize").call({name: :joe, test: true}))
78
- end
56
+ assert_equal serializer, r.serialize
79
57
 
80
- def test_route
81
- # no parent
82
- singleton!
83
- assert_equal "/project", @r.route
84
-
85
- collection!
86
- assert_equal "/projects", @r.route
87
-
88
- member!
89
- assert_equal "/projects/:project_id", @r.route
90
- assert_equal "/projects/1", @r.route(project_id: 1)
91
-
92
- member!
93
- assert_equal "/projects/:project_id/myaction", @r.route({}, :myaction)
94
- assert_equal "/projects/1/myaction", @r.route({project_id: 1}, :myaction)
58
+ serializer.expects(:+).with(:name)
59
+ r.serialize :name
60
+ end
95
61
 
96
- # --- singleton
62
+ def test_route_replace
63
+ resource = many(:projects).one
97
64
 
98
- singleton!
99
- @r2 = @r.singleton(:statistic) { }
100
- assert_equal "/project/statistic", @r2.route
65
+ assert_equal "/projects/1", resource.route_replace(resource.route, project_id: 1)
66
+ assert_equal "/projects/1/myaction", resource.route_replace(resource.route(:myaction), project_id: 1)
101
67
 
102
- singleton!
103
- @r2 = @r.many(:statistics) { }
104
- assert_equal "/project/statistics", @r2.route
68
+ resource = singleton(:project).many(:statistics).one
69
+ assert_equal "/project/statistics/1", resource.route_replace(resource.route, statistic_id: 1)
105
70
 
106
- singleton!
107
- @r2 = @r.one(:statistic) { }
108
- assert_equal "/project/statistics/:statistic_id", @r2.route
109
- assert_equal "/project/statistics/1", @r2.route(statistic_id: 1)
71
+ resource = singleton(:project).many(:statistics).one.singleton(:today)
72
+ assert_equal "/project/statistics/1/today", resource.route_replace(resource.route, statistic_id: 1)
110
73
 
111
- # --- collection
74
+ resource = singleton(:project).many(:statistics).one.many(:entries)
75
+ assert_equal "/project/statistics/1/entries", resource.route_replace(resource.route, statistic_id: 1)
112
76
 
113
- collection!
114
- @r2 = @r.singleton(:statistic) { }
115
- assert_equal "/projects/statistic", @r2.route
77
+ resource = singleton(:project).many(:statistics).one.many(:entries).one
78
+ assert_equal "/project/statistics/1/entries/2", resource.route_replace(resource.route, statistic_id: 1, entry_id: 2)
79
+ end
116
80
 
117
- collection!
118
- @r2 = @r.one { }
119
- assert_equal "/projects/:project_id", @r2.route
120
- assert_equal "/projects/1", @r2.route(project_id: 1)
81
+ def test_route
82
+ assert_equal "/project", singleton(:project).route
83
+ assert_equal "/projects", many(:projects).route
121
84
 
122
- # --- member
85
+ assert_equal "/projects/:project_id", many(:projects).one.route
86
+ assert_equal "/projects/:project_id/myaction", many(:projects).one.route(:myaction)
123
87
 
124
- member!
125
- @r2 = @r.singleton(:statistic) { }
126
- assert_equal "/projects/:project_id/statistic", @r2.route
127
- assert_equal "/projects/1/statistic", @r2.route(project_id: 1)
88
+ assert_equal "/project/statistic", singleton(:project).singleton(:statistic).route
89
+ assert_equal "/project/statistics", singleton(:project).many(:statistics).route
128
90
 
129
- member!
130
- @r2 = @r.many(:statistic) { }
131
- assert_equal "/projects/:project_id/statistics", @r2.route
132
- assert_equal "/projects/1/statistics", @r2.route(project_id: 1)
91
+ assert_equal "/project/statistics/:statistic_id", singleton(:project).many(:statistics).one.route
92
+ assert_equal "/project/statistics/:statistic_id/today", singleton(:project).many(:statistics).one.singleton(:today).route
133
93
 
134
- member!
135
- @r2 = @r.one(:statistic) { }
136
- assert_equal "/projects/:project_id/statistics/:statistic_id", @r2.route
137
- assert_equal "/projects/1/statistics/2", @r2.route(project_id: 1, statistic_id: 2)
94
+ assert_equal "/project/statistics/:statistic_id/entries", singleton(:project).many(:statistics).one.many(:entries).route
95
+ assert_equal "/project/statistics/:statistic_id/entries/:entry_id", singleton(:project).many(:statistics).one.many(:entries).one.route
138
96
  end
139
97
 
140
98
 
141
99
  def test_singleton
142
- singleton!
143
- @r.expects(:child_resource).with(:statistic, true, false, nil)
144
- @r.singleton(:statistic)
100
+ resource = singleton(:project)
101
+ resource.expects(:child_resource).with(:statistic, true, false, nil)
102
+ resource.singleton(:statistic)
145
103
 
146
- member!
147
- @r.expects(:child_resource).with(:statistic, true, false, nil)
148
- @r.singleton(:statistic)
104
+ resource = many(:projects)
105
+ resource.expects(:child_resource).with(:statistic, true, false, nil)
106
+ resource.singleton(:statistic)
149
107
 
150
- collection!
151
- @r.expects(:child_resource).with(:statistic, true, false, nil)
152
- @r.singleton(:statistic)
108
+ resource = many(:projects).one
109
+ resource.expects(:child_resource).with(:statistic, true, false, nil)
110
+ resource.singleton(:statistic)
153
111
  end
154
112
 
155
113
  def test_one
156
- singleton!
157
- @r.expects(:child_resource).with(:statistic, false, false, nil)
158
- @r.one(:statistic)
159
-
160
- member!
161
- @r.expects(:child_resource).with(:statistic, false, false, nil)
162
- @r.one(:statistic)
163
-
164
- collection!
165
- @r.expects(:child_resource).with(nil, false, false, nil)
166
- @r.one
167
-
168
- collection!
169
- assert_raise ::Nested::OneWithNameInManyError do
170
- @r.one :statistic
171
- end
114
+ resource = many(:projects)
115
+ resource.expects(:child_resource).with(:project, false, false, nil)
116
+ resource.one
172
117
  end
173
118
 
174
119
  def test_many
175
- singleton!
176
- @r.expects(:child_resource).with(:statistics, false, true, nil)
177
- @r.many(:statistics)
178
-
179
- member!
180
- @r.expects(:child_resource).with(:statistics, false, true, nil)
181
- @r.many(:statistics)
120
+ resource = singleton(:project)
121
+ resource.expects(:child_resource).with(:statistics, false, true, nil)
122
+ resource.many(:statistics)
182
123
 
183
- collection!
184
- assert_raise ::Nested::ManyInManyError do
185
- @r.many :statistic
186
- end
124
+ resource = singleton(:project).many(:statistics).one
125
+ resource.expects(:child_resource).with(:entries, false, true, nil)
126
+ resource.many(:entries)
187
127
  end
188
128
 
189
129
  def test_get
190
- singleton!
130
+ resource = singleton(:project)
191
131
 
192
- @r.expects(:create_sinatra_route).with(:get, nil)
193
- @r.get
132
+ resource.expects(:create_sinatra_route).with(:get, nil)
133
+ resource.get
194
134
 
195
- @r.expects(:create_sinatra_route).with(:get, :action)
196
- @r.get :action
135
+ resource.expects(:create_sinatra_route).with(:get, :action)
136
+ resource.get :action
197
137
  end
198
138
 
199
139
  def test_post
200
- singleton!
140
+ resource = singleton(:project)
201
141
 
202
- @r.expects(:create_sinatra_route).with(:post, nil)
203
- @r.post
142
+ resource.expects(:create_sinatra_route).with(:post, nil)
143
+ resource.post
204
144
 
205
- @r.expects(:create_sinatra_route).with(:post, :action)
206
- @r.post :action
145
+ resource.expects(:create_sinatra_route).with(:post, :action)
146
+ resource.post :action
207
147
  end
208
148
 
209
149
  def test_put
210
- singleton!
150
+ resource = singleton(:project)
211
151
 
212
- @r.expects(:create_sinatra_route).with(:put, nil)
213
- @r.put
152
+ resource.expects(:create_sinatra_route).with(:put, nil)
153
+ resource.put
214
154
 
215
- @r.expects(:create_sinatra_route).with(:put, :action)
216
- @r.put :action
155
+ resource.expects(:create_sinatra_route).with(:put, :action)
156
+ resource.put :action
217
157
  end
218
158
 
219
159
  def test_delete
220
- singleton!
160
+ resource = singleton(:project)
221
161
 
222
- @r.expects(:create_sinatra_route).with(:delete, nil)
223
- @r.delete
162
+ resource.expects(:create_sinatra_route).with(:delete, nil)
163
+ resource.delete
224
164
 
225
- @r.expects(:create_sinatra_route).with(:delete, :action)
226
- @r.delete :action
165
+ resource.expects(:create_sinatra_route).with(:delete, :action)
166
+ resource.delete :action
227
167
  end
228
168
 
229
169
  def test_child_resource
230
- singleton!
231
- r = @r.child_resource(:statistic, false, false, nil) { }
232
- assert_equal :statistic, r.name
233
- assert_equal false, r.instance_variable_get("@singleton")
234
- assert_equal false, r.instance_variable_get("@collection")
235
-
236
- singleton!
237
- r = @r.child_resource(:statistic, true, false, nil) { }
238
- assert_equal :statistic, r.name
239
- assert_equal true, r.instance_variable_get("@singleton")
240
- assert_equal false, r.instance_variable_get("@collection")
241
-
242
- singleton!
243
- r = @r.child_resource(:statistic, false, true, nil) { }
244
- assert_equal :statistic, r.name
245
- assert_equal false, r.instance_variable_get("@singleton")
246
- assert_equal true, r.instance_variable_get("@collection")
247
-
248
- singleton!
249
- assert_raise Nested::SingletonAndCollectionError do
250
- @r.child_resource(:statistic, true, true, nil) { }
251
- end
170
+ resource = singleton(:project).child_resource(:statistic, false, false, nil) { }
171
+ assert_equal :statistic, resource.name
172
+ assert_equal false, resource.instance_variable_get("@singleton")
173
+ assert_equal false, resource.instance_variable_get("@collection")
174
+
175
+ resource = singleton(:project).child_resource(:statistic, true, false, nil) { }
176
+ assert_equal :statistic, resource.name
177
+ assert_equal true, resource.instance_variable_get("@singleton")
178
+ assert_equal false, resource.instance_variable_get("@collection")
179
+
180
+ resource = singleton(:project).child_resource(:statistic, false, true, nil) { }
181
+ assert_equal :statistic, resource.name
182
+ assert_equal false, resource.instance_variable_get("@singleton")
183
+ assert_equal true, resource.instance_variable_get("@collection")
252
184
  end
253
185
 
254
186
  def test_instance_variable_name
255
- singleton!
256
- assert_equal :project, @r.instance_variable_name
257
-
258
- member!
259
- assert_equal :project, @r.instance_variable_name
260
-
261
- collection!
262
- assert_equal :projects, @r.instance_variable_name
263
-
264
- collection!
265
- r2 = @r.one {}
266
- assert_equal :project, r2.instance_variable_name
187
+ assert_equal :project, singleton(:project).instance_variable_name
188
+ assert_equal :projects, many(:projects).instance_variable_name
189
+ assert_equal :project, many(:projects).one.instance_variable_name
267
190
  end
268
191
 
269
192
  def test_parents
270
- singleton!
271
- assert_equal [], @r.parents
193
+ r1 = singleton(:project)
194
+ r2 = r1.many(:todos)
195
+ r3 = r2.one
272
196
 
273
- r2 = @r.singleton(:statistic) { }
274
- assert_equal [@r], r2.parents
275
-
276
- r3 = r2.singleton(:statistic) { }
277
- assert_equal [@r, r2], r3.parents
197
+ assert_equal [], r1.parents
198
+ assert_equal [r1], r2.parents
199
+ assert_equal [r1, r2], r3.parents
278
200
  end
279
201
 
280
202
  def test_self_and_parents
281
- singleton!
282
- assert_equal [@r], @r.self_and_parents
283
-
284
- r2 = @r.singleton(:statistic) { }
285
- assert_equal [r2, @r], r2.self_and_parents
203
+ r1 = singleton(:project)
204
+ r2 = r1.many(:todos)
205
+ r3 = r2.one
286
206
 
287
- r3 = r2.singleton(:statistic) { }
288
- assert_equal [r3, r2, @r], r3.self_and_parents
207
+ assert_equal [r1], r1.self_and_parents
208
+ assert_equal [r2, r1], r2.self_and_parents
209
+ assert_equal [r3, r2, r1], r3.self_and_parents
289
210
  end
290
211
 
291
212
  def test_create_sinatra_route
292
- @sinatra.expects(:nested_config).at_least_once.returns({})
293
-
294
- singleton!
213
+ # get
295
214
 
296
215
  @sinatra.expects(:send).with(:get, "/project")
297
- block = ->{ }
298
- @r.create_sinatra_route(:get, nil, &block)
299
- assert_equal [{method: :get, action: nil, block: block}], @r.actions
216
+ singleton(:project).get
300
217
 
301
- singleton!
218
+ @sinatra.expects(:send).with(:get, "/project/action")
219
+ singleton(:project).get(:action)
302
220
 
303
- @sinatra.expects(:send).with(:post, "/project")
304
- @r.create_sinatra_route(:post, nil, &block)
305
- assert_equal [{method: :post, action: nil, block: block}], @r.actions
221
+ # post
306
222
 
307
- singleton!
223
+ @sinatra.expects(:send).with(:post, "/project")
224
+ singleton(:project).post
308
225
 
309
226
  @sinatra.expects(:send).with(:post, "/project/action")
310
- @r.create_sinatra_route(:post, :action, &block)
311
- assert_equal [{method: :post, action: :action, block: block}], @r.actions
312
- end
227
+ singleton(:project).post(:action)
313
228
 
314
- def test_serializer
315
- singleton!
229
+ # put
316
230
 
317
- @r.serialize :name
231
+ @sinatra.expects(:send).with(:put, "/project")
232
+ singleton(:project).put
318
233
 
319
- assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
320
- assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe", boss: true}))
321
- assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call(OpenStruct.new({name: "joe"})))
234
+ @sinatra.expects(:send).with(:put, "/project/action")
235
+ singleton(:project).put(:action)
322
236
 
323
- @r.serialize :name, virtual: ->(o){ o[:name] + "!!" }
324
- assert_equal({name: "joe", virtual: "joe!!"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
325
- end
326
-
327
- # ----
237
+ # delete
328
238
 
329
- def test_sinatra_response_type
330
- singleton!
331
- assert_equal :error, @r.sinatra_response_type(ActiveModel::Errors.new({}))
332
-
333
- obj = OpenStruct.new(errors: ActiveModel::Errors.new({}))
334
- assert_equal :data, @r.sinatra_response_type(obj)
239
+ @sinatra.expects(:send).with(:delete, "/project")
240
+ singleton(:project).delete
335
241
 
336
- obj.errors.add(:somefield, "some error")
337
- assert_equal :error, @r.sinatra_response_type(obj)
338
-
339
- assert_equal :data, @r.sinatra_response_type(nil)
340
- assert_equal :data, @r.sinatra_response_type(123)
242
+ @sinatra.expects(:send).with(:delete, "/project/action")
243
+ singleton(:project).delete(:action)
341
244
  end
342
245
 
343
246
 
344
- # ----
345
-
346
-
347
- def test_function_name
348
- singleton!
349
- assert_equal "project", Nested::JsUtil::generate_function_name(@r, :get, nil)
350
- assert_equal "updateProject", Nested::JsUtil::generate_function_name(@r, :put, nil)
351
- assert_equal "createProject", Nested::JsUtil::generate_function_name(@r, :post, nil)
352
- assert_equal "destroyProject", Nested::JsUtil::generate_function_name(@r, :delete, nil)
353
-
354
- assert_equal "projectAction", Nested::JsUtil::generate_function_name(@r, :get, :action)
355
- assert_equal "updateProjectAction", Nested::JsUtil::generate_function_name(@r, :put, :action)
356
- assert_equal "createProjectAction", Nested::JsUtil::generate_function_name(@r, :post, :action)
357
- assert_equal "destroyProjectAction", Nested::JsUtil::generate_function_name(@r, :delete, :action)
358
-
359
- collection!
360
- assert_equal "projects", Nested::JsUtil::generate_function_name(@r, :get, nil)
361
- assert_equal "updateProjects", Nested::JsUtil::generate_function_name(@r, :put, nil)
362
- assert_equal "createProject", Nested::JsUtil::generate_function_name(@r, :post, nil)
363
- assert_equal "destroyProjects", Nested::JsUtil::generate_function_name(@r, :delete, nil)
364
-
365
- assert_equal "projectsAction", Nested::JsUtil::generate_function_name(@r, :get, :action)
366
- assert_equal "updateProjectsAction", Nested::JsUtil::generate_function_name(@r, :put, :action)
367
- assert_equal "createProjectAction", Nested::JsUtil::generate_function_name(@r, :post, :action)
368
- assert_equal "destroyProjectsAction", Nested::JsUtil::generate_function_name(@r, :delete, :action)
369
-
370
- member!
371
- assert_equal "project", Nested::JsUtil::generate_function_name(@r, :get, nil)
372
- assert_equal "updateProject", Nested::JsUtil::generate_function_name(@r, :put, nil)
373
- assert_equal "createProject", Nested::JsUtil::generate_function_name(@r, :post, nil)
374
- assert_equal "destroyProject", Nested::JsUtil::generate_function_name(@r, :delete, nil)
247
+ # def test_serializer
248
+ # singleton!
375
249
 
376
- assert_equal "projectAction", Nested::JsUtil::generate_function_name(@r, :get, :action)
377
- assert_equal "updateProjectAction", Nested::JsUtil::generate_function_name(@r, :put, :action)
378
- assert_equal "createProjectAction", Nested::JsUtil::generate_function_name(@r, :post, :action)
379
- assert_equal "destroyProjectAction", Nested::JsUtil::generate_function_name(@r, :delete, :action)
250
+ # @r.serialize :name
380
251
 
252
+ # assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
253
+ # assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe", boss: true}))
254
+ # assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call(OpenStruct.new({name: "joe"})))
381
255
 
382
- # with parent
256
+ # @r.serialize :name, virtual: ->(o){ o[:name] + "!!" }
257
+ # assert_equal({name: "joe", virtual: "joe!!"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
258
+ # end
383
259
 
384
- singleton!
260
+ # # ----
385
261
 
386
- r2 = @r.singleton(:statistic) {}
262
+ def test_sinatra_response_type
263
+ assert_equal :error, singleton(:project).sinatra_response_type(ActiveModel::Errors.new({}))
387
264
 
388
- assert_equal "projectStatistic", Nested::JsUtil::generate_function_name(r2, :get, nil)
389
- assert_equal "updateProjectStatistic", Nested::JsUtil::generate_function_name(r2, :put, nil)
390
- assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(r2, :post, nil)
391
- assert_equal "destroyProjectStatistic", Nested::JsUtil::generate_function_name(r2, :delete, nil)
265
+ obj = OpenStruct.new(errors: ActiveModel::Errors.new({}))
266
+ assert_equal :data, singleton(:project).sinatra_response_type(obj)
392
267
 
393
- assert_equal "projectStatisticAction", Nested::JsUtil::generate_function_name(r2, :get, :action)
394
- assert_equal "updateProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :put, :action)
395
- assert_equal "createProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :post, :action)
396
- assert_equal "destroyProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :delete, :action)
268
+ obj.errors.add(:somefield, "some error")
269
+ assert_equal :error, singleton(:project).sinatra_response_type(obj)
397
270
 
398
- member!
271
+ assert_equal :data, singleton(:project).sinatra_response_type(nil)
272
+ assert_equal :data, singleton(:project).sinatra_response_type(123)
273
+ end
399
274
 
400
- r2 = @r.singleton(:statistic) {}
401
275
 
402
- assert_equal "projectStatistic", Nested::JsUtil::generate_function_name(r2, :get, nil)
403
- assert_equal "updateProjectStatistic", Nested::JsUtil::generate_function_name(r2, :put, nil)
404
- assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(r2, :post, nil)
405
- assert_equal "destroyProjectStatistic", Nested::JsUtil::generate_function_name(r2, :delete, nil)
276
+ # # ----
406
277
 
407
- assert_equal "projectStatisticAction", Nested::JsUtil::generate_function_name(r2, :get, :action)
408
- assert_equal "updateProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :put, :action)
409
- assert_equal "createProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :post, :action)
410
- assert_equal "destroyProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :delete, :action)
278
+ def test_function_name
279
+ resource = singleton(:project)
411
280
 
412
- collection!
281
+ assert_equal "project", Nested::JsUtil::generate_function_name(resource, :get, nil)
282
+ assert_equal "updateProject", Nested::JsUtil::generate_function_name(resource, :put, nil)
283
+ assert_equal "createProject", Nested::JsUtil::generate_function_name(resource, :post, nil)
284
+ assert_equal "destroyProject", Nested::JsUtil::generate_function_name(resource, :delete, nil)
413
285
 
414
- r2 = @r.singleton(:statistic) {}
286
+ assert_equal "projectAction", Nested::JsUtil::generate_function_name(resource, :get, :action)
287
+ assert_equal "updateProjectAction", Nested::JsUtil::generate_function_name(resource, :put, :action)
288
+ assert_equal "createProjectAction", Nested::JsUtil::generate_function_name(resource, :post, :action)
289
+ assert_equal "destroyProjectAction", Nested::JsUtil::generate_function_name(resource, :delete, :action)
415
290
 
416
- assert_equal "projectsStatistic", Nested::JsUtil::generate_function_name(r2, :get, nil)
417
- assert_equal "updateProjectsStatistic", Nested::JsUtil::generate_function_name(r2, :put, nil)
418
- assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(r2, :post, nil)
419
- assert_equal "destroyProjectsStatistic", Nested::JsUtil::generate_function_name(r2, :delete, nil)
291
+ resource = many(:projects)
420
292
 
421
- assert_equal "projectsStatisticAction", Nested::JsUtil::generate_function_name(r2, :get, :action)
422
- assert_equal "updateProjectsStatisticAction", Nested::JsUtil::generate_function_name(r2, :put, :action)
423
- assert_equal "createProjectStatisticAction", Nested::JsUtil::generate_function_name(r2, :post, :action)
424
- assert_equal "destroyProjectsStatisticAction", Nested::JsUtil::generate_function_name(r2, :delete, :action)
293
+ assert_equal "projects", Nested::JsUtil::generate_function_name(resource, :get, nil)
294
+ assert_equal "updateProjects", Nested::JsUtil::generate_function_name(resource, :put, nil)
295
+ assert_equal "createProject", Nested::JsUtil::generate_function_name(resource, :post, nil)
296
+ assert_equal "destroyProjects", Nested::JsUtil::generate_function_name(resource, :delete, nil)
425
297
 
426
- singleton!
298
+ assert_equal "projectsAction", Nested::JsUtil::generate_function_name(resource, :get, :action)
299
+ assert_equal "updateProjectsAction", Nested::JsUtil::generate_function_name(resource, :put, :action)
300
+ assert_equal "createProjectAction", Nested::JsUtil::generate_function_name(resource, :post, :action)
301
+ assert_equal "destroyProjectsAction", Nested::JsUtil::generate_function_name(resource, :delete, :action)
427
302
 
428
- r2 = @r.many(:statistics) {}
429
- r3 = r2.one {}
303
+ resource = many(:projects).one
430
304
 
431
- assert_equal "projectStatistic", Nested::JsUtil::generate_function_name(r3, :get, nil)
432
- assert_equal "updateProjectStatistic", Nested::JsUtil::generate_function_name(r3, :put, nil)
433
- assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(r3, :post, nil)
434
- assert_equal "destroyProjectStatistic", Nested::JsUtil::generate_function_name(r3, :delete, nil)
305
+ assert_equal "project", Nested::JsUtil::generate_function_name(resource, :get, nil)
306
+ assert_equal "updateProject", Nested::JsUtil::generate_function_name(resource, :put, nil)
307
+ assert_equal "createProject", Nested::JsUtil::generate_function_name(resource, :post, nil)
308
+ assert_equal "destroyProject", Nested::JsUtil::generate_function_name(resource, :delete, nil)
435
309
 
436
- assert_equal "projectStatisticAction", Nested::JsUtil::generate_function_name(r3, :get, :action)
437
- assert_equal "updateProjectStatisticAction", Nested::JsUtil::generate_function_name(r3, :put, :action)
438
- assert_equal "createProjectStatisticAction", Nested::JsUtil::generate_function_name(r3, :post, :action)
439
- assert_equal "destroyProjectStatisticAction", Nested::JsUtil::generate_function_name(r3, :delete, :action)
310
+ assert_equal "projectAction", Nested::JsUtil::generate_function_name(resource, :get, :action)
311
+ assert_equal "updateProjectAction", Nested::JsUtil::generate_function_name(resource, :put, :action)
312
+ assert_equal "createProjectAction", Nested::JsUtil::generate_function_name(resource, :post, :action)
313
+ assert_equal "destroyProjectAction", Nested::JsUtil::generate_function_name(resource, :delete, :action)
440
314
 
315
+ resource = singleton(:project).many(:statistics)
441
316
 
442
- singleton!
443
- r2 = @r.many(:statistics) {}
444
- assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(r2, :post, nil)
317
+ assert_equal "projectStatistics", Nested::JsUtil::generate_function_name(resource, :get, nil)
318
+ assert_equal "updateProjectStatistics", Nested::JsUtil::generate_function_name(resource, :put, nil)
319
+ assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(resource, :post, nil)
320
+ assert_equal "destroyProjectStatistics", Nested::JsUtil::generate_function_name(resource, :delete, nil)
445
321
 
322
+ assert_equal "projectStatisticsAction", Nested::JsUtil::generate_function_name(resource, :get, :action)
323
+ assert_equal "updateProjectStatisticsAction", Nested::JsUtil::generate_function_name(resource, :put, :action)
324
+ assert_equal "createProjectStatisticAction", Nested::JsUtil::generate_function_name(resource, :post, :action)
325
+ assert_equal "destroyProjectStatisticsAction", Nested::JsUtil::generate_function_name(resource, :delete, :action)
446
326
 
447
- singleton!
448
- r2 = @r.many(:statistics) {}
449
- r3 = r2.singleton(:user) {}
327
+ resource = singleton(:project).many(:statistics).one
450
328
 
451
- assert_equal "projectStatisticsUser", Nested::JsUtil::generate_function_name(r3, :get, nil)
329
+ assert_equal "projectStatistic", Nested::JsUtil::generate_function_name(resource, :get, nil)
330
+ assert_equal "updateProjectStatistic", Nested::JsUtil::generate_function_name(resource, :put, nil)
331
+ assert_equal "createProjectStatistic", Nested::JsUtil::generate_function_name(resource, :post, nil)
332
+ assert_equal "destroyProjectStatistic", Nested::JsUtil::generate_function_name(resource, :delete, nil)
452
333
 
453
- singleton!
454
- r2 = @r.many(:statistics) {}
455
- r3 = r2.one {}
456
- r4 = r3.singleton(:user) {}
334
+ assert_equal "projectStatisticAction", Nested::JsUtil::generate_function_name(resource, :get, :action)
335
+ assert_equal "updateProjectStatisticAction", Nested::JsUtil::generate_function_name(resource, :put, :action)
336
+ assert_equal "createProjectStatisticAction", Nested::JsUtil::generate_function_name(resource, :post, :action)
337
+ assert_equal "destroyProjectStatisticAction", Nested::JsUtil::generate_function_name(resource, :delete, :action)
457
338
 
458
- assert_equal "projectStatisticUser", Nested::JsUtil::generate_function_name(r4, :get, nil)
339
+ resource = singleton(:project).many(:statistics).one.many(:entries)
340
+ assert_equal "projectStatisticEntries", Nested::JsUtil::generate_function_name(resource, :get, nil)
459
341
  end
460
342
 
461
- # -----------------
462
-
463
343
  def test_function_arguments
464
- # --- singleton
465
- singleton!
466
- assert_equal [], Nested::JsUtil.function_arguments(@r)
467
-
468
- singleton!
469
- assert_equal [], Nested::JsUtil.function_arguments(@r.singleton(:statistic) {})
470
-
471
- singleton!
472
- assert_equal [], Nested::JsUtil.function_arguments(@r.many(:statistics) {})
473
-
474
- singleton!
475
- assert_equal ["statistic"], Nested::JsUtil.function_arguments(@r.one(:statistic) {})
476
-
477
- singleton!
478
- r2 = @r.one(:statistic) {}
479
- r3 = r2.singleton(:test) {}
480
- assert_equal ["statistic"], Nested::JsUtil.function_arguments(r3)
481
-
482
- # --- member
483
-
484
- member!
485
- assert_equal ["project"], Nested::JsUtil.function_arguments(@r)
486
-
487
- member!
488
- assert_equal ["project"], Nested::JsUtil.function_arguments(@r.singleton(:statistic) {})
489
-
490
- member!
491
- assert_equal ["project"], Nested::JsUtil.function_arguments(@r.many(:statistics) {})
492
-
493
- member!
494
- assert_equal ["project", "statistic"], Nested::JsUtil.function_arguments(@r.one(:statistic) {})
495
-
496
- # --- collection
497
-
498
- collection!
499
- assert_equal [], Nested::JsUtil.function_arguments(@r)
500
-
501
- collection!
502
- assert_equal [], Nested::JsUtil.function_arguments(@r.singleton(:statistic) {})
503
-
504
- collection!
505
- assert_equal ["project"], Nested::JsUtil.function_arguments(@r.one {})
506
-
507
- collection!
508
- r2 = @r.one {}
509
- r3 = r2.one(:statistic) {}
510
-
511
- assert_equal ["project", "statistic"], Nested::JsUtil.function_arguments(r3)
344
+ assert_equal [], Nested::JsUtil.function_arguments(singleton(:project))
345
+ assert_equal [], Nested::JsUtil.function_arguments(singleton(:project).singleton(:statistic))
346
+ assert_equal [], Nested::JsUtil.function_arguments(singleton(:project).many(:statistics))
347
+ assert_equal ["statistic"], Nested::JsUtil.function_arguments(singleton(:project).many(:statistics).one)
348
+
349
+ assert_equal [], Nested::JsUtil.function_arguments(many(:projects))
350
+ assert_equal [], Nested::JsUtil.function_arguments(many(:projects).singleton(:statistic))
351
+ assert_equal ["project"], Nested::JsUtil.function_arguments(many(:projects).one)
352
+
353
+ assert_equal ["project"], Nested::JsUtil.function_arguments(many(:projects).one.singleton(:today))
354
+ assert_equal [], Nested::JsUtil.function_arguments(many(:projects).singleton(:statistic).singleton(:today))
355
+ assert_equal ["project", "entry"], Nested::JsUtil.function_arguments(many(:projects).one.many(:entries).one)
512
356
  end
513
357
 
514
358
  end
@@ -0,0 +1,13 @@
1
+ require "test/unit"
2
+ require "mocha/setup"
3
+ require "active_support/all"
4
+ require "active_model/errors"
5
+ require "active_record"
6
+ require "nested"
7
+
8
+ class SerializerFieldTest < Test::Unit::TestCase
9
+ def test_initialize
10
+ assert_equal :name, Nested::SerializerField.new(:name, :condition).name
11
+ assert_equal :condition, Nested::SerializerField.new(:name, :condition).condition
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ require "test/unit"
2
+ require "mocha/setup"
3
+ require "active_support/all"
4
+ require "active_model/errors"
5
+ require "active_record"
6
+ require "nested"
7
+
8
+ class SerializerTest < Test::Unit::TestCase
9
+
10
+ def test_initialize
11
+ assert_equal [], Nested::Serializer.new.includes
12
+ assert_equal [], Nested::Serializer.new.excludes
13
+
14
+ assert_equal [1,2,3], Nested::Serializer.new([1,2,3]).includes
15
+ end
16
+
17
+ def test_operator_plus
18
+ ser = Nested::Serializer.new
19
+ assert_equal ser, ser + :name
20
+ assert_equal 1, ser.includes.length
21
+ assert_equal :name, ser.includes[0].name
22
+ end
23
+
24
+ def test_operator_minus
25
+ ser = Nested::Serializer.new([:name])
26
+ assert_equal ser, ser - :name
27
+ assert_equal 1, ser.excludes.length
28
+ assert_equal :name, ser.excludes[0].name
29
+ end
30
+
31
+ end
@@ -0,0 +1,9 @@
1
+ module TestHelper
2
+ def singleton(name)
3
+ Nested::Resource.new(@sinatra, name, true, false, nil, nil)
4
+ end
5
+
6
+ def many(name)
7
+ Nested::Resource.new(@sinatra, name, false, true, nil, nil)
8
+ end
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.24
4
+ version: 0.0.25
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-01-27 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -81,6 +81,9 @@ files:
81
81
  - lib/nested.rb
82
82
  - nested.gemspec
83
83
  - test/nested_test.rb
84
+ - test/serializer_field_test.rb
85
+ - test/serializer_test.rb
86
+ - test/test_helper.rb
84
87
  homepage:
85
88
  licenses: []
86
89
  metadata: {}
@@ -106,3 +109,6 @@ specification_version: 4
106
109
  summary: a nestable dsl to create a restful api
107
110
  test_files:
108
111
  - test/nested_test.rb
112
+ - test/serializer_field_test.rb
113
+ - test/serializer_test.rb
114
+ - test/test_helper.rb