Class: Philiprehberger::RuleEngine::Rule

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Helpers

#all?, #any?, #none?

Constructor Details

#initialize(name, tags: []) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • name (String)

    the rule name

  • tags (Array<Symbol>) (defaults to: [])

    optional tags for grouping



20
21
22
23
24
25
26
27
# File 'lib/philiprehberger/rule_engine/rule.rb', line 20

def initialize(name, tags: [])
  @name = name
  @tags = tags.map(&:to_sym)
  @priority = 0
  @condition = nil
  @action = nil
  @enabled = true
end

Instance Attribute Details

#enabledBoolean

Returns whether the rule is enabled.

Returns:

  • (Boolean)

    whether the rule is enabled



16
17
18
# File 'lib/philiprehberger/rule_engine/rule.rb', line 16

def enabled
  @enabled
end

#nameString (readonly)

Returns the rule name.

Returns:

  • (String)

    the rule name



10
11
12
# File 'lib/philiprehberger/rule_engine/rule.rb', line 10

def name
  @name
end

#tagsArray<Symbol> (readonly)

Returns the rule tags.

Returns:

  • (Array<Symbol>)

    the rule tags



13
14
15
# File 'lib/philiprehberger/rule_engine/rule.rb', line 13

def tags
  @tags
end

Instance Method Details

#action {|facts| ... } ⇒ void

This method returns an undefined value.

Set the action for this rule.

Yields:

  • (facts)

    block that receives facts and performs the action



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.

Yields:

  • (facts)

    block that receives facts and returns truthy/falsy



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.

Parameters:

  • facts (Object)

    the facts to act on

Returns:

  • (Object)

    the action result



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.

Parameters:

  • facts (Object)

    the facts to evaluate

Returns:

  • (Boolean)


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.

Parameters:

  • value (Integer, nil) (defaults to: nil)

    priority value (lower runs first); omit to get current value

Returns:

  • (Integer)

    the current priority



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_hHash

Serialize rule metadata to a hash.

Returns:

  • (Hash)

    rule metadata (name, priority, enabled)



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