Class: Sekken::XML::Element
- Inherits:
-
Object
- Object
- Sekken::XML::Element
- Defined in:
- lib/sekken/xml/element.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Public: The attributes.
-
#base_type ⇒ Object
Public: The base name for a simple type element.
-
#children ⇒ Object
Public: The child elements.
-
#complex_type_id ⇒ Object
Private: The complex type id for this element to track recursive type definitions.
-
#form ⇒ Object
Returns the value of attribute form.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#recursive_type ⇒ Object
Public: The name of the recursive type definition if any.
-
#singular ⇒ Object
(also: #singular?)
Public: Accessor for whether this is a single element.
Instance Method Summary collapse
-
#complex_type? ⇒ Boolean
Public: Whether this element is a complex type.
-
#initialize ⇒ Element
constructor
A new instance of Element.
-
#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.
-
#simple_type? ⇒ Boolean
Public: Whether this element is a simple type.
-
#to_a(memo = [], stack = []) ⇒ Object
Public: Returns this element and its children as an Array of Hashes for inspection.
Constructor Details
#initialize ⇒ Element
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
#attributes ⇒ Object
Public: The attributes.
47 48 49 |
# File 'lib/sekken/xml/element.rb', line 47 def attributes @attributes end |
#base_type ⇒ Object
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 |
#children ⇒ Object
Public: The child elements.
44 45 46 |
# File 'lib/sekken/xml/element.rb', line 44 def children @children end |
#complex_type_id ⇒ Object
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 |
#form ⇒ Object
Returns the value of attribute form.
12 13 14 |
# File 'lib/sekken/xml/element.rb', line 12 def form @form end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/sekken/xml/element.rb', line 12 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
12 13 14 |
# File 'lib/sekken/xml/element.rb', line 12 def namespace @namespace end |
#parent ⇒ Object
Returns the value of attribute parent.
12 13 14 |
# File 'lib/sekken/xml/element.rb', line 12 def parent @parent end |
#recursive_type ⇒ Object
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 |
#singular ⇒ Object 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.
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.
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.
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 |