Class: Rexport::TreeNode

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

Overview

A basic tree for building up ActiveRecord find :include parameters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *names) ⇒ TreeNode

Initialize a tree node setting name and adding a child if one was passed



9
10
11
12
13
# File 'lib/rexport/tree_node.rb', line 9

def initialize(name, *names)
  self.name = name
  self.children = []
  add_child(names)
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_child(*names) ⇒ Object

Add one or more children to the tree



16
17
18
19
20
21
22
# File 'lib/rexport/tree_node.rb', line 16

def add_child(*names)
  names.flatten!
  return unless (name = names.shift)

  node = children.find { |c| c.name == name }
  node ? node.add_child(names) : (children << TreeNode.new(name, names))
end

#build_includeObject

Return the include parameters for a child



35
36
37
# File 'lib/rexport/tree_node.rb', line 35

def build_include
  leaf_node? ? name.to_sym : { name.to_sym => children.map(&:build_include) }
end

#to_aObject

Return an array representation of the tree



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

def to_a
  [name, children.map(&:to_a)]
end

#to_includeObject

Return a :include comptatible statement from the tree



30
31
32
# File 'lib/rexport/tree_node.rb', line 30

def to_include
  children.map(&:build_include)
end