Class: Sekken::XML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/sekken/xml/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElement

Returns a new instance of Element.



5
6
7
8
9
10
# File 'lib/sekken/xml/element.rb', line 5

def initialize
  @children   = []
  @attributes = {}
  @recursive  = false
  @singular   = true
end

Instance Attribute Details

#attributesObject

Public: The attributes.



47
48
49
# File 'lib/sekken/xml/element.rb', line 47

def attributes
  @attributes
end

#base_typeObject

Public: The base name for a simple type element.



25
26
27
# File 'lib/sekken/xml/element.rb', line 25

def base_type
  @base_type
end

#childrenObject

Public: The child elements.



44
45
46
# File 'lib/sekken/xml/element.rb', line 44

def children
  @children
end

#complex_type_idObject

Private: The complex type id for this element to track recursive type definitions.



41
42
43
# File 'lib/sekken/xml/element.rb', line 41

def complex_type_id
  @complex_type_id
end

#formObject

Returns the value of attribute form.



12
13
14
# File 'lib/sekken/xml/element.rb', line 12

def form
  @form
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/sekken/xml/element.rb', line 12

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



12
13
14
# File 'lib/sekken/xml/element.rb', line 12

def namespace
  @namespace
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/sekken/xml/element.rb', line 12

def parent
  @parent
end

#recursive_typeObject

Public: The name of the recursive type definition if any.



38
39
40
# File 'lib/sekken/xml/element.rb', line 38

def recursive_type
  @recursive_type
end

#singularObject Also known as: singular?

Public: Accessor for whether this is a single element.



28
29
30
# File 'lib/sekken/xml/element.rb', line 28

def singular
  @singular
end

Instance Method Details

#complex_type?Boolean

Public: Whether this element is a complex type.

Returns:

  • (Boolean)


20
21
22
# File 'lib/sekken/xml/element.rb', line 20

def complex_type?
  !simple_type?
end

#recursive?Boolean

Public: Whether or not this element's type is defined recursively, meaning one of this element's parents is of the same type as this element.

Returns:

  • (Boolean)


33
34
35
# File 'lib/sekken/xml/element.rb', line 33

def recursive?
  !!recursive_type
end

#simple_type?Boolean

Public: Whether this element is a simple type.

Returns:

  • (Boolean)


15
16
17
# File 'lib/sekken/xml/element.rb', line 15

def simple_type?
  !!base_type
end

#to_a(memo = [], stack = []) ⇒ Object

Public: Returns this element and its children as an Array of Hashes for inspection.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sekken/xml/element.rb', line 50

def to_a(memo = [], stack = [])
  new_stack = stack + [name]
  data = { namespace: namespace, form: form, singular: singular? }

  unless attributes.empty?
    data[:attributes] = attributes.each_with_object({}) do |attribute, memo|
      memo[attribute.name] = { optional: attribute.optional? }
    end
  end

  if recursive?
    data[:recursive_type] = recursive_type
    memo << [new_stack, data]

  elsif simple_type?
    data[:type] = base_type
    memo << [new_stack, data]

  elsif complex_type?
    memo << [new_stack, data]

    children.each do |child|
      child.to_a(memo, new_stack)
    end

  end

  memo
end