From 7178b697c1a3b1ae6395b70162a62c32658608a2 Mon Sep 17 00:00:00 2001 From: Steve Richert Date: Fri, 31 Mar 2017 15:10:40 -0400 Subject: [PATCH] Address Rubocop concerns 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. --- .rubocop.yml | 2 ++ lib/interactor.rb | 14 ++++++-------- lib/interactor/context.rb | 9 +++++++++ spec/interactor/context_spec.rb | 20 ++++++++++++++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0745e89..080edff 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -43,3 +43,5 @@ Style/StringLiterals: EnforcedStyle: double_quotes Style/SymbolArray: Enabled: false +Style/WordArray: + Enabled: false diff --git a/lib/interactor.rb b/lib/interactor.rb index 69edce0..8cf246f 100644 --- a/lib/interactor.rb +++ b/lib/interactor.rb @@ -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 diff --git a/lib/interactor/context.rb b/lib/interactor/context.rb index cb269d2..238b9f2 100644 --- a/lib/interactor/context.rb +++ b/lib/interactor/context.rb @@ -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. # diff --git a/spec/interactor/context_spec.rb b/spec/interactor/context_spec.rb index eb8b905..f93fd68 100644 --- a/spec/interactor/context_spec.rb +++ b/spec/interactor/context_spec.rb @@ -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 }