Class: RXSD::RubyObjectBuilder

Inherits:
ObjectBuilder show all
Defined in:
lib/rxsd/builders/ruby_object.rb

Overview

Implements the RXSD::ObjectBuilder interface to build Ruby Objects from a xsd-conforming xml doc

Instance Attribute Summary

Attributes inherited from ObjectBuilder

#attributes, #children, #content, #obj, #parent, #tag_name

Instance Method Summary collapse

Methods inherited from ObjectBuilder

#initialize

Constructor Details

This class inherits a constructor from RXSD::ObjectBuilder

Instance Method Details

#build(schema) ⇒ Object

implementation of RXSD::ObjectBuilder::build



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rxsd/builders/ruby_object.rb', line 12

def build(schema)
   # return object if already built
   return @obj unless @obj.nil?
   
   Logger.debug "instantiating class #{@tag_name} from xsd"

   # find class builder corresponding to tag_name to instantiate
   tags   = schema.tags
   klass  = tags[@tag_name].klass

   # instantiate the target class
   if @content.nil? # not a text based obj, construct normally
     @obj = klass.new
   elsif klass == Array # special case when instantiating arrays, need to specify item type
     @obj = klass.from_s @content, tags[@tag_name].associated_builder.klass
   else
     @obj = klass.from_s @content
   end

   # go through each attribute, find corresponding class builder, 
   # instantiate, and assign to object
   @attributes.each { |atn, atv|
     if tags.has_key? @tag_name + ":" + atn # FIXME how do we want to handle attributes that are not in the schema (eg the else here)
       aklass  = tags[@tag_name + ":" + atn].klass
       if aklass == Array # special case when instantiating arrays, need to specify item type
         val = aklass.from_s atv, tags[@tag_name + ":" + atn].associated_builder.klass
       else
         val = aklass.from_s atv
       end
       @obj.send("#{atn.underscore}=".intern, val)
     end
   }

   # instantiate each child using builder and assign to object
   @children.each { |child|
     cob  = RubyObjectBuilder.new(:builder => child)
     cobj = cob.build(schema)
     @obj.send("#{cob.tag_name.underscore}=".intern, cobj)
   }

   Logger.debug "object type #{@tag_name} instantiated, returning"

   return @obj
end