ddc 0.1.4 → 0.1.5

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: 47db88316f12256bf011cba49ba908240c03ae22
4
- data.tar.gz: 8be0483b5cb162c19e64e1c9e5961f6def051ecc
3
+ metadata.gz: 730dff323e8780de877e38da76e5f9095eb14664
4
+ data.tar.gz: bcebe8c41b6acaad90efb3454b7145dcf629c135
5
5
  SHA512:
6
- metadata.gz: fd48fbdcba04e2d1d3243c43df3e40db755aad4bd514b314f4a2d57e7d2cfadcbb59cb6345eeb213dd3a1338743b2436007b37d3bba6209392e9a95f1d7748e6
7
- data.tar.gz: f3b76c51dc6e8f2f81bdb5f36aae083045fa103bbd7b0045350aa9d46cb9c156a386d1dc666332da98f04279a672918a18e0fdbdd25cbfb906833ca956759901
6
+ metadata.gz: d5e577bfaf5d03c1c7684b0b2b98178a0f384ace150a2ff0e9f011ac727a4eb09bce2574fdcb866e60d4f4d3ea258371517f5d8c36e80c2a18e4f3daac2d8570
7
+ data.tar.gz: 2ab75c7a10cca1ef97d0af014a0b01ab6234ddcd548c94e564cc1486a0ace6ee103f39f10c081d3d4d419562f76991dfef5c5d4e6216b54cf16edc3389f6d76a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # DDC Changelog
2
2
 
3
+ ### 0.1.5
4
+
5
+ * remove :render_opts to use :object_render_opts
6
+ * add :errors_render_opts
7
+
3
8
  ### 0.1.4
4
9
 
5
10
  * remove :serializer and :each_serializer, use :render_opts to pass a hash
@@ -45,18 +45,32 @@ module DDC
45
45
  end
46
46
  end
47
47
 
48
+ def reduce_contexts(contexts, context_params)
49
+ computed_contexts = contexts.map do |context_klass, context_method|
50
+ context_klass.new.send(context_method, context_params)
51
+ end
52
+ if computed_contexts.size == 1
53
+ computed_contexts.first
54
+ else
55
+ computed_contexts.reduce({}) do |h, ctx|
56
+ h.merge(ctx)
57
+ end.with_indifferent_access
58
+ end
59
+ end
60
+
48
61
  def setup_action!(controller_name, klass, action, action_desc, config)
49
62
  raise "Must specify a service for each action" unless action_desc[:service].present?
50
- raise "Must specify a context for each action" unless action_desc[:context].present?
63
+ raise "Must specify a context for each action" unless (action_desc[:context].present? || action_desc[:contexts].present?)
51
64
  proc_klass, proc_method = parse_class_and_method(action_desc[:service])
52
- context_klass, context_method = parse_class_and_method(action_desc[:context])
65
+ contexts = (action_desc[:contexts] || [action_desc[:context]]).map { |ctx| parse_class_and_method ctx }
66
+ #context_klass, context_method = parse_class_and_method(action_desc[:context])
53
67
 
54
68
  klass.send :define_method, action do
55
69
  context_params = (action_desc[:context_params] || config[:context_params] || DEFAULT_CONTEXT_PARAMS).inject({}) do |h, param|
56
70
  h[param] = send param
57
71
  h
58
72
  end
59
- context = context_klass.new.send(context_method, context_params)
73
+ context = DDC::ControllerBuilder.reduce_contexts contexts, context_params
60
74
 
61
75
  result = proc_klass.new.send(proc_method, context)
62
76
  obj = result[:object]
@@ -76,13 +90,14 @@ module DDC
76
90
  respond_to do |format|
77
91
  format.json do
78
92
  if obj.nil?
79
- render_opts = {
80
- json: {errors: errors}, status: status }
93
+ render_opts = { json: {errors: errors}, status: status }
94
+ render_opts.reverse_merge!(action_desc[:error_render_opts]) if action_desc.has_key? :error_render_opts
81
95
  else
82
96
  render_opts = { json: obj, status: status }
