Class: Dry::Logic::Rule
- Inherits:
-
Object
show all
- Includes:
- Core::Constants, Operators
- Defined in:
- lib/dry/logic/rule.rb,
lib/dry/logic/rule/interface.rb
Defined Under Namespace
Classes: Interface, Predicate
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Operators
#and, #or, #then, #xor
Constructor Details
#initialize(predicate, options = EMPTY_HASH) ⇒ Rule
Returns a new instance of Rule.
50
51
52
53
54
55
|
# File 'lib/dry/logic/rule.rb', line 50
def initialize(predicate, options = EMPTY_HASH)
@predicate = predicate
@options = options
@args = options[:args] || EMPTY_ARRAY
@arity = options[:arity] || predicate.arity
end
|
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
29
30
31
|
# File 'lib/dry/logic/rule.rb', line 29
def args
@args
end
|
#arity ⇒ Object
Returns the value of attribute arity.
31
32
33
|
# File 'lib/dry/logic/rule.rb', line 31
def arity
@arity
end
|
#options ⇒ Object
Returns the value of attribute options.
27
28
29
|
# File 'lib/dry/logic/rule.rb', line 27
def options
@options
end
|
#predicate ⇒ Object
Returns the value of attribute predicate.
25
26
27
|
# File 'lib/dry/logic/rule.rb', line 25
def predicate
@predicate
end
|
Class Method Details
.build(predicate, args: EMPTY_ARRAY, arity: predicate.arity, **options) ⇒ Object
46
47
48
|
# File 'lib/dry/logic/rule.rb', line 46
def self.build(predicate, args: EMPTY_ARRAY, arity: predicate.arity, **options)
specialize(arity, args.size).new(predicate, { args: args, arity: arity, **options })
end
|
.interfaces ⇒ Object
33
34
35
|
# File 'lib/dry/logic/rule.rb', line 33
def self.interfaces
@interfaces ||= ::Concurrent::Map.new
end
|
.specialize(arity, curried, base = Rule) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/dry/logic/rule.rb', line 37
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
|
Instance Method Details
#ast(input = Undefined) ⇒ Object
92
93
94
|
# File 'lib/dry/logic/rule.rb', line 92
def ast(input = Undefined)
[:predicate, [id, args_with_names(input)]]
end
|
#bind(object) ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/dry/logic/rule.rb', line 69
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.merge(arity: arity, parameters: parameters)
)
end
end
|
#curry(*new_args) ⇒ Object
65
66
67
|
# File 'lib/dry/logic/rule.rb', line 65
def curry(*new_args)
with(args: args + new_args)
end
|
#eval_args(object) ⇒ Object
80
81
82
|
# File 'lib/dry/logic/rule.rb', line 80
def eval_args(object)
with(args: args.map { |arg| UnboundMethod === arg ? arg.bind(object).() : arg })
end
|
#id ⇒ Object
61
62
63
|
# File 'lib/dry/logic/rule.rb', line 61
def id
options[:id]
end
|
#parameters ⇒ Object
88
89
90
|
# File 'lib/dry/logic/rule.rb', line 88
def parameters
options[:parameters] || predicate.parameters
end
|
#type ⇒ Object
57
58
59
|
# File 'lib/dry/logic/rule.rb', line 57
def type
:rule
end
|
#with(new_opts) ⇒ Object
84
85
86
|
# File 'lib/dry/logic/rule.rb', line 84
def with(new_opts)
self.class.build(predicate, options.merge(new_opts))
end
|