Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component convenience method #53

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Loading