Class: Rexport::TreeNode
- Inherits:
-
Object
- Object
- Rexport::TreeNode
- Defined in:
- lib/rexport/tree_node.rb
Overview
A basic tree for building up ActiveRecord find :include parameters
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#add_child(*names) ⇒ Object
Add one or more children to the tree.
-
#build_include ⇒ Object
Return the include parameters for a child.
-
#initialize(name, *names) ⇒ TreeNode
constructor
Initialize a tree node setting name and adding a child if one was passed.
-
#to_a ⇒ Object
Return an array representation of the tree.
-
#to_include ⇒ Object
Return a :include comptatible statement from the tree.
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
#children ⇒ Object
Returns the value of attribute children.
4 5 6 |
# File 'lib/rexport/tree_node.rb', line 4 def children @children end |
#name ⇒ Object
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_include ⇒ Object
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_a ⇒ Object
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_include ⇒ Object
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 |