From 3661bfcf11913ac2b69b7a0b5efd5abe629d1ea7 Mon Sep 17 00:00:00 2001 From: Aamir Ahmad Date: Fri, 1 Nov 2024 01:15:05 -0400 Subject: [PATCH] Chore(WIN-2123): Address kwargs deprecation warning --- .github/workflows/testing.yml | 2 +- lib/active_operation/base.rb | 24 ++++++++++++++---------- spec/active_operation/pipeline_spec.rb | 5 +++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 1d6b3ef..1625a29 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby_version: [2.6.9, 2.7.5, 3.0.3] + ruby_version: [2.7.8, 3.0.7] steps: - uses: actions/checkout@v2 - name: Set up Ruby diff --git a/lib/active_operation/base.rb b/lib/active_operation/base.rb index ba29277..33af0d2 100644 --- a/lib/active_operation/base.rb +++ b/lib/active_operation/base.rb @@ -16,8 +16,8 @@ class ActiveOperation::Base define_callbacks :halted, scope: [:name] class << self - def perform(*args) - new(*args).call + def perform(...) + new(...).call end def from_proc(execute) @@ -30,8 +30,8 @@ def from_proc(execute) when :req input name, type: :positional, required: true positional_arguments << name - when :keyreq - input name, type: :keyword, required: true + when :keyreq, :key + input name, type: :keyword, required: (type == :keyreq) keyword_arguments << name else raise ArgumentError, "Argument type not supported: #{type}" @@ -51,8 +51,8 @@ def from_proc(execute) end end - def call(*args) - perform(*args) + def call(...) + perform(...) end def inputs @@ -60,8 +60,8 @@ def inputs end def to_proc - ->(*args) { - perform(*args) + ->(*args, **kwargs) { + perform(*args, **kwargs) } end @@ -130,7 +130,11 @@ def inherited(subclass) def initialize(*args) arity = self.class.inputs.count(&:positional?) arguments = args.shift(arity) - attributes = args.last.kind_of?(Hash) ? args.pop : {} + attributes = if args.last.is_a?(Hash) + args.pop.transform_keys(&:to_sym) + else + {} + end raise ArgumentError, "wrong number of arguments #{arguments.length + args.length} for #{arity}" unless args.empty? @@ -138,7 +142,7 @@ def initialize(*args) attributes[input.name] = arguments[index] end - super(attributes) + super(**attributes) end def perform diff --git a/spec/active_operation/pipeline_spec.rb b/spec/active_operation/pipeline_spec.rb index 0a5c751..d14c66b 100644 --- a/spec/active_operation/pipeline_spec.rb +++ b/spec/active_operation/pipeline_spec.rb @@ -230,8 +230,9 @@ def execute expect { described_class.compose { use ->(a = 1) {} } }.to raise_error(ArgumentError) end - it "does not support optional keyword arguments as Ruby's reflection mechanism does not support accessing the default value which is required to setup the operation inputs correctly" do - expect { described_class.compose { use ->(a: 1) {} } }.to raise_error(ArgumentError) + it "supports optional keyword arguments" do + pipeline = described_class.compose { use ->(a: 1) { a } } + expect(pipeline.call(a: 2)).to eq(2) end end end