Class: Ergo::Rule
- Inherits:
-
Object
- Object
- Ergo::Rule
- Defined in:
- lib/ergo/rule.rb
Overview
Rule class encapsulates a rule definition.
Instance Attribute Summary collapse
-
#state ⇒ State
Access logic condition.
Instance Method Summary collapse
-
#apply(digest) ⇒ void
(also: #invoke)
Apply rule, running the rule’s procedure if the state condition is satisfied.
- #bookmark?(name) ⇒ Boolean (also: #mark?)
-
#bookmarks ⇒ Array<String>
Rule bookmarks.
-
#call(*result_set) ⇒ Object
protected
Run rule procedure.
-
#desc=(string) ⇒ Object
protected
Set description of rule.
-
#description ⇒ String
(also: #to_s)
Description of rule.
-
#initialize(state, options = {}, &procedure) ⇒ Rule
constructor
Initialize new instanance of Rule.
-
#mark=(names) ⇒ Object
protected
Set bookmark(s) of rule.
-
#private=(boolean) ⇒ Object
protected
Set privacy of rule.
-
#private? ⇒ Boolean
Is the rule private? A private rule does not run with the “master book”, only when it’s specific book is invoked.
-
#to_a ⇒ Array
Convenience method for producing a rule list.
-
#to_proc ⇒ Proc
Rule procedure.
Constructor Details
#initialize(state, options = {}, &procedure) ⇒ Rule
Initialize new instanance of Rule.
15 16 17 18 19 20 21 22 |
# File 'lib/ergo/rule.rb', line 15 def initialize(state, ={}, &procedure) self.state = state self.desc = [:desc] || [:description] self.mark = [:mark] || [:bookmarks] self.private = [:private] @proc = procedure end |
Instance Attribute Details
#state ⇒ State
Access logic condition.
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?
49 50 51 |
# File 'lib/ergo/rule.rb', line 49 def bookmark?(name) @bookmarks.include?(name.to_s) end |
#bookmarks ⇒ Array<String>
Rule bookmarks.
44 45 46 |
# File 'lib/ergo/rule.rb', line 44 def bookmarks @bookmarks end |
#call(*result_set) ⇒ Object (protected)
Run rule procedure.
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 |
#description ⇒ String Also known as: to_s
Description of rule.
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.
56 57 58 |
# File 'lib/ergo/rule.rb', line 56 def private? @private end |
#to_a ⇒ Array
Convenience method for producing a rule list.
Rertuns [Array]
90 91 92 |
# File 'lib/ergo/rule.rb', line 90 def to_a [description, bookmarks, private?] end |
#to_proc ⇒ Proc
Rule procedure.
63 64 65 |
# File 'lib/ergo/rule.rb', line 63 def to_proc @proc end |