class Ergo::Rule

Rule class encapsulates a rule definition.

Attributes

state[R]

Access logic condition.

Returns [State]

Public Class Methods

new(state, options={}, &procedure) click to toggle source

Initialize new instanance of Rule.

state - State condition. [Logic] procedure - Procedure to run if logic condition is met. [Proc]

Options

desc - Description of rule. [String]
mark - List of bookmark names. [Array<String>]
# File lib/ergo/rule.rb, line 15
def initialize(state, options={}, &procedure)
  self.state   = state
  self.desc    = options[:desc] || options[:description]
  self.mark    = options[:mark] || options[:bookmarks]
  self.private = options[:private]

  @proc = procedure
end

Public Instance Methods

apply(digest) click to toggle source

Apply rule, running the rule’s procedure if the state condition is satisfied.

Returns nothing.

# File lib/ergo/rule.rb, line 71
def apply(digest)
  case state
  when true
    call
  when false, nil
  else
    result_set = state.call(digest)
    if result_set && !result_set.empty?
      call(result_set)
    end
  end
end
Also aliased as: invoke
bookmark?(name) click to toggle source
# File lib/ergo/rule.rb, line 49
def bookmark?(name)
  @bookmarks.include?(name.to_s)
end
Also aliased as: mark?
bookmarks() click to toggle source

Rule bookmarks.

Returns [Array<String>]

# File lib/ergo/rule.rb, line 44
def bookmarks
  @bookmarks
end
description() click to toggle source

Description of rule.

Returns [String]

# File lib/ergo/rule.rb, line 32
def description
  @description
end
Also aliased as: to_s
invoke(digest)

Alias for apply.

Alias for: apply
mark?(name)
Alias for: bookmark?
private?() click to toggle source

Is the rule private? A private rule does not run with the “master book”, only when it’s specific book is invoked.

# File lib/ergo/rule.rb, line 56
def private?
  @private
end
to_a() click to toggle source

Convenience method for producing a rule list.

Rertuns [Array]

# File lib/ergo/rule.rb, line 90
def to_a
  [description, bookmarks, private?]
end
to_proc() click to toggle source

Rule procedure.

Returns [Proc]

# File lib/ergo/rule.rb, line 63
def to_proc
  @proc
end
to_s()

Returns the description.

Returns [String]

Alias for: description

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/ergo/rule.rb, line 123
def call(*result_set)
  if @proc.arity == 0
    @proc.call
  else
    #@procedure.call(session, *args)
    @proc.call(*result_set)
  end
end
desc=(string) click to toggle source

Set description of rule.

# File lib/ergo/rule.rb, line 114
def desc=(string)
  @description = string.to_s
end
mark=(names) click to toggle source

Set bookmark(s) of rule.

# File lib/ergo/rule.rb, line 103
def mark=(names)
  @bookmarks = Array(names).map{ |b| b.to_s }
end
private=(boolean) click to toggle source

Set privacy of rule. A private rule does not run with the “master book”, only when it’s specific book is invoked.

# File lib/ergo/rule.rb, line 109
def private=(boolean)
  @private = !! boolean
end
state=(state) click to toggle source

Set state of rule.

# File lib/ergo/rule.rb, line 97
def state=(state)
  #raise unless State === state || Boolean === state
  @state = state
end