Class: Sawtooth::Rules::CallRule

Inherits:
Base
  • Object
show all
Defined in:
lib/sawtooth/rules/call_rule.rb

Overview

The call rule allows to execute custom blocks of code upon start or finish.

CallRule.new do
  on_start  { |doc, name, attrs| doc << name }
  on_finish { |doc, name, text| doc.pop }
end

Instance Method Summary collapse

Methods inherited from Base

#print_rule

Constructor Details

#initialize(options = {}, &block) ⇒ CallRule

Should be initialized with a block, the block invokes the current instance.



18
19
20
21
# File 'lib/sawtooth/rules/call_rule.rb', line 18

def initialize(options = {}, &block)
  @options = options || {}
  self.instance_eval(&block) if block_given?
end

Instance Method Details

#finish(path, doc, node) ⇒ Object

Called when the end of a matching XML node is encountered. If an element has no body, this method is called with an empty string instead.

  • doc, the current sawtooth document stack

  • node, the current node



47
48
49
# File 'lib/sawtooth/rules/call_rule.rb', line 47

def finish(path, doc, node)
  invoke_block @options[:finish], doc, node
end

#on_finish(&block) ⇒ Object

Configure the ‘on_finish` block.



29
30
31
# File 'lib/sawtooth/rules/call_rule.rb', line 29

def on_finish(&block)
  @options[:finish] = block
end

#on_start(&block) ⇒ Object

Configure the ‘on_start` block.



24
25
26
# File 'lib/sawtooth/rules/call_rule.rb', line 24

def on_start(&block)
  @options[:start] = block
end

#start(path, doc, node) ⇒ Object

Called when the beginning of a matching XML node is encountered.

  • doc, the current sawtooth document stack

  • node, the current node



37
38
39
# File 'lib/sawtooth/rules/call_rule.rb', line 37

def start(path, doc, node)
  invoke_block @options[:start], doc, node
end