-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'support-if-then-syntax' into namespace
* support-if-then-syntax: extract IfKlass add support for then else in if method syntax fix constant name rename to success_arg etc add missing specs
- Loading branch information
Showing
7 changed files
with
246 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
module Interactify | ||
module Dsl | ||
class IfKlass | ||
attr_reader :if_builder | ||
|
||
def initialize(if_builder) | ||
@if_builder = if_builder | ||
end | ||
|
||
def klass | ||
attach_expectations | ||
attach_source_location | ||
attach_run! | ||
attach_inspect | ||
|
||
if_builder.klass_basis | ||
end | ||
|
||
def run!(context) | ||
result = condition.is_a?(Proc) ? condition.call(context) : context.send(condition) | ||
|
||
interactor = result ? success_interactor : failure_interactor | ||
interactor.respond_to?(:call!) ? interactor.call!(context) : interactor&.call(context) | ||
end | ||
|
||
private | ||
|
||
def attach_source_location | ||
attach do |_klass, this| | ||
define_singleton_method(:source_location) do # def self.source_location | ||
const_source_location this.evaluating_receiver.to_s # [file, line] | ||
end | ||
end | ||
end | ||
|
||
def attach_expectations | ||
attach do |klass, this| | ||
klass.expects do | ||
required(this.condition) unless this.condition.is_a?(Proc) | ||
end | ||
end | ||
end | ||
|
||
def attach_run! | ||
this = self | ||
|
||
attach_method(:run!) do | ||
this.run!(context) | ||
end | ||
end | ||
|
||
delegate :condition, :success_interactor, :failure_interactor, to: :if_builder | ||
|
||
def attach_inspect | ||
this = if_builder | ||
|
||
attach_method(:inspect) do | ||
name = "#{this.namespace}::#{this.if_klass_name}" | ||
"<#{name} #{this.condition} ? #{this.success_interactor} : #{this.failure_interactor}>" | ||
end | ||
end | ||
|
||
def attach_method(name, &) | ||
attach do |klass, _this| | ||
klass.define_method(name, &) | ||
end | ||
end | ||
|
||
def attach | ||
this = if_builder | ||
|
||
this.klass_basis.instance_eval do | ||
yield self, this | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Interactify::Dsl do | ||
self::Slot = Class.new do | ||
extend Interactify::Dsl | ||
end | ||
|
||
let(:slot) { self.class::Slot } | ||
|
||
describe ".if" do | ||
context "with condition, success, and failure arguments" do | ||
let(:return_value) { "some return value" } | ||
|
||
it "passes them through to the IfInteractor" do | ||
allow(described_class::IfInteractor).to receive(:attach_klass).and_return(return_value) | ||
|
||
expect(slot.if(:condition, :success, :failure)).to eq(return_value) | ||
|
||
expect(described_class::IfInteractor).to have_received(:attach_klass).with( | ||
slot, | ||
:condition, | ||
:success, | ||
:failure | ||
) | ||
end | ||
|
||
let(:on_success1) do | ||
lambda { |ctx| | ||
ctx.success1 = true | ||
} | ||
end | ||
let(:on_success2) { ->(ctx) { ctx.success2 = true } } | ||
|
||
let(:on_failure1) { ->(ctx) { ctx.success1 = false } } | ||
let(:on_failure2) { ->(ctx) { ctx.success2 = false } } | ||
|
||
context "when the success and failure arguments are arrays" do | ||
it "chains the interactors" do | ||
klass = slot.if( | ||
:condition, | ||
[on_success1, on_success2], | ||
[on_failure1, on_failure2] | ||
) | ||
|
||
expect(klass.ancestors).to include Interactor | ||
expect(klass.ancestors).to include Interactor::Contracts | ||
|
||
result = klass.call!(condition: true) | ||
expect(result.success1).to eq(true) | ||
expect(result.success2).to eq(true) | ||
|
||
result = klass.call!(condition: false) | ||
expect(result.success1).to eq(false) | ||
expect(result.success2).to eq(false) | ||
end | ||
end | ||
|
||
context "when using hash then, else syntax" do | ||
it "chains the interactors" do | ||
klass = slot.if( | ||
:condition, | ||
then: [on_success1, on_success2], | ||
else: [on_failure1, on_failure2] | ||
) | ||
|
||
expect(klass.ancestors).to include Interactor | ||
expect(klass.ancestors).to include Interactor::Contracts | ||
|
||
result = klass.call!(condition: true) | ||
expect(result.success1).to eq(true) | ||
expect(result.success2).to eq(true) | ||
|
||
result = klass.call!(condition: false) | ||
expect(result.success1).to eq(false) | ||
expect(result.success2).to eq(false) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters