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



7
8
9
10
11
# File 'lib/rexport/tree_node.rb', line 7

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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_child(*names) ⇒ Object

Add one or more children to the tree



14
15
16
17
18
19
# File 'lib/rexport/tree_node.rb', line 14

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



32
33
34
# File 'lib/rexport/tree_node.rb', line 32

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



22
23
24
# File 'lib/rexport/tree_node.rb', line 22

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

#to_includeObject

Return a :include comptatible statement from the tree



27
28
29
# File 'lib/rexport/tree_node.rb', line 27

def to_include
  children.map(&:build_include)
end