Skip to content

Commit

Permalink
Add component convenience method
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Sep 25, 2024
1 parent 0d01cf8 commit 193700b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/operations/convenience.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ def contract(prefix = nil, from: OperationContract, &block)
const_set(:"#{prefix.to_s.camelize}Contract", contract)
end

%w[policy precondition callback].each do |kind|
define_method kind do |prefix = nil, from: Object, &block|
raise ArgumentError.new("Please provide either a superclass or a block for #{kind}") unless from || block

klass = Class.new(from)
def component(name, from: Object, dry_initializer: false, dry_monads_result: false, &block) # rubocop:disable Metrics/CyclomaticComplexity
raise ArgumentError.new("Please provide either a superclass or a block for #{name}") unless from || block

if from == Object
klass.extend(Dry::Initializer)
klass.include(Dry::Monads[:result])
end
klass = Class.new(from)
klass.extend(Dry::Initializer) if dry_initializer && !klass.include?(Dry::Initializer) # rubocop:disable Rails/NegateInclude
klass.include(Dry::Monads[:result]) if dry_monads_result && !klass.is_a?(Dry::Monads[:result])
klass.define_method(:call, &block) if block

klass.define_method(:call, &block) if block
const_set(name.to_s.camelize, klass)
end

const_set(:"#{prefix.to_s.camelize}#{kind.camelize}", klass)
%w[policy precondition callback].each do |kind|
define_method kind do |prefix = nil, from: Object, &block|
component([prefix, kind].compact_blank.join("_"), from: from,
dry_initializer: true, dry_monads_result: true, &block)
end
end
end
29 changes: 29 additions & 0 deletions spec/operations/convenience_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,39 @@ def self.default(arg)
end
end

describe "#component" do
subject(:component) { dummy_operation.component(name, **options) { :foobar } }

let(:name) { :custom_hydrator }
let(:options) { {} }

specify do
expect(component.name).to eq("DummyOperation::CustomHydrator")
expect(component).not_to be_a(Dry::Initializer)
expect(component).not_to include(Dry::Monads[:result])
expect(component.new.call).to eq(:foobar)
end

context "with options given" do
let(:options) { { dry_initializer: true, dry_monads_result: true } }

specify do
expect(component.name).to eq("DummyOperation::CustomHydrator")
expect(component).to be_a(Dry::Initializer)
expect(component).to include(Dry::Monads[:result])
expect(component.new.call).to eq(:foobar)
end
end
end

%w[policy precondition callback].each do |kind|
describe "##{kind}" do
subject(:component) { dummy_operation.public_send(kind) { :foobar } }

specify do
expect(component.name).to eq("DummyOperation::#{kind.camelize}")
expect(component).to be_a(Dry::Initializer)
expect(component).to include(Dry::Monads[:result])
expect(component.new.call).to eq(:foobar)
end

Expand All @@ -89,6 +116,8 @@ def other_method
specify do
expect(component.name).to eq("DummyOperation::#{kind.camelize}")
expect(component).to be < parent
expect(component).to be_a(Dry::Initializer)
expect(component).to include(Dry::Monads[:result])
expect(component.new.call).to eq(:foobar)
end
end
Expand Down

0 comments on commit 193700b

Please sign in to comment.