Class: REXML::Object::ReflectiveElementList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/voruby/misc/rexml_ext.rb

Direct Known Subclasses

ClassifiedElementList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, *xpath) ⇒ ReflectiveElementList

Returns a new instance of ReflectiveElementList.



11
12
13
14
# File 'lib/voruby/misc/rexml_ext.rb', line 11

def initialize(parent, *xpath)
  @parent = parent
  @xpath = xpath
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/voruby/misc/rexml_ext.rb', line 9

def parent
  @parent
end

#xpathObject (readonly)

Returns the value of attribute xpath.



9
10
11
# File 'lib/voruby/misc/rexml_ext.rb', line 9

def xpath
  @xpath
end

Instance Method Details

#<<(element) ⇒ Object



26
27
28
29
30
# File 'lib/voruby/misc/rexml_ext.rb', line 26

def <<(element)
  path = "#{self.xpath.first}[last()]"
  last = XPath.first(self.parent, path, self.xpath.last)
  last ? last.next_sibling = element : self.parent << element
end

#[](i) ⇒ Object



16
17
18
19
# File 'lib/voruby/misc/rexml_ext.rb', line 16

def [](i)
  path = "#{self.xpath.first}[#{i+1}]"
  XPath.first(self.parent, path, self.xpath.last)
end

#[]=(i, element) ⇒ Object



21
22
23
24
# File 'lib/voruby/misc/rexml_ext.rb', line 21

def []=(i, element)
  el = self[i]
  el ? el.replace_with(element) : self<<(element)
end

#clearObject



56
57
58
# File 'lib/voruby/misc/rexml_ext.rb', line 56

def clear
  self.each { |el| el.remove }
end

#delete_at(i) ⇒ Object



42
43
44
45
# File 'lib/voruby/misc/rexml_ext.rb', line 42

def delete_at(i)
  path = "#{self.xpath.first}[#{i+1}]"
  XPath.first(self.parent, path, self.xpath.last).remove
end

#eachObject



65
66
67
# File 'lib/voruby/misc/rexml_ext.rb', line 65

def each
  XPath.each(self.parent, *self.xpath) { |el| yield el }
end

#firstObject



37
38
39
40
# File 'lib/voruby/misc/rexml_ext.rb', line 37

def first
  path = "#{self.xpath.first}[1]"
  XPath.first(self.parent, path, self.xpath.last)
end

#lastObject



32
33
34
35
# File 'lib/voruby/misc/rexml_ext.rb', line 32

def last
  path = "#{self.xpath.first}[last()]"
  XPath.first(self.parent, path, self.xpath.last)
end

#replace(ary) ⇒ Object



60
61
62
63
# File 'lib/voruby/misc/rexml_ext.rb', line 60

def replace(ary)
  self.clear
  ary.each { |el| self<<(el) }
end

#sizeObject



47
48
49
50
# File 'lib/voruby/misc/rexml_ext.rb', line 47

def size
  path = "count(#{self.xpath.first})"
  XPath.first(self.parent, path, self.xpath.last)
end

#to_aObject



52
53
54
# File 'lib/voruby/misc/rexml_ext.rb', line 52

def to_a
  XPath.match(self.parent, *self.xpath)
end