Class: XSD::BaseObject
- Inherits:
-
Object
- Object
- XSD::BaseObject
- Defined in:
- lib/xsd/base_object.rb
Overview
Base object
Direct Known Subclasses
All, Annotation, Any, AnyAttribute, Appinfo, Attribute, AttributeGroup, Choice, ComplexContent, ComplexType, Documentation, Element, Extension, Facet, Field, Group, Import, Include, Key, Keyref, List, Restriction, Schema, Selector, Sequence, SimpleContent, SimpleType, Union, Unique
Constant Summary collapse
- NO_ELEMENTS_CONTAINER =
Objects that can not have nested elements
%i[annotation simpleType attributeGroup attribute unique union simpleContent list any anyAttribute key keyref].freeze
- NO_ATTRIBUTES_CONTAINER =
Objects that cannot have nested attributes
%i[annotation unique anyAttribute all attribute choice sequence group simpleType facet key keyref].freeze
- XML_SCHEMA =
Base XMLSchema namespace
'http://www.w3.org/2001/XMLSchema'
Class Attribute Summary collapse
-
.children ⇒ Object
readonly
Returns the value of attribute children.
-
.links ⇒ Object
readonly
Returns the value of attribute links.
-
.properties ⇒ Object
readonly
Returns the value of attribute properties.
Instance Attribute Summary collapse
-
#id ⇒ Object
Optional.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Get element or attribute by path.
-
#collect_attributes ⇒ Object
Get all available attributes on the current stack level.
-
#collect_elements ⇒ Object
Get all available elements on the current stack level.
-
#documentation ⇒ Object
Return element documentation.
-
#documentation_for(node) ⇒ Object
Return documentation for specified node.
-
#get_prefix(name) ⇒ Object
Get namespace prefix from node name.
-
#initialize(options = {}) ⇒ BaseObject
constructor
A new instance of BaseObject.
-
#inspect ⇒ Object
Get object string representation.
-
#map_child(name) ⇒ Object
Get child object.
-
#map_children(name) ⇒ Object
Get child objects.
-
#namespaces ⇒ Object
Get current namespaces.
-
#node ⇒ Object
Get current XML node.
-
#node_to_object(node) ⇒ Object
Get reader object for node.
-
#nodes(name = :*, deep = false) ⇒ Object
Get child nodes.
-
#object_by_name(node_name, name) ⇒ Object
Search node by name in all available schemas and return its object.
-
#parent ⇒ Object
Get xml parent object.
-
#reader ⇒ Object
Get reader instance.
-
#schema ⇒ Object
Get current schema object.
-
#schemas_for_namespace(ns_or_prefix) ⇒ Object
Get schemas by namespace or prefix.
-
#strip_prefix(name) ⇒ Object
Strip namespace prefix from node name.
Constructor Details
#initialize(options = {}) ⇒ BaseObject
Returns a new instance of BaseObject.
35 36 37 38 39 40 |
# File 'lib/xsd/base_object.rb', line 35 def initialize( = {}) @options = @cache = {} raise Error, "#{self.class}.new expects a hash parameter" unless @options.is_a?(Hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
Lookup for properties
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/xsd/base_object.rb', line 309 def method_missing(method, *args) # check cache first return @cache[method] if @cache[method] # check for property first if (property = self.class.properties[method]) value = property[:resolve] ? property[:resolve].call(self) : node[property[:name].to_s] r = if value.nil? if node['ref'] # if object has reference - search property in referenced object reference.send(method, *args) else property[:default].is_a?(Proc) ? instance_eval(&property[:default]) : property[:default] end else case property[:type] when :integer property[:name] == :maxOccurs && value == 'unbounded' ? :unbounded : value.to_i when :boolean value == 'true' else value end end return @cache[method] = r end # if object has ref it cannot contain any type and children, so proxy call to target object if node['ref'] && method != :ref && method != :reference return reference.send(method, *args) end # then check for linked types if (link = self.class.links[method]) name = link[:property] ? send(link[:property]) : nil if name return @cache[method] = object_by_name(link[:type], name) elsif is_a?(Restriction) && %i[base_simple_type base_complex_type].include?(method) # handle restriction without base return nil end end # last check for nested objects if (child = self.class.children[method]) result = child[:type].is_a?(Array) ? map_children(child[:type][0]) : map_child(child[:type]) return @cache[method] = result end super # api = self.class.properties.keys + self.class.links.keys + self.class.children.keys # raise Error, "Tried to access unknown object '#{method}' on '#{self.class.name}'. Available options are: #{api}" end |
Class Attribute Details
.children ⇒ Object (readonly)
Returns the value of attribute children.
20 21 22 |
# File 'lib/xsd/base_object.rb', line 20 def children @children end |
.links ⇒ Object (readonly)
Returns the value of attribute links.
20 21 22 |
# File 'lib/xsd/base_object.rb', line 20 def links @links end |
.properties ⇒ Object (readonly)
Returns the value of attribute properties.
20 21 22 |
# File 'lib/xsd/base_object.rb', line 20 def properties @properties end |
Instance Attribute Details
#id ⇒ Object
Optional. Specifies a unique ID for the element property :id, :string
58 59 60 |
# File 'lib/xsd/base_object.rb', line 58 def id node['id'] end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/xsd/base_object.rb', line 6 def @options end |
Instance Method Details
#[](*args) ⇒ Object
Get element or attribute by path
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/xsd/base_object.rb', line 94 def [](*args) result = self args.flatten.each do |curname| next if result.nil? curname = curname.to_s if curname[0] == '@' result = result.collect_attributes.find { |attr| definition_match?(attr, curname[1..]) } else result = result.collect_elements.find { |elem| definition_match?(elem, curname) } end end result end |
#collect_attributes ⇒ Object
Get all available attributes on the current stack level
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/xsd/base_object.rb', line 237 def collect_attributes(*) return @collect_attributes if @collect_attributes r = if NO_ATTRIBUTES_CONTAINER.include?(self.class.mapped_name) [] elsif is_a?(Referenced) && ref reference.collect_attributes else # map children recursive map_children(:*).map do |obj| if obj.is_a?(Attribute) || obj.is_a?(AnyAttribute) obj else # get attributes considering references (obj.is_a?(Referenced) && obj.ref ? obj.reference : obj).collect_attributes end end.flatten end @collect_attributes = r end |
#collect_elements ⇒ Object
Get all available elements on the current stack level
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/xsd/base_object.rb', line 213 def collect_elements(*) return @collect_elements if @collect_elements r = if NO_ELEMENTS_CONTAINER.include?(self.class.mapped_name) [] elsif is_a?(Referenced) && ref reference.collect_elements else # map children recursive map_children(:*).map do |obj| if obj.is_a?(Element) || obj.is_a?(Any) obj else # get elements considering references (obj.is_a?(Referenced) && obj.ref ? obj.reference : obj).collect_elements end end.flatten end @collect_elements = r end |
#documentation ⇒ Object
Return element documentation
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/xsd/base_object.rb', line 191 def documentation @documentation ||= begin docs = documentation_for(node) if docs.any? docs elsif is_a?(Referenced) && referenced? documentation_for(reference.node) else [] end end end |
#documentation_for(node) ⇒ Object
Return documentation for specified node
207 208 209 |
# File 'lib/xsd/base_object.rb', line 207 def documentation_for(node) node.xpath('./xs:annotation/xs:documentation/text()', { 'xs' => XML_SCHEMA }).map { |x| x.text.strip } end |
#get_prefix(name) ⇒ Object
Get namespace prefix from node name
185 186 187 |
# File 'lib/xsd/base_object.rb', line 185 def get_prefix(name) name&.include?(':') ? name.split(':').first : nil end |
#inspect ⇒ Object
Get object string representation
50 51 52 |
# File 'lib/xsd/base_object.rb', line 50 def inspect "#<#{self.class.name} path=#{node.path}>" end |
#map_child(name) ⇒ Object
Get child object
171 172 173 |
# File 'lib/xsd/base_object.rb', line 171 def map_child(name) map_children(name).first end |
#map_children(name) ⇒ Object
Get child objects
164 165 166 |
# File 'lib/xsd/base_object.rb', line 164 def map_children(name) nodes(name).map { |node| node_to_object(node) } end |
#namespaces ⇒ Object
Get current namespaces
64 65 66 |
# File 'lib/xsd/base_object.rb', line 64 def namespaces node.namespaces || {} end |
#node ⇒ Object
Get current XML node
44 45 46 |
# File 'lib/xsd/base_object.rb', line 44 def node [:node] end |
#node_to_object(node) ⇒ Object
Get reader object for node
139 140 141 142 143 144 145 146 147 |
# File 'lib/xsd/base_object.rb', line 139 def node_to_object(node) # check object in cache first return reader.object_cache[node.object_id] if reader.object_cache[node.object_id] klass = XML::CLASS_MAP[node.name] raise Error, "Object class not found for '#{node.name}'" unless klass reader.object_cache[node.object_id] = klass.new(.merge(node: node, schema: schema)) end |
#nodes(name = :*, deep = false) ⇒ Object
Get child nodes
71 72 73 |
# File 'lib/xsd/base_object.rb', line 71 def nodes(name = :*, deep = false) node.xpath("./#{deep ? '/' : ''}xs:#{name}", { 'xs' => XML_SCHEMA }) end |
#object_by_name(node_name, name) ⇒ Object
Search node by name in all available schemas and return its object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/xsd/base_object.rb', line 116 def object_by_name(node_name, name) # get prefix and local name name_prefix = get_prefix(name) name_local = strip_prefix(name) # do not search for built-in types return nil if schema.namespace_prefix == name_prefix # determine schema for namespace search_schemas = schemas_for_namespace(name_prefix) # find element in target schema search_schemas.each do |s| node = s.node.at_xpath("./xs:#{node_name}[@name=\"#{name_local}\"]", { 'xs' => XML_SCHEMA }) return s.node_to_object(node) if node end nil end |
#parent ⇒ Object
Get xml parent object
151 152 153 |
# File 'lib/xsd/base_object.rb', line 151 def parent node.respond_to?(:parent) && node.parent ? node_to_object(node.parent) : nil end |
#reader ⇒ Object
Get reader instance
261 262 263 |
# File 'lib/xsd/base_object.rb', line 261 def reader [:reader] end |
#schema ⇒ Object
Get current schema object
157 158 159 |
# File 'lib/xsd/base_object.rb', line 157 def schema [:schema] end |
#schemas_for_namespace(ns_or_prefix) ⇒ Object
Get schemas by namespace or prefix
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xsd/base_object.rb', line 78 def schemas_for_namespace(ns_or_prefix) # resolve namespace for current node if prefix was provided prefix = node.namespaces[['xmlns', (ns_or_prefix == '' ? nil : ns_or_prefix)].compact.join(':')] ns = prefix || ns_or_prefix if schema.targets_namespace?(ns) [schema, *schema.includes.map(&:imported_schema)] elsif (import = schema.import_by_namespace(ns)) [import.imported_schema] else raise Error, "Schema not found for namespace '#{ns}' in '#{schema.id || schema.target_namespace}'" end end |
#strip_prefix(name) ⇒ Object
Strip namespace prefix from node name
178 179 180 |
# File 'lib/xsd/base_object.rb', line 178 def strip_prefix(name) name&.include?(':') ? name.split(':').last : name end |