Class: MandateClaw::DSL::DecisionRule
- Inherits:
-
Object
- Object
- MandateClaw::DSL::DecisionRule
- Defined in:
- lib/mandate_claw/dsl/decision_rule.rb
Overview
A named decision rule with ternary truth: true / false / :unknown. Borrowed from L4’s model: an unknown value propagates uncertainty rather than defaulting to false.
rule :payment_due_today do
decide :due, when: ->(ctx) { ctx.issued_at + ctx.net_terms <= Date.today }
unknown_when: ->(ctx) { ctx.issued_at.nil? }
end
Constant Summary collapse
- TRUTH_VALUES =
[true, false, :unknown].freeze
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#outcome ⇒ Object
readonly
Returns the value of attribute outcome.
-
#unknown_condition ⇒ Object
readonly
Returns the value of attribute unknown_condition.
Instance Method Summary collapse
- #decide(outcome, when: nil, unknown_when: nil) ⇒ Object
-
#evaluate(context) ⇒ Object
Evaluate against a context object.
-
#initialize(name) ⇒ DecisionRule
constructor
A new instance of DecisionRule.
Constructor Details
#initialize(name) ⇒ DecisionRule
Returns a new instance of DecisionRule.
19 20 21 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 19 def initialize(name) @name = name end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
17 18 19 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 17 def condition @condition end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 17 def name @name end |
#outcome ⇒ Object (readonly)
Returns the value of attribute outcome.
17 18 19 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 17 def outcome @outcome end |
#unknown_condition ⇒ Object (readonly)
Returns the value of attribute unknown_condition.
17 18 19 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 17 def unknown_condition @unknown_condition end |
Instance Method Details
#decide(outcome, when: nil, unknown_when: nil) ⇒ Object
23 24 25 26 27 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 23 def decide(outcome, when: nil, unknown_when: nil) @outcome = outcome @condition = binding.local_variable_get(:when) @unknown_condition = unknown_when end |
#evaluate(context) ⇒ Object
Evaluate against a context object. Returns true, false, or :unknown — never nil.
31 32 33 34 35 36 |
# File 'lib/mandate_claw/dsl/decision_rule.rb', line 31 def evaluate(context) return :unknown if @unknown_condition&.call(context) return @condition.call(context) if @condition :unknown end |