class Noodle::Node
Attributes
childs[R]
content[RW]
name[RW]
parent[R]
Public Class Methods
new(name_node, hash_node = nil)
click to toggle source
you need to add name optionnaly you can set value for attributes node
# File lib/node.rb, line 8 def initialize(name_node, hash_node = nil) @name = name_node @values = hash_node || {} @next = nil @previous = nil @parent = nil @content = nil @childs = [] end
Public Instance Methods
addChild(child)
click to toggle source
Add Noodle::Node
to childs’ node
# File lib/node.rb, line 65 def addChild(child) child.setParent(self) @childs.push child end
attr(key, value = nil)
click to toggle source
attr(key) where key is a string : getter method attr(key, value) key=string, value=string : setter method attr(key) key=Hash : setter method for all pair key/value in params value
# File lib/node.rb, line 24 def attr(key, value = nil) if key.class == "Hash" key.each {|keyt, val| @values[keyt] = val} elsif value.nil? return @values[key] if @values.has_key? key else @values[key] = value end puts "#{@values.inspect}" return nil end
deleteAttr(key)
click to toggle source
Delete all attr values
# File lib/node.rb, line 44 def deleteAttr(key) if @values.has_key? key @values.delete(key) return true end return false end
listAttr()
click to toggle source
Return all values containt by node
# File lib/node.rb, line 37 def listAttr data = [] @values.each {|key, val| data.push key} return data end
newChild(name, hash_tab = nil)
click to toggle source
Create child same parametre that Noodle::Node
and this node is adding to child
# File lib/node.rb, line 58 def newChild(name, hash_tab = nil) nchild = Noodle::Node.new(name, hash_tab) addChild(nchild) return nchild end
next()
click to toggle source
jump to the next node if exist return nil if he doesn’t exist
# File lib/node.rb, line 71 def next return (@parent ? @parent.childNext(self) : nil) end
previous()
click to toggle source
jump to the previous node if exist return nil if he doesn’t exist
# File lib/node.rb, line 76 def previous return (@parent ? @parent.childPrevious(self) : nil) end
setParent(parent)
click to toggle source
Set value parent, you need to pass a Noodle::Node
class
# File lib/node.rb, line 53 def setParent(parent) @parent = parent end
to_xml()
click to toggle source
Convert Object and all childs NOT parent in xml Set content, becarefull we can add childs && content set ! Content are printing in to_xml
if are not null
# File lib/node.rb, line 83 def to_xml return "<#{@name}#{values_str} />" if content.nil? && @childs.empty? data = "<#{@name}#{values_str}>" if @content.nil? @childs.each do |child| data += child.to_xml end else data += @content end data += "</#{@name}>" end
Private Instance Methods
childNext(node_child)
click to toggle source
# File lib/node.rb, line 98 def childNext(node_child) return_child(iter_child(node_child) + 1) end
childPrevious(node_child)
click to toggle source
# File lib/node.rb, line 102 def childPrevious(node_child) return_child(iter_child(node_child) - 1) end
iter_child(obj_node)
click to toggle source
# File lib/node.rb, line 114 def iter_child(obj_node) i = 0 while @childs.length > i return i if @childs[i] = obj_node i += 1 end return i end
return_child(i)
click to toggle source
# File lib/node.rb, line 123 def return_child(i) return (i < @childs.length && i >= 0 ? @childs[i] : nil) end
values_str()
click to toggle source
# File lib/node.rb, line 106 def values_str data = "" @values.each do |key, val| data += " #{key}=\"#{val}\"" end return data end