97
+ render_opts.reverse_merge!(action_desc[:object_render_opts]) if action_desc.has_key? :object_render_opts
83
98
  end
84
99
 
85
- render_opts = (action_desc[:render_opts]).merge(render_opts) if action_desc.has_key? :render_opts
100
+ render_opts.reverse_merge!(action_desc[:render_opts]) if action_desc.has_key? :render_opts
86
101
  render render_opts
87
102
  end
88
103
  format.html do
data/lib/ddc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ddc
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -54,7 +54,7 @@ describe DDC::ControllerBuilder do
54
54
  end
55
55
 
56
56
  expect(FooController).to receive(:before_action).with(:my_before_action)
57
- subject.build :foo,
57
+ subject.build :foo,
58
58
  before_actions: [:my_before_action],
59
59
  actions: {
60
60
  index: {
@@ -65,7 +65,7 @@ describe DDC::ControllerBuilder do
65
65
 
66
66
  end
67
67
 
68
- it 'sunny day get params, process, return object and status, render' do
68
+ it 'sunny day get params, process, return object and status, render' do
69
69
  class FooController
70
70
  def current_user; end
71
71
  def some_user; end
@@ -89,7 +89,7 @@ describe DDC::ControllerBuilder do
89
89
  current_user: :some_user,
90
90
  params: {a: :b})) { :context }
91
91
 
92
- expect_any_instance_of(BazService).to receive(:qux).with(:context) do
92
+ expect_any_instance_of(BazService).to receive(:qux).with(:context) do
93
93
  { object: :some_obj, status: :ok }
94
94
  end
95
95
 
@@ -137,7 +137,7 @@ describe DDC::ControllerBuilder do
137
137
  current_user: :some_user,
138
138
  params: {a: :b})) { :context }
139
139
 
140
- expect_any_instance_of(BazService).to receive(:qux).with(:context) do
140
+ expect_any_instance_of(BazService).to receive(:qux).with(:context) do
141
141
  { status: :error, errors: ["BOOM"] }
142
142
  end
143
143
 
@@ -151,7 +151,7 @@ describe DDC::ControllerBuilder do
151
151
  def some_user; end
152
152
  def render(args); end
153
153
  end
154
- subject.build :foo,
154
+ subject.build :foo,
155
155
  params: [:current_user, :params],
