Class: Ergo::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ergo/rule.rb

Overview

Rule class encapsulates a rule definition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, options = {}, &procedure) ⇒ Rule

Initialize new instanance of Rule.

Parameters:

  • state

    State condition. [Logic]

  • procedure

    Procedure to run if logic condition is met. [Proc]



15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#stateState

Access logic condition.

Returns:



27
28
29
# File 'lib/ergo/rule.rb', line 27

def state
  @state
end

Instance Method Details

#apply(digest) ⇒ void Also known as: invoke

This method returns an undefined value.

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



71
72
73
74
75
76
77
78
79
80
81
82
# 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

#bookmark?(name) ⇒ Boolean Also known as: mark?

Returns:



49
50
51
# File 'lib/ergo/rule.rb', line 49

def bookmark?(name)
  @bookmarks.include?(name.to_s)
end

#bookmarksArray<String>

Rule bookmarks.

Returns:

  • (Array<String>)

    Returns



44
45
46
# File 'lib/ergo/rule.rb', line 44

def bookmarks
  @bookmarks
end

#call(*result_set) ⇒ Object (protected)

Run rule procedure.

Parameters:

  • result_set

    The result set returned by the logic condition.

Returns:

  • (Object)

    Returns whatever the procedure returns.



123
124
125
126
127
128
129
130
# 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) ⇒ Object (protected)

Set description of rule.



114
115
116
# File 'lib/ergo/rule.rb', line 114

def desc=(string)
  @description = string.to_s
end

#descriptionString Also known as: to_s

Description of rule.

Returns:

  • (String)

    Returns



32
33
34
# File 'lib/ergo/rule.rb', line 32

def description
  @description
end

#mark=(names) ⇒ Object (protected)

Set bookmark(s) of rule.



103
104
105
# File 'lib/ergo/rule.rb', line 103

def mark=(names)
  @bookmarks = Array(names).map{ |b| b.to_s }
end

#private=(boolean) ⇒ Object (protected)

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



109
110
111
# File 'lib/ergo/rule.rb', line 109

def private=(boolean)
  @private = !! boolean
end

#private?Boolean

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

Returns:



56
57
58
# File 'lib/ergo/rule.rb', line 56

def private?
  @private
end

#to_aArray

Convenience method for producing a rule list.

Rertuns [Array]

Returns:



90
91
92
# File 'lib/ergo/rule.rb', line 90

def to_a
  [description, bookmarks, private?]
end

#to_procProc

Rule procedure.

Returns:

  • (Proc)

    Returns



63
64
65
# File 'lib/ergo/rule.rb', line 63

def to_proc
  @proc
end