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 .parameters and .parameters! methods for input parameters expectations on call #112

Closed
wants to merge 2 commits into from
Closed
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
79 changes: 79 additions & 0 deletions lib/interactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,85 @@ def call(context = {})
def call!(context = {})
new(context).tap(&:run!).context
end

# Public: Declare input parameters. This method will simply delegate
# specified methods to the to the context.
#
# input_parameters_list - zero or more Symbol argument names that are
# expected to present at the context. Private instance methods
# with corresponding names will be created.
#
# Examples
#
# class MyInteractor
# include Interactor
#
# parameters :message
#
# def call
# puts message
# end
# end
#
# MyInteractor.call(message: "Hello!")
# # Hello!
# # => #<Interactor::Context message="Hello!">
#
# Returns array of Symbols, representing expected arguments' names

def parameters(*input_parameters_list)
input_parameters_list.each do |parameter_name|
define_method parameter_name do
context.public_send(parameter_name)
end
private parameter_name
end
end

# Public: Declare input parameters. This method will simply delegate
# specified methods to the to the context. If any of the specified
# parameters is missing on call, it will fail the context.
#
# input_parameters_list - zero or more Symbol argument names that are
# expected to present at the context. Private instance methods
# with corresponding names will be created. All these arguments
# will be verified to present in the context in before hook.
#
# Examples
#
# class MyInteractor
# include Interactor
#
# parameters! :foo, :bar
#
# def call
# puts "#{foo} #{bar}"
# end
# end
#
# MyInteractor.call(foo: "foo")
# # => #<Interactor::Context foo="foo">
#
# MyInteractor.call!(foo: "foo")
# # => #<Interactor::Failure: #<Interactor::Context foo="foo">>
#
# MyInteractor.call(foo: "foo", bar: "bar")
# # foo bar
# # => #<Interactor::Context foo="foo", bar="bar">
#
# MyInteractor.call!(foo: "foo", bar: "bar")
# # foo bar
# # => #<Interactor::Context foo="foo", bar="bar">

def parameters!(*input_parameters_list)
parameters(*input_parameters_list)

before do
input_parameters_list.each do |parameter_name|
context.fail! if context.public_send(parameter_name).nil?
end
end
end
end

# Internal: Initialize an Interactor.
Expand Down
47 changes: 47 additions & 0 deletions spec/support/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,53 @@
end
end

describe ".parameters" do
let(:context) { double(:context, foo: "foo", bar: "bar") }
let(:instance) { interactor.new }

it "defines instance parameter methods" do
expect(Interactor::Context).to receive(:build) { context }

interactor.parameters(:foo, :bar)

expect(instance.private_methods).to include(:foo, :bar)
expect(instance.send(:foo)).to eq "foo"
expect(instance.send(:bar)).to eq "bar"
end
end

describe ".parameters!" do
it "defines instance parameter methods" do
expect(interactor).to receive(:parameters).with(:foo, :bar).once

interactor.parameters!(:foo, :bar)
end

context "with missing parameter" do
let(:context) { double foo: "foo", bar: nil, called!: double }

it "fails the context" do
expect(Interactor::Context).to receive(:build) { context }
expect(context).to receive(:fail!)

interactor.parameters!(:foo, :bar)
interactor.call
end
end

context "with all required parameters specified" do
let(:context) { double foo: "foo", bar: "bar", called!: double }

it "does not fail the context" do
expect(Interactor::Context).to receive(:build) { context }
expect(context).not_to receive(:fail!)

interactor.parameters!(:foo, :bar)
interactor.call
end
end
end

describe "#run" do
let(:instance) { interactor.new }

Expand Down