Class: XML::Digester::ObjectCreateRule

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

Overview

Rule that creates an instance of the specified class (or alternatively obtains the result of a call to specified method on the specified object, possibly with custom arguments) and pushes it onto the stack in the begin event, and pops it off again in the end event. This combines the functionality of both ObjectCreateRule and FactoryCreateRule from commons digester.

It is also possible to supply a block, in which case it will be executed when the rule is triggered (in begin) and the result from that pushed. In this case, any class/message will be ignored.

Instance Attribute Summary

Attributes inherited from RulesBase

#digester, #next, #pattern, #prev

Instance Method Summary collapse

Methods inherited from RulesBase

#body, #finish

Constructor Details

#initialize(pattern, klass = Object, msg = :new, *args, &blk) ⇒ ObjectCreateRule

call-seq:

ObjectCreateRule.new(pattern, klass, message = :new, *args)
ObjectCreateRule.new(pattern, obj, message, *args)
ObjectCreateRule.new(pattern) { ... }

Create a new ObjectCreateRule with the specified class, method call or block.



118
119
120
121
# File 'lib/xml/digestr.rb', line 118

def initialize(pattern, klass = Object, msg = :new, *args, &blk)
  super(pattern)
  @klass, @msg, @args, @blk = klass, msg, args, blk
end

Instance Method Details

#begin(namespace, name, attrs) ⇒ Object

:nodoc:



123
124
125
126
127
128
129
# File 'lib/xml/digestr.rb', line 123

def begin(namespace, name, attrs) #:nodoc:
  @digester.push(if b = @blk 
    b.call(*@args)
  else
    @klass.send(@msg, *@args)
  end)
end

#end(namespace, name) ⇒ Object

:nodoc:



131
132
133
# File 'lib/xml/digestr.rb', line 131

def end(namespace, name) #:nodoc:
  @digester.pop
end