dry-transformer icon indicating copy to clipboard operation
dry-transformer copied to clipboard

Support keyword arguments for functions.

Open pat opened this issue 3 years ago • 4 comments

Another Ruby 3 related patch: now that keyword arguments are no longer implicitly translated into hashes, we need to explicitly handle them as a separate argument type.

And I must be clear: I don't have a deep understanding of this gem, so I feel like the specs and code are quite possibly incomplete. Hopefully they're at least useful as a starting point. 😅

pat avatar Mar 10 '21 02:03 pat

… and now that I removed the :focus that avoided the full suite from running, I can see that the build is not happy at all with pre-3.0 Rubies. And I'm feeling well and truly out of my depth.

I might set this aside for now, rather than muddling my way through the failing tests. Hopefully what's here is useful, but for now I think I'm going to change my functions to avoid keyword arguments.

pat avatar Mar 10 '21 03:03 pat

Thanks. It's best to hold off with this. I think it would be good to revisit the implementation of this gem in general, given how many things changed in Ruby since transproc was born (which is now dry-transformer).

solnic avatar Mar 10 '21 08:03 solnic

got exactly the same problem when moving a running project from 2.7.x to 3.1.

My solution was vastly simple (naive).

i.e adding a kwargs build from given *args Array in functions.rb along the lines


def initialize(fn, options = {})
        @fn = fn
        @args = options.fetch(:args, [])
        @kwargs = {}
        if fn.parameters.any? { |(param_type, _)| param_type == :key}
          if args.last.is_a? Hash
            @kwargs = @args.pop
          end
        end
        @name = options.fetch(:name, fn)
      end

      # Call the wrapped proc
      #
      # @param [Object] value The input value
      #
      # @alias []
      #
      # @api public
      def call(*value)
        fn.call(*value, *args, **kwargs)
      end

worked for a simple composition of Dry::Transformer::Pipe

But then discovered this more extensive and nicer version.

Is there is plans to revisit this and perhaps drop support for pre 2.7 on a recent version ?

This gem is nice and perhaps need a bit of love ;-)

nonnenmacher avatar Nov 21 '22 14:11 nonnenmacher

Ah, I'm having the same issue using prefix: kw arg with unwrap

rfb avatar Jan 17 '23 19:01 rfb