Class: RXSD::RubyClassBuilder

Inherits:
ClassBuilder show all
Defined in:
lib/rxsd/builders/ruby_class.rb

Overview

Implements the RXSD::ClassBuilder interface to build Ruby Classes from a xsd schema

Instance Attribute Summary

Attributes inherited from ClassBuilder

#associated_builder, #attribute_builders, #attribute_name, #base_builder, #klass, #klass_name

Instance Method Summary collapse

Methods inherited from ClassBuilder

#associated, #base=, #clone, #initialize

Constructor Details

This class inherits a constructor from RXSD::ClassBuilder

Instance Method Details

#buildObject

implementation of RXSD::ClassBuilder::build



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rxsd/builders/ruby_class.rb', line 13

def build
   # return if already built
   return @klass unless @klass.nil?

   # need the class name to build class
   return nil    if @klass_name.nil?

   # return if we can find constant corresponding to class name
   if Object.constants.include? @klass_name
     @klass = @klass_name.constantize
     return @klass
   end

   Logger.debug "building class #{@klass}/#{@klass_name} from xsd"

   # determine object's superclass, creating it if need be
   superclass = Object
   unless @base_builder.nil?
     if @base_builder.klass.nil?
       @base_builder = RubyClassBuilder.new(:builder => @base_builder)
       @base_builder.build
     end
     superclass = @base_builder.klass
   end

   # create class
   Object.const_set(@klass_name, Class.new(superclass))
   @klass = @klass_name.constantize

   # FIXME should only do this if the klass corresponds to a simple type
   @klass.class_method :from_s do |str|
         new(:superclass_value => superclass.from_s(str))
   end
   @klass.send :define_method, :initialize do |*args|
     args = args.first || Hash.new
     if !args.nil? && args.has_key?(:superclass_value)
        if Parser.is_builtin? superclass
           super(args[:superclass_value])
        else
           super(:superclass_value => args[:superclass_value])
        end
     end
   end

   # define accessors for attributes
   @attribute_builders.each { |atb|
     unless atb.nil?
       att_name = nil
       if !atb.attribute_name.nil?
          att_name = atb.attribute_name.underscore
       elsif !atb.klass_name.nil?
          att_name = atb.klass_name.underscore
       elsif !atb.klass.nil?
          att_name = atb.klass.to_s.underscore
       end

       @klass.send :attr_accessor,  att_name.intern unless att_name.nil?
     end
   }

   Logger.debug "class #{@klass} built, returning"
   return @klass
end