156
156
  actions: {
157
157
  index: {
@@ -176,51 +176,90 @@ describe DDC::ControllerBuilder do
176
176
  def qux(context) {} end
177
177
  end
178
178
 
179
- it 'uses specified serializer json render calls' do
180
- class FooController
181
- def current_user; end
182
- def some_user; end
183
- def render(args); end
184
- def respond_to; end
185
- end
186
- controller = FooController.new
187
- expect(controller).to receive_messages( params: {a: :b})
179
+ context 'render_opts' do
188
180
 
189
-
190
- subject.build :foo,
191
- actions: {
192
- index: {
193
- context: 'foo_context_builder#bar',
194
- service: 'baz_service#qux',
195
- render_opts: {
196
- serializer: MySerializer
181
+ let (:controller) {
182
+ class FooController
183
+ def current_user; end
184
+ def some_user; end
185
+ def render(args); end
186
+ def respond_to; end
187
+ end
188
+ FooController.new
189
+ }
190
+ before do
191
+ expect(controller).to receive_messages( params: {a: :b})
192
+
193
+ subject.build :foo,
194
+ actions: {
195
+ index: {
196
+ context: 'foo_context_builder#bar',
197
+ service: 'baz_service#qux',
198
+ render_opts: {
199
+ serializer: MySerializer
200
+ }
201
+ },
202
+ show: {
203
+ context: 'foo_context_builder#bar',
204
+ service: 'baz_service#qux',
205
+ render_opts: {
206
+ serializer: MySerializer
207
+ },
208
+ object_render_opts: {
209
+ serializer: MyObjectSerializer
210
+ },
211
+ error_render_opts: {
212
+ serializer: MyErrorSerializer
213
+ },
197
214
  }
198
- }
199
215
 
200
- }
216
+ }
201
217
 
202
- render_args = nil
203
- expect(controller).to receive(:render) do |args|
204
- render_args = args
218
+ @render_args = nil
219
+ expect(controller).to receive(:render) do |args|
220
+ @render_args = args
221
+ end
222
+ expect(controller).to receive(:respond_to) do |&block|
223
+ block.call(json_format)
224
+ end
225
+ expect_any_instance_of(FooContextBuilder).to receive(:bar).with(hash_including(
226
+ params: {a: :b})) { :context }
205
227
  end
206
- expect(controller).to receive(:respond_to) do |&block|
207
- block.call(json_format)
228
+
229
+ it 'uses specified serializer json render calls' do
230
+ expect_any_instance_of(BazService).to receive(:qux).with(:context) do
231
+ { object: :some_obj, status: :ok }
232
+ end
233
+ controller.index
234
+ expect(@render_args[:serializer]).to eq(MySerializer)
208
235
  end
209
- expect_any_instance_of(FooContextBuilder).to receive(:bar).with(hash_including(
210
- params: {a: :b})) { :context }
211
236
 
212
- expect_any_instance_of(BazService).to receive(:qux).with(:context) do
213
- { object: :some_obj, status: :ok }
237
+ it 'uses object_render_opts' do
238
+ expect_any_instance_of(BazService).to receive(:qux).with(:context) do
239
+ { object: :some_obj, status: :ok }
240
+ end
241
+ controller.show
242
+ expect(@render_args[:serializer]).to eq(MyObjectSerializer)
214
243
  end
215
244
 
216
- controller.index
217
-
218
- expect(render_args[:serializer]).to eq(MySerializer)
245
+ it 'uses error_render_opts' do
246
+ expect_any_instance_of(BazService).to receive(:qux).with(:context) do
247
+ { errors: [], status: :not_valid }
248
+ end
249
+ controller.show
250
+ expect(@render_args[:serializer]).to eq(MyErrorSerializer)
251
+ end
219
252
  end
220
253
 
221
254
  class MySerializer
222
255
  end
223
256
 
257
+ class MyObjectSerializer
258
+ end
259
+
260
+ class MyErrorSerializer
261
+ end
262
+
224
263
  class FooContextBuilder
225
264
  def bar(opts) {} end
226
265
  end
@@ -228,7 +267,42 @@ describe DDC::ControllerBuilder do
228
267
  class BazService
229
268
  def qux(context) {} end
230
269
  end
231
- end
232
270
 
271
+ class MultiContextBuilder
272
+ def foo(opts) {foo: 2} end
273
+ def bar(opts) {bar: 3} end
274
+ end
275
+
276
+ class MultiContextService
277
+ def check(ctx)
278
+ {status: :ok, object: ctx[:foo] + ctx[:bar]}
279
+ end
280
+ end
281
+
282
+ it 'supports multiple contexts' do
283
+ class FooController
284
+ def render(args); end
285
+ def respond_to; end
286
+ end
287
+ subject.build :foo, actions: {
288
+ index: {
289
+ context_params: [],
290
+ contexts: ['multi_context_builder#foo', 'multi_context_builder#bar'],
291
+ service: 'multi_context_service#check'
292
+ }
293
+ }
294
+ expect(Object.const_get("FooController")).not_to be_nil
295
+ controller = Object.const_get("FooController").new
296
+ render_args = nil
297
+ expect(controller).to receive(:render) do |args|
298
+ render_args = args
299
+ end
300
+ expect(controller).to receive(:respond_to) do |&block|
301
+ block.call(json_format)
302
+ end
303
+ controller.index
304
+ expect(render_args[:json]).to eq(5)
305
+ end
306
+ end
233
307
  end
234
308
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-15 00:00:00.000000000 Z
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  requirements: []
138
138
  rubyforge_project:
139
- rubygems_version: 2.2.2
139
+ rubygems_version: 2.4.5
140
140
  signing_key:
141
141
  specification_version: 4
142
142
  summary: Data Driven Controllers for Rails