Sha256: 7972159714895b560fc4fafed2055262aefb8c6acb2d3bbdbf5ed5e9c5d518a2

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require 'dry/logic/rule'

module Dry
  module Logic
    class RuleCompiler
      attr_reader :predicates

      def initialize(predicates)
        @predicates = predicates
      end

      def call(ast)
        ast.map { |node| visit(node) }
      end

      def visit(node)
        name, nodes = node
        send(:"visit_#{name}", nodes)
      end

      def visit_check(node)
        name, predicate, keys = node
        Rule::Check.new(visit(predicate), name: name, keys: keys || [name])
      end

      def visit_not(node)
        visit(node).negation
      end

      def visit_key(node)
        name, predicate = node
        Rule::Key.new(visit(predicate), name: name)
      end

      def visit_attr(node)
        name, predicate = node
        Rule::Attr.new(visit(predicate), name: name)
      end

      def visit_val(node)
        Rule::Value.new(visit(node))
      end

      def visit_set(node)
        Rule::Set.new(call(node))
      end

      def visit_each(node)
        Rule::Each.new(visit(node))
      end

      def visit_predicate(node)
        name, args = node
        predicates[name].curry(*args)
      end

      def visit_and(node)
        left, right = node
        visit(left) & visit(right)
      end

      def visit_or(node)
        left, right = node
        visit(left) | visit(right)
      end

      def visit_xor(node)
        left, right = node
        visit(left) ^ visit(right)
      end

      def visit_implication(node)
        left, right = node
        visit(left) > visit(right)
      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_compiler.rb
dry-logic-0.2.2 lib/dry/logic/rule_compiler.rb
dry-logic-0.2.1 lib/dry/logic/rule_compiler.rb
dry-logic-0.2.0 lib/dry/logic/rule_compiler.rb