Class: MyData::Xsd::Doc

Inherits:
Object
  • Object
show all
Defined in:
lib/my_data/xsd/doc.rb

Constant Summary collapse

DEFAULT_ATTRS =
{
  "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Doc

Returns a new instance of Doc.



10
11
12
# File 'lib/my_data/xsd/doc.rb', line 10

def initialize(doc)
  @doc = doc
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/my_data/xsd/doc.rb', line 4

def doc
  @doc
end

Instance Method Details

#attributesObject



21
22
23
24
25
# File 'lib/my_data/xsd/doc.rb', line 21

def attributes
  @attributes ||= DEFAULT_ATTRS
                  .merge(target_namespace_attributes)
                  .merge(namespace_attributes)
end

#complex_typesObject



40
41
42
43
44
# File 'lib/my_data/xsd/doc.rb', line 40

def complex_types
  @complex_types ||= doc.xpath("//xs:schema/xs:complexType").map do |node|
    MyData::Xsd::ComplexType.new(node, namespace: target_namespace)
  end
end

#elementsObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/my_data/xsd/doc.rb', line 27

def elements
  @elements ||=
    begin
      sequence = doc.child.xpath("xs:element/xs:complexType/xs:sequence").first
      elements = sequence.xpath("xs:element")
      collection = elements.count == 1 && sequence.attributes["maxOccurs"].present?

      sequence.xpath("xs:element").map do |element|
        MyData::Xsd::Element.new(element, force_collection: collection)
      end
    end
end

#inspectObject



52
53
54
# File 'lib/my_data/xsd/doc.rb', line 52

def inspect
  "<Schema target_namespace: #{target_namespace.to_json}, attributes: #{attributes}>"
end

#simple_typesObject



46
47
48
49
50
# File 'lib/my_data/xsd/doc.rb', line 46

def simple_types
  @simple_types ||= doc.xpath("//xs:schema/xs:simpleType").map do |node|
    MyData::Xsd::Element.new(node, namespace: target_namespace)
  end
end

#target_namespaceObject



14
15
16
17
18
19
# File 'lib/my_data/xsd/doc.rb', line 14

def target_namespace
  return @target_namespace if defined? @target_namespace

  @target_namespace =
    (doc.namespaces.find { |_k, v| v == target_namespace_value }.first.split(":").last if target_namespace_value)
end