Module: BlueTree::Node

Defined in:
lib/blue_tree/node.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#child_nodesObject (readonly)

Returns the value of attribute child_nodes.



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

def child_nodes
  @child_nodes
end

#parent_nodeObject (readonly)

Returns the value of attribute parent_node.



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

def parent_node
  @parent_node
end

#root_nodeObject (readonly)

Returns the value of attribute root_node.



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

def root_node
  @root_node
end

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/blue_tree/node.rb', line 25

def self.included(base)
  base.extend ClassMethods
  base.use_template_path(
    File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'templates'))
  )
  base.use_template_ext '.html.erb'
end

Instance Method Details

#add_child(child) ⇒ Object



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

def add_child(child)
  child.set_root_node(@root_node || self)
  child.set_parent_node self
  @child_nodes << child
end

#initialize(template_symbol) ⇒ Object



33
34
35
36
37
38
# File 'lib/blue_tree/node.rb', line 33

def initialize(template_symbol)
  @template = template_contents(template_symbol)
  @child_nodes = []
  @parent_node = nil
  @root_node = nil
end

#renderObject



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

def render
  ERB.new(@template, nil, '-').result(binding)
end

#render_template(template_symbol) ⇒ Object



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

def render_template(template_symbol)
  ERB.new(template_contents(template_symbol), nil, '-').result(binding)
end

#set_parent_node(parent) ⇒ Object



63
64
65
# File 'lib/blue_tree/node.rb', line 63

def set_parent_node(parent)
  @parent_node = parent
end

#set_root_node(root_node) ⇒ Object



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

def set_root_node(root_node)
  @root_node = root_node
  child_nodes.each { |child| child.set_root_node(root_node) }
end

#template_contents(template_symbol) ⇒ Object



48
49
50
# File 'lib/blue_tree/node.rb', line 48

def template_contents(template_symbol)
  File.read(File.join(template_path, "#{template_symbol}#{template_ext}"))
end

#template_extObject



44
45
46
# File 'lib/blue_tree/node.rb', line 44

def template_ext
  self.class.template_ext
end

#template_pathObject



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

def template_path
  self.class.template_path
end