Class: Dry::Logic::Rule
- Inherits:
-
Object
- Object
- Dry::Logic::Rule
- Includes:
- Core::Constants, Operators
- Defined in:
- lib/dry/logic/rule.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Predicate
Constant Summary collapse
- DEFAULT_OPTIONS =
{ args: [].freeze }.freeze
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#predicate ⇒ Object
readonly
Returns the value of attribute predicate.
Instance Method Summary collapse
- #[](*input) ⇒ Object
- #ast(input = Undefined) ⇒ Object
- #bind(object) ⇒ Object
- #call(*input) ⇒ Object
- #curry(*new_args) ⇒ Object
- #eval_args(object) ⇒ Object
- #id ⇒ Object
-
#initialize(predicate, options = DEFAULT_OPTIONS) ⇒ Rule
constructor
A new instance of Rule.
- #parameters ⇒ Object
- #type ⇒ Object
- #with(new_opts) ⇒ Object
Methods included from Operators
Constructor Details
#initialize(predicate, options = DEFAULT_OPTIONS) ⇒ Rule
Returns a new instance of Rule.
31 32 33 34 35 36 |
# File 'lib/dry/logic/rule.rb', line 31 def initialize(predicate, = DEFAULT_OPTIONS) @predicate = predicate @options = @args = [:args] @arity = [:arity] || predicate.arity end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
27 28 29 |
# File 'lib/dry/logic/rule.rb', line 27 def args @args end |
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
29 30 31 |
# File 'lib/dry/logic/rule.rb', line 29 def arity @arity end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
25 26 27 |
# File 'lib/dry/logic/rule.rb', line 25 def @options end |
#predicate ⇒ Object (readonly)
Returns the value of attribute predicate.
23 24 25 |
# File 'lib/dry/logic/rule.rb', line 23 def predicate @predicate end |
Instance Method Details
#[](*input) ⇒ Object
50 51 52 |
# File 'lib/dry/logic/rule.rb', line 50 def [](*input) arity == 0 ? predicate.() : predicate[*args, *input] end |
#ast(input = Undefined) ⇒ Object
87 88 89 |
# File 'lib/dry/logic/rule.rb', line 87 def ast(input = Undefined) [:predicate, [id, args_with_names(input)]] end |
#bind(object) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dry/logic/rule.rb', line 64 def bind(object) if UnboundMethod === predicate self.class.new(predicate.bind(object), ) else self.class.new( -> *args { object.instance_exec(*args, &predicate) }, .merge(arity: arity, parameters: parameters) ) end end |
#call(*input) ⇒ Object
46 47 48 |
# File 'lib/dry/logic/rule.rb', line 46 def call(*input) Result.new(self[*input], id) { ast(*input) } end |
#curry(*new_args) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/dry/logic/rule.rb', line 54 def curry(*new_args) all_args = args + new_args if all_args.size > arity raise ArgumentError, "wrong number of arguments (#{all_args.size} for #{arity})" else with(args: all_args) end end |
#eval_args(object) ⇒ Object
75 76 77 |
# File 'lib/dry/logic/rule.rb', line 75 def eval_args(object) with(args: args.map { |arg| UnboundMethod === arg ? arg.bind(object).() : arg }) end |
#id ⇒ Object
42 43 44 |
# File 'lib/dry/logic/rule.rb', line 42 def id [:id] end |
#parameters ⇒ Object
83 84 85 |
# File 'lib/dry/logic/rule.rb', line 83 def parameters [:parameters] || predicate.parameters end |
#type ⇒ Object
38 39 40 |
# File 'lib/dry/logic/rule.rb', line 38 def type :rule end |
#with(new_opts) ⇒ Object
79 80 81 |
# File 'lib/dry/logic/rule.rb', line 79 def with(new_opts) self.class.new(predicate, .merge(new_opts)) end |