Sha256: 2dcd8ed8396baa8c59e547a6cb42bf8f9d8acb864b49ec10284b3715031f01e6

Contents?: true

Size: 913 Bytes

Versions: 2

Compression:

Stored size: 913 Bytes

Contents

# frozen_string_literal: true

require_relative 'refinements'
require_relative 'parser'

module Rack
  module Reducer
    # call `reduce` on a params hash, filtering data via lambdas with
    # matching keyword arguments
    class Reduction
      using Refinements # augment Hash & Proc inside this scope

      DEFAULTS = {
        dataset: [],
        filters: [],
        params: nil
      }.freeze

      def initialize(options)
        @props = DEFAULTS.merge(options)
        @params = Parser.call(@props[:params]).symbolize_keys
      end

      def reduce
        @props[:filters].reduce(@props[:dataset], &method(:apply_filter))
      end

      private

      def apply_filter(data, fn)
        requirements = fn.required_argument_names.to_set
        return data unless @params.satisfies?(requirements)
        data.instance_exec(@params.slice(*fn.all_argument_names), &fn)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-reducer-0.1.2 lib/rack/reducer/reduction.rb
rack-reducer-0.1.1 lib/rack/reducer/reduction.rb