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
48
49
50
51
52
53
|
# File 'lib/dry/logic/rule.rb', line 48
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.
27
28
29
|
# File 'lib/dry/logic/rule.rb', line 27
def args
@args
end
|
#arity ⇒ Object
Returns the value of attribute arity.
29
30
31
|
# File 'lib/dry/logic/rule.rb', line 29
def arity
@arity
end
|
#options ⇒ Object
Returns the value of attribute options.
25
26
27
|
# File 'lib/dry/logic/rule.rb', line 25
def options
@options
end
|
#predicate ⇒ Object
Returns the value of attribute predicate.
23
24
25
|
# File 'lib/dry/logic/rule.rb', line 23
def predicate
@predicate
end
|
Class Method Details
.build(predicate, args: EMPTY_ARRAY, arity: predicate.arity, **options) ⇒ Object
44
45
46
|
# File 'lib/dry/logic/rule.rb', line 44
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
31
32
33
|
# File 'lib/dry/logic/rule.rb', line 31
def self.interfaces
@interfaces ||= ::Concurrent::Map.new
end
|
.specialize(arity, curried, base = Rule) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/dry/logic/rule.rb', line 35
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
90
91
92
|
# File 'lib/dry/logic/rule.rb', line 90
def ast(input = Undefined)
[:predicate, [id, args_with_names(input)]]
end
|
#bind(object) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/dry/logic/rule.rb', line 67
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
63
64
65
|
# File 'lib/dry/logic/rule.rb', line 63
def curry(*new_args)
with(args: args + new_args)
end
|
#eval_args(object) ⇒ Object
78
79
80
|
# File 'lib/dry/logic/rule.rb', line 78
def eval_args(object)
with(args: args.map { |arg| UnboundMethod === arg ? arg.bind(object).() : arg })
end
|
#id ⇒ Object
59
60
61
|
# File 'lib/dry/logic/rule.rb', line 59
def id
options[:id]
end
|
#parameters ⇒ Object
86
87
88
|
# File 'lib/dry/logic/rule.rb', line 86
def parameters
options[:parameters] || predicate.parameters
end
|
#type ⇒ Object
55
56
57
|
# File 'lib/dry/logic/rule.rb', line 55
def type
:rule
end
|
#with(new_opts) ⇒ Object
82
83
84
|
# File 'lib/dry/logic/rule.rb', line 82
def with(new_opts)
self.class.build(predicate, options.merge(new_opts))
end
|