Skip to content

Commit

Permalink
Address Rubocop concerns
Browse files Browse the repository at this point in the history
This also improves performance of the Interactor#arguments_for_call
method by not duplicating the table of data held internally by the
context. As a happy side effect, this adds the
Interactor::Context#include? method which may be helpful for developers.
  • Loading branch information
laserlemon committed Mar 31, 2017
1 parent 2eea39b commit 7178b69
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ Style/StringLiterals:
EnforcedStyle: double_quotes
Style/SymbolArray:
Enabled: false
Style/WordArray:
Enabled: false
14 changes: 6 additions & 8 deletions lib/interactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,16 @@ def rollback
# based on their names.
#
# Returns an Array of arguments to be applied as an argument list.
def arguments_for_call
positional_arguments, keyword_arguments = [], {}
available_context_keys = context.to_h.keys
def arguments_for_call # rubocop:disable Metrics/MethodLength
positional_arguments = []
keyword_arguments = {}

method(:call).parameters.each do |(type, name)|
next unless available_context_keys.include?(name)
next unless context.include?(name)

case type
when :req, :opt
positional_arguments << context[name]
when :keyreq, :key
keyword_arguments[name] = context[name]
when :req, :opt then positional_arguments << context[name]
when :keyreq, :key then keyword_arguments[name] = context[name]
end
end

Expand Down
9 changes: 9 additions & 0 deletions lib/interactor/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ def rollback!
@rolled_back = true
end

# Public: Check for the presence of a given key in the context. This does
# not check whether the value is truthy, just whether the key is set to any
# value at all.
#
# Returns true if the key is found or false otherwise.
def include?(key)
table.include?(key.to_sym)
end

# Internal: An Array of successfully called Interactor instances invoked
# against this Interactor::Context instance.
#
Expand Down
20 changes: 20 additions & 0 deletions spec/interactor/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ module Interactor
end
end

describe "#include?" do
it "returns true if the key is found" do
context = Context.build(foo: "bar")

expect(context.include?(:foo)).to eq(true)
end

it "returns true if the symbolized key is found" do
context = Context.build(foo: "bar")

expect(context.include?("foo")).to eq(true)
end

it "returns false if the key is not found" do
context = Context.build(foo: "bar")

expect(context.include?(:hello)).to eq(false)
end
end

describe "#_called" do
let(:context) { Context.build }

Expand Down

0 comments on commit 7178b69

Please sign in to comment.