Class: XML::Digester::LinkRule

Inherits:
RulesBase show all
Defined in:
lib/xml/digestr.rb

Overview

Rule that allows links to be made between the top two stack objects using a supplied block. See SetNextRule and SetTopRule for common-case specializations of this class. Operates within the end event.

Direct Known Subclasses

SetNextRule, SetTopRule

Instance Attribute Summary

Attributes inherited from RulesBase

#digester, #next, #pattern, #prev

Instance Method Summary collapse

Methods inherited from RulesBase

#begin, #body, #finish

Constructor Details

#initialize(pattern, &blk) ⇒ LinkRule

call-seq:

LinkRule.new(pattern) { |parent, child| ... }

Create a new LinkRule that, when matched, will pass the top two elements (in order - see below) to the supplied block.

The notion of ‘parent’ and ‘child’ is quite arbitrary, and merely reflects the common usage of this rule. Specifically, the ‘parent’ is the next-to-top stack element, while the ‘child’ is the top element itself.



293
294
295
296
# File 'lib/xml/digestr.rb', line 293

def initialize(pattern, &blk)
  super(pattern)
  @blk = blk or raise ArgumentError, "No block given"
end

Instance Method Details

#end(namespace, name) ⇒ Object

:nodoc:



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/xml/digestr.rb', line 298

def end(namespace, name) #:nodoc:
  stk = @digester.stack
  if stk.length > 1
    @blk.call(stk[-2], stk[-1])
  else
    if @digester.pedantic?
      raise Error,
        "Cannot link: no parent on stack: #{digester.stack.inspect}"
    end
  end
end