Sha256: 245bd3183f6a95aded01ae3d9b615b1f8e040f4d19b2457952a59abf3c886e42

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

require 'dry/logic/operations/unary'
require 'dry/logic/evaluator'
require 'dry/logic/result'

module Dry
  module Logic
    module Operations
      class Check < Unary
        attr_reader :evaluator

        def self.new(rule, **options)
          if options[:evaluator]
            super(rule, options)
          else
            keys = options.fetch(:keys)
            evaluator = Evaluator::Set.new(keys)

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

        def initialize(*rules, **options)
          super
          @evaluator = options[:evaluator]
        end

        def type
          :check
        end

        def call(input)
          *head, tail = evaluator[input]
          result = rule.curry(*head).(tail)

          if result.success?
            Result::SUCCESS
          else
            Result.new(false, id) { [type, [options[:keys], result.to_ast]] }
          end
        end

        def [](input)
          rule[*evaluator[input].reverse]
        end

        def ast(input = Undefined)
          [type, [options[:keys], rule.ast(input)]]
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-logic-1.0.0 lib/dry/logic/operations/check.rb
dry-logic-0.6.1 lib/dry/logic/operations/check.rb
dry-logic-0.6.0 lib/dry/logic/operations/check.rb
dry-logic-0.5.0 lib/dry/logic/operations/check.rb
dry-logic-0.4.2 lib/dry/logic/operations/check.rb
dry-logic-0.4.1 lib/dry/logic/operations/check.rb
dry-logic-0.4.0 lib/dry/logic/operations/check.rb