Module: Support::Tree
- Extended by:
- Enumerable
- Included in:
- CSL::Locale, CSL::Node, CSL::Nodes::Node
- Defined in:
- lib/support/tree.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#node_name ⇒ Object
Returns the value of attribute node_name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #add_children(*args) ⇒ Object (also: #add_child)
- #ancestors ⇒ Object
- #ancestors! ⇒ Object
- #children ⇒ Object
- #depth ⇒ Object
- #descendants ⇒ Object
- #descendants! ⇒ Object
- #each ⇒ Object
- #find_children_by_name(name) ⇒ Object
- #has_children? ⇒ Boolean
- #name ⇒ Object
- #root ⇒ Object
- #root! ⇒ Object
- #root? ⇒ Boolean
Instance Attribute Details
#node_name ⇒ Object
Returns the value of attribute node_name.
5 6 7 |
# File 'lib/support/tree.rb', line 5 def node_name @node_name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/support/tree.rb', line 5 def parent @parent end |
Class Method Details
.included(base) ⇒ Object
7 8 9 |
# File 'lib/support/tree.rb', line 7 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#add_children(*args) ⇒ Object Also known as: add_child
35 36 37 38 |
# File 'lib/support/tree.rb', line 35 def add_children(*args) args.flatten.compact.each { |node| node.parent = self; children << node } self end |
#ancestors ⇒ Object
46 |
# File 'lib/support/tree.rb', line 46 def ancestors; @ancestors || ancestors!; end |
#ancestors! ⇒ Object
42 43 44 |
# File 'lib/support/tree.rb', line 42 def ancestors! @ancestors = root? ? [] : [parent] + parent.ancestors! end |
#children ⇒ Object
15 16 17 |
# File 'lib/support/tree.rb', line 15 def children @children ||= [] end |
#depth ⇒ Object
48 49 50 |
# File 'lib/support/tree.rb', line 48 def depth ancestors.length end |
#descendants ⇒ Object
33 |
# File 'lib/support/tree.rb', line 33 def descendants; @descendants || descendants!; end |
#descendants! ⇒ Object
29 30 31 |
# File 'lib/support/tree.rb', line 29 def descendants! @descendants = children + children.map(&:children).flatten end |
#each ⇒ Object
25 26 27 |
# File 'lib/support/tree.rb', line 25 def each descendants.each { |child| yield child } end |
#find_children_by_name(name) ⇒ Object
19 20 21 |
# File 'lib/support/tree.rb', line 19 def find_children_by_name(name) children.select { |node| node.name == name.to_s } end |
#has_children? ⇒ Boolean
23 |
# File 'lib/support/tree.rb', line 23 def has_children?; !children.empty?; end |
#name ⇒ Object
11 12 13 |
# File 'lib/support/tree.rb', line 11 def name node_name || self.class.name.split(/::/).last.gsub(/([[:lower:]])([[:upper:]])/) { [$1, $2].join('-') }.downcase end |
#root ⇒ Object
56 |
# File 'lib/support/tree.rb', line 56 def root; @root || root!; end |
#root! ⇒ Object
52 53 54 |
# File 'lib/support/tree.rb', line 52 def root! @root = root? ? self : parent.root! end |
#root? ⇒ Boolean
58 |
# File 'lib/support/tree.rb', line 58 def root?; parent.nil?; end |