Class: XMLCodec::XMLSubElements

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/subelements.rb

Overview

This is the container class used to hold the elements for the xmlsubelements and xmlsubel_mult in a XMLElement.

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ XMLSubElements

Create a new instance of the container



51
52
53
54
# File 'lib/subelements.rb', line 51

def initialize(parent)
  @elements = []
  @parent = parent
end

Instance Method Details

#<<(value) ⇒ Object

Adds a value to the collection. The value may be either a String or a descendant of XMLElement. If it’s a string it’s converted into a XMLTextElement



70
71
72
73
74
75
76
# File 'lib/subelements.rb', line 70

def <<(value)
  if value.instance_of? String
    value = XMLTextElement.new(value)
  end
  value.__parent = @parent
  @elements << value
end

#[](num) ⇒ Object

Get the element with the given number



79
80
81
# File 'lib/subelements.rb', line 79

def [](num)
  @elements[num]
end

#create_xml(parent) ⇒ Object

Create the XML of all the elements by creating the XML for each of them.



89
90
91
# File 'lib/subelements.rb', line 89

def create_xml(parent)
  @elements.each{|e| e.create_xml(parent)}
end

#delete_element(element) ⇒ Object

Delete a certain element from the collection



99
100
101
# File 'lib/subelements.rb', line 99

def delete_element(element)
  @elements.delete(element)
end

#eachObject

Iterate all the values in the collection



94
95
96
# File 'lib/subelements.rb', line 94

def each
  @elements.dup.each {|e| yield e}
end

#find_all_named(name) ⇒ Object

Returns the elements in the collection with the given element name



63
64
65
# File 'lib/subelements.rb', line 63

def find_all_named(name)
  self.find_all {|el| el.elname.to_s == name.to_s}
end

#find_first_named(name) ⇒ Object

Returns the first element in the collection with the given element name



58
59
60
# File 'lib/subelements.rb', line 58

def find_first_named(name)
  self.find {|el| el.elname.to_s == name.to_s}
end

#sizeObject

Get the number of elements in the container



84
85
86
# File 'lib/subelements.rb', line 84

def size
  @elements.size
end