Class: Schematic::Generator::Xsd
- Inherits:
-
Object
- Object
- Schematic::Generator::Xsd
- Defined in:
- lib/schematic/generator/xsd.rb
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
-
#options ⇒ Object
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #element_for_klass(builder) ⇒ Object
- #generate(builder, klass) ⇒ Object
- #generate_additional_methods(builder, additional_methods) ⇒ Object
- #generate_column_elements(builder, additional_methods, ignored_methods) ⇒ Object
- #generate_complex_type_for_collection(builder) ⇒ Object
- #generate_complex_type_for_model(builder) ⇒ Object
- #generate_inclusion_value_restrictions(builder, value) ⇒ Object
- #generate_uniqueness_constraints(builder) ⇒ Object
- #header(builder) ⇒ Object
-
#initialize(klass, options = {}) ⇒ Xsd
constructor
A new instance of Xsd.
- #nested_attribute_name(name) ⇒ Object
- #nested_attributes ⇒ Object
- #ns(ns, provider, key) ⇒ Object
- #schema(builder) ⇒ Object
Constructor Details
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
4 5 6 |
# File 'lib/schematic/generator/xsd.rb', line 4 def names @names end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/schematic/generator/xsd.rb', line 5 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/schematic/generator/xsd.rb', line 4 def output @output end |
Instance Method Details
#element_for_klass(builder) ⇒ Object
31 32 33 34 35 |
# File 'lib/schematic/generator/xsd.rb', line 31 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) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/schematic/generator/xsd.rb', line 37 def generate(builder, klass) nested_attributes.each do |nested_attribute| next if nested_attribute.klass == klass next if nested_attribute.klass == klass.superclass next if @options && @options[:generated_types] && @options[:generated_types].include?(nested_attribute.klass) nested_attribute.klass.schematic_sandbox.generate_xsd(builder, klass, @options) @options[:generated_types] << nested_attribute.klass end generate_complex_type_for_collection(builder) generate_complex_type_for_model(builder) end |
#generate_additional_methods(builder, additional_methods) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/schematic/generator/xsd.rb', line 106 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) ⇒ Object
79 80 81 82 83 |
# File 'lib/schematic/generator/xsd.rb', line 79 def generate_column_elements(builder, additional_methods, ignored_methods) @klass.columns.each do |column| Column.new(@klass, column, additional_methods, ignored_methods).generate(builder) end end |
#generate_complex_type_for_collection(builder) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/schematic/generator/xsd.rb', line 50 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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/schematic/generator/xsd.rb', line 59 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 | (@options[:exclude] || []) complex_type.xs :all do |all| generate_column_elements(all, additional_methods, ignored_methods) nested_attributes.each do |nested_attribute| 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" end generate_additional_methods(all, additional_methods) end end end |
#generate_inclusion_value_restrictions(builder, value) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/schematic/generator/xsd.rb', line 91 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
85 86 87 88 89 |
# File 'lib/schematic/generator/xsd.rb', line 85 def generate_uniqueness_constraints(builder) @klass.columns.each do |column| Uniqueness.new(@klass, column).generate(builder) end end |
#header(builder) ⇒ Object
19 20 21 |
# File 'lib/schematic/generator/xsd.rb', line 19 def header(builder) builder.instruct! end |
#nested_attribute_name(name) ⇒ Object
152 153 154 |
# File 'lib/schematic/generator/xsd.rb', line 152 def nested_attribute_name(name) "#{name.to_s.gsub("_", "-").pluralize}-attributes" end |
#nested_attributes ⇒ Object
142 143 144 145 146 |
# File 'lib/schematic/generator/xsd.rb', line 142 def nested_attributes @klass.reflect_on_all_associations.select do |association| @klass.instance_methods.include?("#{association.name}_attributes=".to_sym) && association.[:polymorphic] != true end end |
#ns(ns, provider, key) ⇒ Object
148 149 150 |
# File 'lib/schematic/generator/xsd.rb', line 148 def ns(ns, provider, key) { "xmlns:#{ns}" => Namespaces::PROVIDERS[provider][key] } end |