class Rulebow::Rule

Rule class encapsulates a rule definition.

Attributes

fact[R]

Access to the rule’s logic condition. [Fact]

Public Class Methods

new(fact, options={}, &action) click to toggle source

Initialize new instanance of Rule.

fact - Conditional fact. [Fact,Boolean] action - Procedure to run if logic condition is met. [Proc]

# File lib/rulebow/rule.rb, line 11
def initialize(fact, options={}, &action)
  @fact   = fact
  @action = action
end

Public Instance Methods

apply(digest) click to toggle source

Apply rule, running the rule’s procedure if the fact is true.

Returns nothing.

# File lib/rulebow/rule.rb, line 29
def apply(digest)
  case fact
  when false, nil
  when true
    call()
  else
    result_set = fact.call(digest)
    if result_set && !result_set.empty?
      call(result_set)
    end
  end
end
Also aliased as: invoke
invoke(digest)

Alias for apply.

Alias for: apply
to_proc() click to toggle source

Access to the rule’s action procedure.

Returns [Proc]

# File lib/rulebow/rule.rb, line 22
def to_proc
  @action
end

Protected Instance Methods

call(*result_set) click to toggle source

Run rule procedure.

result_set - The result set returned by the logic condition.

Returns whatever the procedure returns. [Object]

# File lib/rulebow/rule.rb, line 52
def call(*result_set)
  if @action.arity == 0
    @action.call
  else
    #@action.call(session, *result_set)
    @action.call(*result_set)
  end
end