From df1998828dd9d6faaace586b60b46f6c585adda8 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Wed, 10 Mar 2021 13:13:51 +1100 Subject: [PATCH 1/4] Update reference to dry/equalizer. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I presume it’s something that was refactored out of dry-core? --- spec/unit/class_transformations_spec.rb | 2 +- spec/unit/transformer/class_interface_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/unit/class_transformations_spec.rb b/spec/unit/class_transformations_spec.rb index b57dce3..a6b6112 100644 --- a/spec/unit/class_transformations_spec.rb +++ b/spec/unit/class_transformations_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "dry/core/equalizer" +require "dry/equalizer" RSpec.describe Dry::Transformer::ClassTransformations do describe ".constructor_inject" do diff --git a/spec/unit/transformer/class_interface_spec.rb b/spec/unit/transformer/class_interface_spec.rb index 9c7c2b1..5567b2d 100644 --- a/spec/unit/transformer/class_interface_spec.rb +++ b/spec/unit/transformer/class_interface_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'ostruct' -require 'dry/core/equalizer' +require 'dry/equalizer' RSpec.describe Dry::Transformer do let(:container) { Module.new { extend Dry::Transformer::Registry } } From 15ecd9826aaf04226e64445ebc9e84977df8c833 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Wed, 10 Mar 2021 13:15:35 +1100 Subject: [PATCH 2/4] Handle keyword arguments in a Ruby 3.0 world. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that keyword arguments are no longer implicitly translated into hashes, we need to explicitly handle them as a separate argument type. This commit may not cover all cases (either from a code or spec perspective), but hopefully it’s at least a useful start. --- lib/dry/transformer/function.rb | 20 +++++++++++++------ lib/dry/transformer/pipe/class_interface.rb | 4 ++-- lib/dry/transformer/registry.rb | 4 ++-- spec/unit/class_transformations_spec.rb | 2 +- spec/unit/registry_spec.rb | 14 +++++++++++++ spec/unit/transformer/class_interface_spec.rb | 2 +- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/dry/transformer/function.rb b/lib/dry/transformer/function.rb index 4c5a346..2496ca1 100644 --- a/lib/dry/transformer/function.rb +++ b/lib/dry/transformer/function.rb @@ -25,6 +25,13 @@ class Function # @api private attr_reader :args + # Additional keyword arguments that will be passed to the wrapped proc + # + # @return [Hash] + # + # @api private + attr_reader :kwargs + # @!attribute [r] name # # @return [ Date: Wed, 10 Mar 2021 13:40:09 +1100 Subject: [PATCH 3/4] Include keyword arguments in AST This means the kwargs are always present in the AST, thus updating all the expected responses. --- lib/dry/transformer/function.rb | 19 ++++++++++++++-- spec/unit/function_spec.rb | 40 +++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/dry/transformer/function.rb b/lib/dry/transformer/function.rb index 2496ca1..46e4598 100644 --- a/lib/dry/transformer/function.rb +++ b/lib/dry/transformer/function.rb @@ -97,8 +97,7 @@ def ==(other) # # @api public def to_ast - args_ast = args.map { |arg| arg.respond_to?(:to_ast) ? arg.to_ast : arg } - [name, args_ast] + [name, object_to_ast(args), object_to_ast(kwargs)] end # Converts a transproc to a simple proc @@ -112,6 +111,22 @@ def to_proc fn.to_proc end end + + private + + # @api private + def object_to_ast(object) + case object + when Array + object.map { |item| object_to_ast(item) } + when Hash + object.each_with_object({}) { |(key, value), hash| + hash[object_to_ast(key)] = object_to_ast(value) + } + else + object.respond_to?(:to_ast) ? object.to_ast : object + end + end end end end diff --git a/spec/unit/function_spec.rb b/spec/unit/function_spec.rb index c7639d2..1befd49 100644 --- a/spec/unit/function_spec.rb +++ b/spec/unit/function_spec.rb @@ -34,9 +34,9 @@ expect(f3.to_ast).to eql( [ - :symbolize_keys, [], + :symbolize_keys, [], {}, [ - :rename_keys, [user_name: :name] + :rename_keys, [], {user_name: :name} ] ] ) @@ -47,12 +47,12 @@ expect(f4.to_ast).to eql( [ - :symbolize_keys, [], + :symbolize_keys, [], {}, [ - :rename_keys, [user_name: :name] + :rename_keys, [], {user_name: :name} ], [ - :nest, [:details, [:name]] + :nest, [:details, [:name]], {} ] ] ) @@ -68,9 +68,9 @@ expect(f3.to_ast).to eql( [ - f1.fn, [2], + f1.fn, [2], {}, [ - f2.fn, [] + f2.fn, [], {} ] ] ) @@ -83,9 +83,9 @@ expect(f["user_name" => "Jane"]).to eql(name: "Jane") expect(f.to_ast).to eql( [ - :symbolize_keys, [], + :symbolize_keys, [], {}, [ - :rename_keys, [user_name: :name] + :rename_keys, [], {user_name: :name} ] ] ) @@ -96,7 +96,7 @@ fn = container.t(:to_string) expect(fn[:ok]).to eql("ok") - expect(fn.to_ast).to eql([:to_string, []]) + expect(fn.to_ast).to eql([:to_string, [], {}]) end it "plays well with functions as arguments" do @@ -108,11 +108,27 @@ expect(fn.to_ast).to eql( [ :map_array, [ - [:to_symbol, []] - ] + [:to_symbol, [], {}] + ], {} ] ) end + + it "plays well with keyword arguments" do + container = Module.new do + extend Dry::Transformer::Registry + + def self.trim(string, limit:) + string[0..(limit - 1)] + end + end + + fn = container[:trim, limit: 3] + + expect(fn.to_ast).to eql( + [:trim, [], {limit: 3}] + ) + end end describe "#==" do From aa50f197ef53f6ff97b3f24943b25cd101c2b0a4 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Wed, 10 Mar 2021 14:01:03 +1100 Subject: [PATCH 4/4] Remove focus so all specs run by default. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’m presuming this was committed accidentally 😅 --- spec/unit/store_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/unit/store_spec.rb b/spec/unit/store_spec.rb index 96c4664..2b51772 100644 --- a/spec/unit/store_spec.rb +++ b/spec/unit/store_spec.rb @@ -50,7 +50,7 @@ it "returns false if requested proc is unknown" do expect(store.contain?(:bar)).to be false end - end # describe #fetch + end # describe #contain? describe "#register" do subject { new_store } @@ -73,7 +73,7 @@ end end - describe "#import", :focus do + describe "#import" do before do module Bar def self.bar