Class: Noodle::Node

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

Direct Known Subclasses

Noodlecr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_node, hash_node = nil) ⇒ Node

you need to add name optionnaly you can set value for attributes node



8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#childsObject (readonly)

Returns the value of attribute childs.



4
5
6
# File 'lib/node.rb', line 4

def childs
  @childs
end

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/node.rb', line 3

def content
  @content
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/node.rb', line 3

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/node.rb', line 4

def parent
  @parent
end

Instance Method Details

#addChild(child) ⇒ Object

Add Noodle::Node to childs’ node



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

def addChild(child)
  child.setParent(self)
  @childs.push child
end

#attr(key, value = nil) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object

Delete all attr values



44
45
46
47
48
49
50
# File 'lib/node.rb', line 44

def deleteAttr(key)
  if @values.has_key? key
    @values.delete(key)
    return true
  end
  return false
end

#listAttrObject

Return all values containt by node



37
38
39
40
41
# File 'lib/node.rb', line 37

def listAttr
  data = []
  @values.each {|key, val| data.push key}
  return data
end

#newChild(name, hash_tab = nil) ⇒ Object

Create child same parametre that Noodle::Node and this node is adding to child



58
59
60
61
62
# File 'lib/node.rb', line 58

def newChild(name, hash_tab = nil)
  nchild = Noodle::Node.new(name, hash_tab)
  addChild(nchild)
  return nchild
end

#nextObject

jump to the next node if exist return nil if he doesn’t exist



71
72
73
# File 'lib/node.rb', line 71

def next
  return (@parent ? @parent.childNext(self) : nil)
end

#previousObject

jump to the previous node if exist return nil if he doesn’t exist



76
77
78
# File 'lib/node.rb', line 76

def previous
  return (@parent ? @parent.childPrevious(self) : nil)
end

#setParent(parent) ⇒ Object

Set value parent, you need to pass a Noodle::Node class



53
54
55
# File 'lib/node.rb', line 53

def setParent(parent)
  @parent = parent
end

#to_xmlObject

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



83
84
85
86
87
88
89
90
91
92
93
94
# 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