Sha256: 72be2b02856fdb7a8ec1a479e9a9667b1c584543acdca9282a9c9b4a264ada28

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Rack
  module Reducer
    # refine Proc and hash in this scope only
    module Refinements
      refine Proc do
        def required_argument_names
          parameters.select { |type, _| type == :keyreq }.map(&:last)
        end

        def all_argument_names
          parameters.map(&:last)
        end

        def satisfies?(params)
          keywords = required_argument_names
          params.slice(*keywords).keys.to_set == keywords.to_set
        end
      end

      # backport Hash#slice for Ruby < 2.4
      unless {}.respond_to?(:slice)
        refine Hash do
          def slice(*keys)
            [keys, values_at(*keys)].transpose.select { |_k, val| val }.to_h
          end
        end
      end

      refine Hash do
        def symbolize_keys
          each_with_object({}) do |(key, val), hash|
            hash[key.to_sym] = val.is_a?(Hash) ? symbolize(val) : val
          end
        end

        alias_method :to_unsafe_h, :to_h
      end
    end

    private_constant :Refinements
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-reducer-1.1.1 lib/rack/reducer/refinements.rb
rack-reducer-1.1.0 lib/rack/reducer/refinements.rb