Class: Schematic::Generator::Xsd

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Xsd

Returns a new instance of Xsd.



13
14
15
16
# File 'lib/schematic/generator/xsd.rb', line 13

def initialize(klass)
  @klass = klass
  @names = Names.new(klass)
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



10
11
12
# File 'lib/schematic/generator/xsd.rb', line 10

def names
  @names
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/schematic/generator/xsd.rb', line 11

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



10
11
12
# File 'lib/schematic/generator/xsd.rb', line 10

def output
  @output
end

Instance Method Details

#element_for_klass(builder) ⇒ Object



37
38
39
40
41
# File 'lib/schematic/generator/xsd.rb', line 37

def element_for_klass(builder)
  builder.xs :element, 'name' => @names.element_collection, 'type' => @names.collection_type do |element|
    generate_uniqueness_constraints(element)
  end
end

#generate(builder, klass, include_collection = true, exclude_from_parent = []) ⇒ Object



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
# File 'lib/schematic/generator/xsd.rb', line 43

def generate(builder, klass, include_collection=true, exclude_from_parent = [])
  @iteration ||= 0
  @exclude = exclude_from_parent
  nested_attributes.each do |nested_attribute|
    next if nested_attribute.klass == klass
    next if nested_attribute.klass == klass.superclass
    @options ||= {}
    @options[:generated_singular_types] ||= []
    @options[:generated_plural_types] ||= []
    exclude = klass.schematic_sandbox.ignored_elements[nested_attribute.name].dup

    if nested_attribute.macro == :has_many && !@options[:generated_plural_types].include?(nested_attribute.klass)
      nested_attribute.klass.schematic_sandbox.generate_xsd(builder, klass, true, @options, exclude)
      @options[:generated_plural_types] << nested_attribute.klass
    elsif !@options[:generated_singular_types].include?(nested_attribute.klass)
      nested_attribute.klass.schematic_sandbox.generate_xsd(builder, klass, false, @options, exclude)
      @options[:generated_singular_types] << nested_attribute.klass
    end
  end
  if include_collection && !@options[:generated_plural_types].include?(@names.collection_type)
    generate_complex_type_for_collection(builder)
    @options[:generated_plural_types] << @names.collection_type
  end
  unless @options[:generated_singular_types].include?(@names.type)
    generate_complex_type_for_model(builder)
    @options[:generated_singular_types] << @names.type
  end
  builder
end

#generate_additional_methods(builder, additional_methods) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/schematic/generator/xsd.rb', line 160

def generate_additional_methods(builder, additional_methods)
  additional_methods.each do |method_name, values|
    method_xsd_name = method_name.to_s.dasherize
    if values.is_a?(Array) || values.is_a?(Hash)
      builder.xs :element, 'name' => method_xsd_name, 'minOccurs' => '0', 'maxOccurs' => '1' do |element|
        element.xs :complexType do |complex_type|
          if values.is_a?(Array)
            complex_type.xs :sequence do |nested_sequence|
              if values.present?
                values.each do |value|
                  nested_sequence.xs :element, 'name' => value.to_s.dasherize, 'minOccurs' => '0', 'maxOccurs' => 'unbounded' do |sequence_element|
                    generate_inclusion_value_restrictions(sequence_element, value)
                  end
                end
              else
                nested_sequence.xs :any, 'processContents' => 'skip', 'minOccurs' => '0', 'maxOccurs' => 'unbounded'
              end
            end
          elsif values.is_a?(Hash)
            complex_type.xs :all do |nested_all|
              generate_additional_methods(nested_all, values)
            end
          else
            raise 'Additional methods must be a hash of hashes or hash of arrays'
          end
          complex_type.xs :attribute, 'name' => 'type', 'type' => 'xs:string', 'fixed' => 'array', 'use' => 'optional'
        end
      end
    else
      column_klass = Struct.new(:name, :type)
      column = column_klass.new(method_name.to_s, :string)
      Column.new(@klass, column, {}, @klass.schematic_sandbox.ignored_elements).generate(builder)
    end
  end
end

#generate_column_elements(builder, additional_methods, ignored_methods, required_methods, non_required_methods) ⇒ Object



131
132
133
134
135
136
# File 'lib/schematic/generator/xsd.rb', line 131

def generate_column_elements(builder, additional_methods, ignored_methods, required_methods, non_required_methods)
  return unless @klass.respond_to?(:columns)
  @klass.columns.each do |column|
    Column.new(@klass, column, additional_methods, ignored_methods, required_methods, non_required_methods).generate(builder)
  end
end

#generate_complex_type_for_collection(builder) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/schematic/generator/xsd.rb', line 73

