Sha256: e9f3daabe22cafb8a8fe6899a736dbe9552af73a4e6e1b1a9d08532e23ab5bfd

Contents?: true

Size: 603 Bytes

Versions: 1

Compression:

Stored size: 603 Bytes

Contents

require "keyword_argnize/version"

module KeywordArgnize
  NOT_GIVEN = Object.new

  def keyword_argnize(method_id)
    m = instance_method(method_id)
    mod = Module.new do
      names = m.parameters.map(&:last)
      keys = names.map {|n| "#{n}: KeywordArgnize::NOT_GIVEN" }
      params = (["*args"] + keys).join(", ")
      module_eval(<<~RUBY)
      def #{method_id}(#{params})
        if args.empty?
          super(*[#{names.join(",")}].reject {|param| param == KeywordArgnize::NOT_GIVEN })
        else
          super(*args)
        end
      end
      RUBY
    end
    prepend(mod)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keyword_argnize-0.1.0 lib/keyword_argnize.rb