Class: RSCM::Accurev::XMLMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rscm/scm/accurev/xml.rb

Overview

XMLMapper creates an object tree from an XML element and its children.

For each XML node, it locates a class associated with that node’s name, and builds a new object of that type from the element.

The nodename-class associations are determined by finding all classes derived from ElementBackedClass and checking each of those classes element_name methods.

See ElementBackedClass for examples.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXMLMapper

Returns a new instance of XMLMapper.



24
25
26
27
28
29
30
31
32
33
# File 'lib/rscm/scm/accurev/xml.rb', line 24

def initialize()
  @classmap = {}
  ObjectSpace.each_object( Class ) do |k|
    if k.ancestors.delete( ElementBackedClass ) and k != ElementBackedClass
      if k.respond_to?( :element_name )
        @classmap[ k.element_name ] = k
      end
    end
  end
end

Instance Attribute Details

#classmapObject

Map of element (tag) names to (ruby) class.



22
23
24
# File 'lib/rscm/scm/accurev/xml.rb', line 22

def classmap
  @classmap
end

Instance Method Details

#map(e) ⇒ Object

Build an object tree from the given REXML element and its children. The returned object will be a subclass of ElementBackedClass.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rscm/scm/accurev/xml.rb', line 39

def map( e )
  unless @classmap.has_key?( e.name )
    return nil
  end
  o = @classmap[e.name].new( e )
  e.children.each do |child|
    if child.kind_of?( REXML::Element )
      a = self.map( child )
      unless a.nil?
        if o.children[ a.class.element_name ].nil?
          o.children[ a.class.element_name ] = []
        end
        o.children[ a.class.element_name ] << a
      end
    end
  end
  return o
end