Sha256: ac198974fd859f8122dfaa041d765f0fe4d4c42d8867b12f1a87860ec4eb9910

Contents?: true

Size: 859 Bytes

Versions: 4

Compression:

Stored size: 859 Bytes

Contents

require 'dry/logic/evaluator'

module Dry
  module Logic
    class Rule::Check < Rule::Value
      attr_reader :name, :evaluator

      def self.new(predicate, options)
        keys = options.fetch(:keys)
        evaluator = Evaluator::Set.new(keys)

        super(predicate, options.merge(evaluator: evaluator))
      end

      def initialize(predicate, options)
        super
        @name = options.fetch(:name)
        @evaluator = options[:evaluator]
      end

      def call(input)
        args = evaluator[input].reverse
        *head, tail = args
        Logic.Result(predicate.curry(*head).(tail), head.size > 0 ? curry(*head) : self, input)
      end

      def evaluate(input)
        evaluator[input].first
      end

      def type
        :check
      end

      def to_ast
        [type, [name, predicate.to_ast]]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-logic-0.2.3 lib/dry/logic/rule/check.rb
dry-logic-0.2.2 lib/dry/logic/rule/check.rb
dry-logic-0.2.1 lib/dry/logic/rule/check.rb
dry-logic-0.2.0 lib/dry/logic/rule/check.rb