Module: ROXML::InstanceMethods::Construction

Defined in:
lib/roxml.rb

Instance Method Summary collapse

Instance Method Details

#xml_initializeObject

xml_initialize is called at the end of the #from_xml operation on objects where xml_construct is not in place. Override xml_initialize in order to establish post-import behavior. For example, you can use xml_initialize to map xml attribute values into the object standard initialize function, thus enabling a ROXML object to freely be either xml-backed or instantiated directly via #new. An example of this follows:

class Measurement
  include ROXML

  xml_reader :units, :attr
  xml_reader :value, :content

  def xml_initialize
    # the object is instantiated, and all xml attributes are imported
    # and available, i.e., value and units below are the same value and units
    # found in the xml via the xml_reader declarations above.
    initialize(value, units)
  end

  def initialize(value, units = 'pixels')
    @value = Float(value)
    @units = units.to_s
    if @units.starts_with? 'hundredths-'
      @value /= 100
      @units = @units.split('hundredths-')[1]
    end
  end
end

#xml_initialize may be written to take arguments, in which case extra arguments from from_xml will be passed into the function.



65
66
# File 'lib/roxml.rb', line 65

def xml_initialize
end