Class: MyData::Xsd::Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, namespace: nil, force_collection: nil) ⇒ Element

Returns a new instance of Element.



6
7
8
9
10
# File 'lib/my_data/xsd/element.rb', line 6

def initialize(element, namespace: nil, force_collection: nil)
  @element = element
  @namespace = namespace
  force_collection && @collection = force_collection
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



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

def element
  @element
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/my_data/xsd/element.rb', line 28

def collection?
  return @collection if defined? @collection

  @collection = element.to_s.include? "maxOccurs"
end

#collection_element_nameObject



34
35
36
37
38
39
40
41
42
# File 'lib/my_data/xsd/element.rb', line 34

def collection_element_name
  return @collection_element_name if defined? @collection_element_name

  @collection_element_name =
    if collection?
      nested_element = element.at_xpath(".//xs:element")
      nested_element ? nested_element.attributes["name"].value : nil
    end
end

#inspectObject



48
49
50
# File 'lib/my_data/xsd/element.rb', line 48

def inspect
  "Element: { name: #{name.to_json}, type: #{type.to_json}, collection: #{collection?} }"
end

#nameObject



12
13
14
# File 'lib/my_data/xsd/element.rb', line 12

def name
  @name ||= element.attributes["name"].value
end

#required?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/my_data/xsd/element.rb', line 44

def required?
  element.attributes["minOccurs"]&.value != "0"
end

#typeObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/my_data/xsd/element.rb', line 16

def type
  @type =
    begin
      extracted_type =
        element.attributes["type"] ||
        element.at_xpath(".//xs:element[@type]")&.attributes.try(:[], "type") ||
        element.at_xpath(".//xs:restriction[@base]")&.attributes.try(:[], "base")

      camelize_type(extracted_type.value)
    end
end