From 8185472b4a0e06fb5ae02ed749856d6512d56994 Mon Sep 17 00:00:00 2001 From: Pablo Herrero Date: Mon, 4 Feb 2019 02:49:10 -0300 Subject: [PATCH] Permit Operation.call even if responder plugin is not loaded --- CHANGELOG.md | 5 ++++- lib/pathway.rb | 5 ++++- lib/pathway/plugins/responder.rb | 2 +- spec/plugins/base_spec.rb | 16 +++++++++++++--- spec/plugins/sequel_models_spec.rb | 3 +-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3042d71..636d579 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ### Changed - Changed behavior for `:after_commit` step wrapper, on `:sequel_models` plugin, to capture current state and reuse it later when executing. +### Fixed +- Allow invoking `call` directly on an operation class even if the `:responder` plugin is not loaded. + ## [0.8.0] - 2018-10-01 ### Changed - Added support for `dry-validation` 0.12.x @@ -17,7 +20,7 @@ - Allow `authorization` block to take multiple parameters on `simple_auth` plugin. ## [0.6.2] - 2018-05-19 -### Fixes +### Fixed - Allow `:error_message` option for `sequel_models` plugin to propagate down inherited classes ## [0.6.1] - 2018-03-16 diff --git a/lib/pathway.rb b/lib/pathway.rb index a0e15c7..201ec84 100644 --- a/lib/pathway.rb +++ b/lib/pathway.rb @@ -77,6 +77,7 @@ module Plugins module Base module ClassMethods attr_accessor :result_key + alias :result_at :result_key= def process(&bl) dsl = self::DSL @@ -87,7 +88,9 @@ def process(&bl) end end - alias :result_at :result_key= + def call(ctx, *params) + new(ctx).call(*params) + end def inherited(subclass) super diff --git a/lib/pathway/plugins/responder.rb b/lib/pathway/plugins/responder.rb index c06c402..7dade2f 100644 --- a/lib/pathway/plugins/responder.rb +++ b/lib/pathway/plugins/responder.rb @@ -3,7 +3,7 @@ module Plugins module Responder module ClassMethods def call(ctx, *params, &bl) - result = new(ctx).call(*params) + result = super(ctx, *params) block_given? ? Responder.respond(result, &bl) : result end end diff --git a/spec/plugins/base_spec.rb b/spec/plugins/base_spec.rb index 1a92aac..649e93e 100644 --- a/spec/plugins/base_spec.rb +++ b/spec/plugins/base_spec.rb @@ -3,6 +3,7 @@ module Pathway module Plugins describe Base do + class OperationWithSteps < Operation context :validator, :back_end, :notifier, :cond result_at :result_value @@ -61,9 +62,8 @@ def notify(state) let(:notifier) { double } let(:cond) { double } - subject(:operation) do - OperationWithSteps.new(validator: validator, back_end: back_end, notifier: notifier, cond: cond) - end + let(:ctx) { { validator: validator, back_end: back_end, notifier: notifier, cond: cond } } + subject(:operation) { OperationWithSteps.new(ctx) } before do allow(validator).to receive(:call) do |input:, **| @@ -99,6 +99,16 @@ def notify(state) end end + describe ".call" do + let(:result) { OperationWithSteps.call(ctx, input) } + it "creates a new instance an invokes the 'call' method on it" do + expect(back_end).to receive(:call).and_return(:SOME_RETURN_VALUE) + + expect(result).to be_a_success + expect(result.value).to eq(:SOME_RETURN_VALUE) + end + end + describe "#set" do it "defines an updating step which sets the result key if no key is specified" do expect(back_end).to receive(:call).and_return(:SOME_VALUE) diff --git a/spec/plugins/sequel_models_spec.rb b/spec/plugins/sequel_models_spec.rb index 7709639..df63e9f 100644 --- a/spec/plugins/sequel_models_spec.rb +++ b/spec/plugins/sequel_models_spec.rb @@ -52,8 +52,7 @@ class ChainedOperation < MyOperation end def chain_operation(input:,**) - opr = MailerOperation.new(mailer: @mailer) - opr.call(input) + MailerOperation.call(context, input) end end