diff --git a/lib/dry/transformer/function.rb b/lib/dry/transformer/function.rb index 4c5a346..46e4598 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 [ "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 diff --git a/spec/unit/registry_spec.rb b/spec/unit/registry_spec.rb index 12df8e1..d1d5de6 100644 --- a/spec/unit/registry_spec.rb +++ b/spec/unit/registry_spec.rb @@ -8,6 +8,10 @@ def self.prefix(value, prefix) "#{prefix}_#{value}" end + + def self.trim(value, limit:) + value[0..(limit - 1)] + end end end @@ -32,6 +36,16 @@ def self.prefix(value, prefix) expect(transproc["qux"]).to eql "baz_qux" end end + + context "with keyword arguments" do + it "passes keywords through" do + expect(foo[:trim]["string", limit: 3]).to eql("str") + end + + it "works with a transproc approach" do + expect(foo[:trim, limit: 2]["string"]).to eql("st") + end + end end describe ".t" do 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 diff --git a/spec/unit/transformer/class_interface_spec.rb b/spec/unit/transformer/class_interface_spec.rb index 9c7c2b1..63ece1a 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 } } @@ -166,7 +166,7 @@ def self.to_symbol(v) map_value(:attr, t(:to_symbol)) end - expect(transproc.new.transproc[attr: 'abc']).to eq(attr: :abc) + expect(transproc.new.transproc[{attr: 'abc'}]).to eq(attr: :abc) end it 'does not affect original transformer' do