Sha256: b5c05b2ced24028a06c97c008f88301f9a5b559106158289423dcfbf489834ca

Contents?: true

Size: 663 Bytes

Versions: 4

Compression:

Stored size: 663 Bytes

Contents

module Dry
  module Logic
    def self.Predicate(block)
      case block
      when Method then Predicate.new(block.name, &block)
      else raise ArgumentError, 'predicate needs an :id'
      end
    end

    class Predicate
      include Dry::Equalizer(:id, :args)

      attr_reader :id, :args, :fn

      def initialize(id, *args, &block)
        @id = id
        @fn = block
        @args = args
      end

      def call(*args)
        fn.(*args)
      end

      def curry(*args)
        self.class.new(id, *args, &fn.curry.(*args))
      end

      def to_ast
        [:predicate, [id, args]]
      end
      alias_method :to_a, :to_ast
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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