Class: Rogdl::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/node.rb,
lib/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aName) ⇒ Node

Returns a new instance of Node.



8
9
10
# File 'lib/node.rb', line 8

def initialize(aName)
  self.gname = aName
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(keyword, *args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/node.rb', line 81

def method_missing(keyword, *args)
  word = keyword.to_s
  if word.reverse[0,2] == 'zz'
    return self["#{keyword}"] unless self["#{keyword}"].nil?
    return find_all_named(word[0, word.length-2])
  else
    return self["#{keyword}"]
  end
  
end

Instance Attribute Details

#gnameObject

Returns the value of attribute gname.



6
7
8
# File 'lib/node.rb', line 6

def gname
  @gname
end

Instance Method Details

#<=>(anOther) ⇒ Object



104
105
106
# File 'lib/node.rb', line 104

def <=>(anOther)
  self.gname <=> anOther.gname
end

#==(bNode) ⇒ Object



137
138
139
140
# File 'lib/node.rb', line 137

def ==(bNode)
  return false if bNode.nil?
  return self.eql?(bNode)
end

#[](aName) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/node.rb', line 62

def [](aName)
  if aName.is_a?(String)
    children.each do |node|
      return node if node.gname == aName
    end
    return nil
  elsif aName.is_a?(Fixnum)
    return children[aName]
  else
    throw "aName must be a String or Fixnum"
  end
end

#[]=(arg1, arg2) ⇒ Object



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

def []=(arg1, arg2)
  a1 = arg1.to_n
  a1.add arg2.to_n
  add(a1)
end

#add(aNode) ⇒ Object



108
109
110
111
112
113
# File 'lib/node.rb', line 108

def add(aNode)
  node = aNode.to_n
  children << node
  node.gparent = self
  return self
end

#assign(pwriter) ⇒ Object



75
76
77
# File 'lib/writer.rb', line 75

def assign(pwriter)
  @@writer = pwriter
end

#contextObject



109
110
111
# File 'lib/writer.rb', line 109

def context
  binding
end

#eachObject



34
35
36
# File 'lib/node.rb', line 34

def each
  children.each {|element| yield(element)}
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  children.empty?
end

#eql?(bNode) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/node.rb', line 133

def eql?(bNode)
  return self.flatten.collect {|n| n.gname} == bNode.flatten.collect {|n| n.gname}
end

#find_all_named(aName) ⇒ Object Also known as: gkids



92
93
94
95
96
97
98
99
100
101
# File 'lib/node.rb', line 92

def find_all_named(aName)
  if aName.is_a?(String)
    find_all {|node| node.gname == aName}
  elsif aName.is_a?(Array)
    find_all {|node| aName.include?(node.gname)}
  else
    throw 'aName must be a String or Array'
  end
  
end

#flattenObject



142
143
144
145
146
147
# File 'lib/node.rb', line 142

def flatten
  me = []
  me << self
  self.each {|n| me << n.flatten}
  return me.flatten
end

#gfirstObject



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

def gfirst
  return children.first      
end

#glengthObject



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

def glength
  children.length
end

#gparent(arg = 1) ⇒ Object Also known as: gpar



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/node.rb', line 12

def gparent(arg=1)
  return @gparent if arg == 1
  if arg.is_a?(Fixnum)
    return self if arg == 0
    return @gparent.gparent(arg-1)
  end
  if arg.is_a?(String)
    return @gparent if @gparent.nil? || @gparent.gname == arg
    return @gparent.gparent(arg)
  end
end

#gparent=(aNode) ⇒ Object



25
26
27
# File 'lib/node.rb', line 25

def gparent=(aNode)
  @gparent = aNode
end

#gvalueObject Also known as: gval



46
47
48
49
50
51
52
53
54
# File 'lib/node.rb', line 46

def gvalue
  if empty?
    return nil
  else
    val = gfirst.gname
    return val unless val[0,1] == ':'
    return @@subs[val[1, val.length].to_sym]
  end
end

#render(template_name = gname) ⇒ Object



79
80
81
82
83
# File 'lib/writer.rb', line 79

def render(template_name=gname)
  template = @@writer.templates[template_name]
  throw "no template defined for #{template_name}" if template.nil?
  return ERB.new(template).result(self.context)
end

#render_child_named(aName) ⇒ Object



85
86
87
# File 'lib/writer.rb', line 85

def render_child_named(aName)
  return self[aName].render
end

#render_childrenObject



89
90
91
92
93
# File 'lib/writer.rb', line 89

def render_children
  s = ""
  self.each {|child| s << child.render}
  return s
end

#render_children_named(aName) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/writer.rb', line 95

def render_children_named(aName)
  if aName.is_a?(String)
    s = ""
    self.find_all_named(aName).each {|child| s << child.render}
    return s
  elsif aName.is_a?(Array)
    s = ""
    self.each {|child| s << child.render if aName.include?(child.gname)}
    return s
  else
    throw 'aName must be a String or Array'
  end
end

#subs(key, value) ⇒ Object



29
30
31
32
# File 'lib/node.rb', line 29

def subs(key,value)
  @@subs ||= {}
  @@subs[key] = value
end

#to_nObject



115
116
117
# File 'lib/node.rb', line 115

def to_n
  self
end

#to_xmlObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/node.rb', line 119

def to_xml
  return "<#{@gname}/>" if empty?
  s = "<#{@gname}>"
  if glength == 1 && gfirst.glength == 0
    s += gvalue
  else
    children.each {|node| s += node.to_xml}
  end
  s += "</#{@gname}>"
  return s
end