Class: Philiprehberger::RuleEngine::Rule
- Inherits:
-
Object
- Object
- Philiprehberger::RuleEngine::Rule
- Includes:
- Helpers
- Defined in:
- lib/philiprehberger/rule_engine/rule.rb
Overview
A single rule with a name, condition, action, and priority.
Instance Attribute Summary collapse
-
#enabled ⇒ Boolean
Whether the rule is enabled.
-
#name ⇒ String
readonly
The rule name.
-
#tags ⇒ Array<Symbol>
readonly
The rule tags.
Instance Method Summary collapse
-
#action {|facts| ... } ⇒ void
Set the action for this rule.
-
#condition {|facts| ... } ⇒ void
Set the condition for this rule.
-
#execute(facts) ⇒ Object
Execute the action with the given facts.
-
#initialize(name, tags: []) ⇒ Rule
constructor
A new instance of Rule.
-
#matches?(facts) ⇒ Boolean
Check if the condition matches the given facts.
-
#priority(value = nil) ⇒ Integer
Get or set the priority for this rule.
-
#to_h ⇒ Hash
Serialize rule metadata to a hash.
Methods included from Helpers
Constructor Details
#initialize(name, tags: []) ⇒ Rule
Returns a new instance of Rule.
20 21 22 23 24 25 26 27 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 20 def initialize(name, tags: []) @name = name @tags = .map(&:to_sym) @priority = 0 @condition = nil @action = nil @enabled = true end |
Instance Attribute Details
#enabled ⇒ Boolean
Returns whether the rule is enabled.
16 17 18 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 16 def enabled @enabled end |
#name ⇒ String (readonly)
Returns the rule name.
10 11 12 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 10 def name @name end |
#tags ⇒ Array<Symbol> (readonly)
Returns the rule tags.
13 14 15 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 13 def @tags end |
Instance Method Details
#action {|facts| ... } ⇒ void
This method returns an undefined value.
Set the action for this rule.
41 42 43 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 41 def action(&block) @action = block end |
#condition {|facts| ... } ⇒ void
This method returns an undefined value.
Set the condition for this rule.
33 34 35 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 33 def condition(&block) @condition = block end |
#execute(facts) ⇒ Object
Execute the action with the given facts.
71 72 73 74 75 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 71 def execute(facts) return nil unless @action instance_exec(facts, &@action) end |
#matches?(facts) ⇒ Boolean
Check if the condition matches the given facts.
61 62 63 64 65 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 61 def matches?(facts) return false unless @condition !!instance_exec(facts, &@condition) end |
#priority(value = nil) ⇒ Integer
Get or set the priority for this rule.
49 50 51 52 53 54 55 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 49 def priority(value = nil) if value.nil? @priority else @priority = value end end |
#to_h ⇒ Hash
Serialize rule metadata to a hash.
80 81 82 83 84 85 86 87 |
# File 'lib/philiprehberger/rule_engine/rule.rb', line 80 def to_h { name: @name, priority: @priority, enabled: @enabled, tags: @tags } end |