lib/rack/reducer/reduction.rb in rack-reducer-0.1.2 vs lib/rack/reducer/reduction.rb in rack-reducer-1.0.0

- old
+ new

@@ -1,37 +1,35 @@ -# 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 + using Refinements # define Proc#required_argument_names, #satisfies?, etc DEFAULTS = { dataset: [], filters: [], params: nil }.freeze def initialize(options) @props = DEFAULTS.merge(options) - @params = Parser.call(@props[:params]).symbolize_keys + @params = Parser.call(@props[:params]) 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) + def apply_filter(data, filter) + return data unless filter.satisfies?(@params) + + data.instance_exec(@params.slice(*filter.all_argument_names), &filter) end end end end