Class: XmlUtils::Node

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

Direct Known Subclasses

ChildNode, Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



42
43
44
# File 'lib/xmlutils/node.rb', line 42

def initialize
  @parent = nil
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



40
41
42
# File 'lib/xmlutils/node.rb', line 40

def parent
  @parent
end

Instance Method Details

#deep_cloneObject



90
91
92
# File 'lib/xmlutils/node.rb', line 90

def deep_clone
  Marshal.load(Marshal.dump(self))
end

#documentObject



66
67
68
69
# File 'lib/xmlutils/node.rb', line 66

def document
  r = root
  r.is_a?(Document) ? r : nil
end

#each(&block) ⇒ Object



46
47
48
# File 'lib/xmlutils/node.rb', line 46

def each(&block)
  return to_enum unless block_given?
end

#next_siblingObject



71
72
73
74
75
76
# File 'lib/xmlutils/node.rb', line 71

def next_sibling
  return nil unless @parent
  siblings = @parent.children
  idx = siblings.index(self)
  idx ? siblings[idx + 1] : nil
end

#node_typeObject



56
57
58
# File 'lib/xmlutils/node.rb', line 56

def node_type
  self.class.name.split('::').last.downcase.to_sym
end

#previous_siblingObject



78
79
80
81
82
83
# File 'lib/xmlutils/node.rb', line 78

def previous_sibling
  return nil unless @parent
  siblings = @parent.children
  idx = siblings.index(self)
  idx && idx > 0 ? siblings[idx - 1] : nil
end

#removeObject



85
86
87
88
# File 'lib/xmlutils/node.rb', line 85

def remove
  @parent.delete(self) if @parent
  self
end

#rootObject



60
61
62
63
64
# File 'lib/xmlutils/node.rb', line 60

def root
  node = self
  node = node.parent while node.parent
  node
end

#to_sObject



50
51
52
53
54
# File 'lib/xmlutils/node.rb', line 50

def to_s
  output = ""
  write(output)
  output
end