class Dry::Logic::Rule
def self.build(predicate, args: EMPTY_ARRAY, arity: predicate.arity, **options)
def self.build(predicate, args: EMPTY_ARRAY, arity: predicate.arity, **options) specialize(arity, args.size).new(predicate, {args: args, arity: arity, **options}) end
def self.interfaces
def self.interfaces @interfaces ||= ::Concurrent::Map.new end
def self.specialize(arity, curried, base = Rule)
def self.specialize(arity, curried, base = Rule) base.interfaces.fetch_or_store([arity, curried]) do interface = Interface.new(arity, curried) klass = ::Class.new(base) { include interface } base.const_set("#{base.name.split("::").last}#{interface.name}", klass) klass end end
def args_with_names(*input)
def args_with_names(*input) parameters.map(&:last).zip(args + input) end
def ast(input = Undefined)
def ast(input = Undefined) [:predicate, [id, args_with_names(input)]] end
def bind(object)
def bind(object) if predicate.respond_to?(:bind) self.class.build(predicate.bind(object), **options) else self.class.build( -> *args { object.instance_exec(*args, &predicate) }, **options, arity: arity, parameters: parameters ) end end
def curry(*new_args)
def curry(*new_args) with(args: args + new_args) end
def eval_args(object)
def eval_args(object) with(args: args.map { |arg| arg.is_a?(UnboundMethod) ? arg.bind(object).() : arg }) end
def id
def id options[:id] end
def initialize(predicate, options = EMPTY_HASH)
def initialize(predicate, options = EMPTY_HASH) @predicate = predicate @options = options @args = options[:args] || EMPTY_ARRAY @arity = options[:arity] || predicate.arity end
def parameters
def parameters options[:parameters] || predicate.parameters end
def type
def type :rule end
def with(new_opts)
def with(new_opts) self.class.build(predicate, **options, **new_opts) end