def generate_complex_type_for_collection(builder)
  builder.xs :complexType, 'name' => @names.collection_type do |complex_type|
    complex_type.xs :sequence do |sequence|
      sequence.xs :element, 'name' => @names.element, 'type' => @names.type, 'minOccurs' => '0', 'maxOccurs' => 'unbounded'
    end
    complex_type.xs :attribute, 'name' => 'type', 'type' => 'xs:string', 'fixed' => 'array'
  end
end

#generate_complex_type_for_model(builder) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/schematic/generator/xsd.rb', line 82

def generate_complex_type_for_model(builder)
  builder.xs :complexType, 'name' => @names.type do |complex_type|
    additional_methods = @klass.schematic_sandbox.added_elements.merge(@options[:methods] || {})
    ignored_methods = @klass.schematic_sandbox.ignored_elements.dup
    exclude = @exclude

    case exclude
    when Hash
      ignored_methods.merge!(exclude)
    when Array
      exclude.each do |key|
        ignored_methods[key] = nil
      end
    end

    required_methods = @klass.schematic_sandbox.required_elements
    non_required_methods = @klass.schematic_sandbox.non_required_elements

    complex_type.xs :all do |all|
      generate_column_elements(all, additional_methods, ignored_methods, required_methods, non_required_methods)

      nested_attributes.each do |nested_attribute|
        case nested_attribute.macro
        when :has_many
          all.xs :element,
            'name' => nested_attribute_name(nested_attribute.name),
            'type' => nested_attribute.klass.schematic_sandbox.xsd_generator.names.collection_type,
            'minOccurs' => '0',
            'maxOccurs' => '1'
        when :has_one
          all.xs :element,
            'name' => nested_attribute_name(nested_attribute.name, {:pluralized => false}),
            'type' => nested_attribute.klass.schematic_sandbox.xsd_generator.names.type,
            'minOccurs' => '0',
            'maxOccurs' => '1'
        when :belongs_to
          all.xs :element,
            'name' => nested_attribute_name(nested_attribute.name, {:pluralized => false}),
            'type' => nested_attribute.klass.schematic_sandbox.xsd_generator.names.type,
            'minOccurs' => '0',
            'maxOccurs' => '1'
        end
      end

      generate_additional_methods(all, additional_methods)
    end
  end
end

#generate_inclusion_value_restrictions(builder, value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/schematic/generator/xsd.rb', line 145

def generate_inclusion_value_restrictions(builder, value)
  enumeration_method = "xsd_#{value}_enumeration_restrictions".to_sym
  builder.xs :complexType do |complex_type|
    complex_type.xs :simpleContent do |simple_content|
      simple_content.xs :restriction, 'base' => 'String' do |restriction|
        if @klass.respond_to? enumeration_method
          @klass.send(enumeration_method).each do |enumeration|
            restriction.xs :enumeration, 'value' => enumeration
          end
        end
      end
    end
  end
end

#generate_uniqueness_constraints(builder) ⇒ Object



138
139
140
141
142
143
# File 'lib/schematic/generator/xsd.rb', line 138

def generate_uniqueness_constraints(builder)
  return unless @klass.respond_to?(:columns)
  @klass.columns.each do |column|
    Uniqueness.new(@klass, column).generate(builder)
  end
end

#header(builder) ⇒ Object



25
26
27
# File 'lib/schematic/generator/xsd.rb', line 25

def header(builder)
  builder.instruct!
end

#nested_attribute_name(name, options = {}) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/schematic/generator/xsd.rb', line 209

def nested_attribute_name(name, options={})
  pluralized = options.delete(:pluralized)
  pluralized = true if pluralized.nil?
  name = name.to_s.gsub('_', '-')
  name = name.pluralize if pluralized
  "#{name}-attributes"
end

#nested_attributesObject



196
197
198
199
200
201
202
203
# File 'lib/schematic/generator/xsd.rb', line 196

def nested_attributes
  return [] unless @klass.respond_to?(:reflect_on_all_associations)
  @klass.reflect_on_all_associations.select do |association|
    @klass.instance_methods.include?(:"#{association.name}_attributes=") &&
      association.options[:polymorphic] != true &&
      !@klass.schematic_sandbox.ignored_elements[association.name.to_sym].nil?
  end
end

#ns(ns, provider, key) ⇒ Object



205
206
207
# File 'lib/schematic/generator/xsd.rb', line 205

def ns(ns, provider, key)
  { "xmlns:#{ns}" => Namespaces::PROVIDERS[provider][key] }
end

#schema(builder) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/schematic/generator/xsd.rb', line 29

def schema(builder)
  builder.xs :schema, ns('xs', :w3, :schema) do |schema|
    Types.xsd(schema)
    element_for_klass(schema)
    generate(schema, @klass)
  end
end