Class: DSLize::Formatter::XSD

Inherits:
Base show all
Defined in:
lib/dslize/formatter/xsd_formatter.rb

Instance Attribute Summary

Attributes inherited from Base

#objects, #superclasses

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DSLize::Formatter::Base

Instance Method Details

#generate!(*args) ⇒ Object



8
9
10
11
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
# File 'lib/dslize/formatter/xsd_formatter.rb', line 8

def generate!(*args)
  schema = XPain::Builder.new do |xsd|
    xsd.schema do
      objects.each do |name, options|
        parent = superclasses[name]
        
        xsd.element(name, :type => (options[:type] or name))
        
        xsd.complexType({ :name => name, :abstract => options[:abstract], :mixed => false }.select { |k,v| !v.nil? }) do
          extends_base_if_needed(xsd, parent) do
            
            # relations
            xsd.sequence do
              { :has_one => nil, :has_many => "unbounded"}.each do |relation, max_occurs|
                (options[relation] or []).each do |k, v|
                  named_object_if_needed(xsd, k, v) do
                    xsd.element(nil, { :ref => (v[:type] or k), :maxOccurs => max_occurs }.select { |k,v| !v.nil? })
                  end
                end
              end
            end
          
            # attributes
            (options[:attributes] or []).each do |k, v|
              xsd.attribute({ :name => k, :default => v[:default], :type => xsd_type(v[:type]) }.select { |k,v| !v.nil? })
            end
            
          end
        end
        
      end
    end
  end
  
  File.open(args[0], "w") do |f|
    f << schema.to_xml.gsub(' name=""', '') # xpain is buggy and always write a name on <element>
  end
end