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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#node_nameObject

Returns the value of attribute node_name.



5
6
7
# File 'lib/support/tree.rb', line 5

def node_name
  @node_name
end

#parentObject

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

#ancestorsObject



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

#childrenObject



15
16
17
# File 'lib/support/tree.rb', line 15

def children
  @children ||= []
end

#depthObject



48
49
50
# File 'lib/support/tree.rb', line 48

def depth
  ancestors.length
end

#descendantsObject



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

#eachObject



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

Returns:

  • (Boolean)


23
# File 'lib/support/tree.rb', line 23

def has_children?; !children.empty?; end

#nameObject



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

#rootObject



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

Returns:

  • (Boolean)


58
# File 'lib/support/tree.rb', line 58

def root?; parent.nil?; end