Class: Shale::Schema::XMLGenerator
- Inherits:
-
Object
- Object
- Shale::Schema::XMLGenerator
- Defined in:
- lib/shale/schema/xml_generator.rb,
lib/shale/schema/xml_generator/import.rb,
lib/shale/schema/xml_generator/schema.rb,
lib/shale/schema/xml_generator/element.rb,
lib/shale/schema/xml_generator/attribute.rb,
lib/shale/schema/xml_generator/ref_element.rb,
lib/shale/schema/xml_generator/complex_type.rb,
lib/shale/schema/xml_generator/ref_attribute.rb,
lib/shale/schema/xml_generator/typed_element.rb,
lib/shale/schema/xml_generator/typed_attribute.rb
Overview
Class for generating XML schema
Defined Under Namespace
Classes: Attribute, ComplexType, Element, Import, RefAttribute, RefElement, Schema, TypedAttribute, TypedElement
Constant Summary collapse
- DEFAULT_SCHEMA_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema default name
'schema'
Class Method Summary collapse
-
.get_xml_type(shale_type) ⇒ String
private
Return XML type for given Shale type.
-
.register_xml_type(shale_type, xml_type) ⇒ Object
Register Shale to XML type mapping.
Instance Method Summary collapse
-
#as_schemas(klass, base_name = nil) ⇒ Array<Shale::Schema::XMLGenerator::Schema>
Generate XML Schema from Shale model and return it as a Shale::Schema::XMLGenerator::Schema array.
-
#to_schemas(klass, base_name = nil, pretty: false, declaration: false) ⇒ Hash<String, String>
Generate XML Schema from Shale model.
Class Method Details
.get_xml_type(shale_type) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return XML type for given Shale type
48 49 50 |
# File 'lib/shale/schema/xml_generator.rb', line 48 def self.get_xml_type(shale_type) @xml_types[shale_type] end |
.register_xml_type(shale_type, xml_type) ⇒ Object
Register Shale to XML type mapping
33 34 35 |
# File 'lib/shale/schema/xml_generator.rb', line 33 def self.register_xml_type(shale_type, xml_type) @xml_types[shale_type] = xml_type end |
Instance Method Details
#as_schemas(klass, base_name = nil) ⇒ Array<Shale::Schema::XMLGenerator::Schema>
Generate XML Schema from Shale model and return it as a Shale::Schema::XMLGenerator::Schema array
74 75 76 77 78 79 80 81 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/shale/schema/xml_generator.rb', line 74 def as_schemas(klass, base_name = nil) unless mapper_type?(klass) raise NotAShaleMapperError, "XML Shema can't be generated for '#{klass}' type" end schemas = Hash.new do |hash, key| name = "#{base_name || DEFAULT_SCHEMA_NAME}#{hash.keys.length}.xsd" hash[key] = Schema.new(name, key) end default_namespace = klass.xml_mapping.default_namespace root_element = TypedElement.new( name: klass.xml_mapping.unprefixed_root, type: [default_namespace.prefix, schematize(klass.model.name)].compact.join(':'), required: true ) schemas[default_namespace.name].add_namespace( default_namespace.prefix, default_namespace.name ) schemas[default_namespace.name].add_child(root_element) complexes = [] collect_complex_types(complexes, klass, klass.xml_mapping.default_namespace.name) complexes.each do |complex| type = complex[:type] namespace = complex[:namespace] children = [] type.xml_mapping.elements.values.each do |mapping| attribute = type.attributes[mapping.attribute] next unless attribute xml_type = get_xml_type_for_attribute(attribute.type, mapping.namespace) default = get_default_for_attribute(attribute) if namespace == mapping.namespace.name children << TypedElement.new( name: mapping.name, type: xml_type, collection: attribute.collection?, default: default ) else target_namespace = mapping.namespace target_schema = schemas[target_namespace.name] children << RefElement.new( ref: mapping.prefixed_name, collection: attribute.collection?, default: default ) target_element = TypedElement.new( name: mapping.name, type: xml_type, required: true ) import_element = Import.new( target_namespace.name, target_schema.name ) target_schema.add_namespace( target_namespace.prefix, target_namespace.name ) target_schema.add_child(target_element) schemas[namespace].add_namespace( target_namespace.prefix, target_namespace.name ) schemas[namespace].add_child(import_element) end end type.xml_mapping.attributes.values.each do |mapping| attribute = type.attributes[mapping.attribute] next unless attribute xml_type = get_xml_type_for_attribute(attribute.type, mapping.namespace) default = get_default_for_attribute(attribute) if namespace == mapping.namespace.name children << TypedAttribute.new( name: mapping.name, type: xml_type, default: default ) else target_namespace = mapping.namespace target_schema = schemas[target_namespace.name] children << RefAttribute.new( ref: mapping.prefixed_name, default: default ) target_element = TypedAttribute.new(name: mapping.name, type: xml_type) import_element = Import.new( target_namespace.name, target_schema.name ) target_schema.add_namespace( target_namespace.prefix, target_namespace.name ) target_schema.add_child(target_element) schemas[namespace].add_namespace( target_namespace.prefix, target_namespace.name ) schemas[namespace].add_child(import_element) end end complex = ComplexType.new( schematize(type.model.name), children, mixed: !type.xml_mapping.content.nil? ) schemas[namespace].add_child(complex) end schemas.values end |
#to_schemas(klass, base_name = nil, pretty: false, declaration: false) ⇒ Hash<String, String>
Generate XML Schema from Shale model
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/shale/schema/xml_generator.rb', line 223 def to_schemas(klass, base_name = nil, pretty: false, declaration: false) schemas = as_schemas(klass, base_name) schemas.to_h do |schema| [ schema.name, Shale.xml_adapter.dump(schema.as_xml, pretty: pretty, declaration: declaration), ] end end |