Class: ActiveZuora::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_zuora/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Generator

Returns a new instance of Generator.


6
7
8
9
10
11
# File 'lib/active_zuora/generator.rb', line 6

def initialize(document, options={})
  # document is a parsed wsdl document.
  @document = document
  @classes = []
  @class_nesting = options[:inside] || ActiveZuora
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.


4
5
6
# File 'lib/active_zuora/generator.rb', line 4

def classes
  @classes
end

#documentObject (readonly)

Returns the value of attribute document.


4
5
6
# File 'lib/active_zuora/generator.rb', line 4

def document
  @document
end

Instance Method Details

#generate_classesObject


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
76
77
78
79
80
81
82
83
# File 'lib/active_zuora/generator.rb', line 13

def generate_classes

  # Defines the classes based on the wsdl document.
  # Assumes the following namespaces in the wsdl.
  # xmlns="http://schemas.xmlsoap.org/wsdl/"
  # xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
  # xmlns:xs="http://www.w3.org/2001/XMLSchema"
  # xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  # xmlns:zns="http://api.zuora.com/"
  # xmlns:ons="http://object.api.zuora.com/"
  # xmlns:fns="http://fault.api.zuora.com/"
  @document.xpath('.//xs:schema[@targetNamespace]').each do |schema|
    namespace = schema.attribute("targetNamespace").value

    schema.xpath('.//xs:complexType[@name]').each do |complex_type|
      class_name = complex_type.attribute("name").value
      # Skip the zObject base class, we define our own.
      next if class_name == "zObject"

      zuora_class = Class.new
      @class_nesting.const_set(class_name, zuora_class)
      @classes << zuora_class

      # Include the Base module for adding fields.
      zuora_class.send :include, Base
      zuora_class.namespace = namespace

      # If it's a zObject, include that module as well.
      if complex_type.xpath(".//xs:extension[@base='ons:zObject']").any?
        zuora_class.send :include, ZObject
      end

      # Define the fields
      complex_type.xpath('.//xs:element[@name][@type]').each do |element|
        # attributes: name, type, nillable, minoccurs, maxoccurs
        zuora_name = element.attribute("name").value
        field_name = zuora_name.underscore
        field_type = element.attribute("type").value
        is_array = element_is_an_array?(element)

        case field_type
        when "string", "xs:string", "zns:ID", "xs:base64Binary"
          zuora_class.field field_name, :string,
            :zuora_name => zuora_name, :array => is_array
        when "boolean", "xs:boolean"
          zuora_class.field field_name, :boolean,
            :zuora_name => zuora_name, :array => is_array
        when "int", "short", "long", "xs:int"
          zuora_class.field field_name, :integer,
            :zuora_name => zuora_name, :array => is_array
        when "decimal"
          zuora_class.field field_name, :decimal,
            :zuora_name => zuora_name, :array => is_array
        when "dateTime"
          zuora_class.field field_name, :datetime,
            :zuora_name => zuora_name, :array => is_array
        when /\A(zns:|ons:)/
          zuora_class.field field_name, :object,
            :zuora_name => zuora_name, :array => is_array,
            :class_name => zuora_class.nested_class_name(field_type.split(':').last)
        else
          puts "Unkown field type: #{field_type}"
        end
      end # each element

    end # each complexType
  end # each schema

  add_obvious_associations
  add_extra_customizations
end