Class: Kayessess::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/kayessess/node.rb

Overview

Node represents an item in a Tree. It provides some helpers for navigating the tree that is constructed from styleguide reference paths.

Direct Known Subclasses

Section

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, parent = nil, children_hash = {}, sections_hash = {}) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
# File 'lib/kayessess/node.rb', line 11

def initialize(id, name, parent = nil, children_hash = {}, sections_hash = {})
  @id            = id
  @name          = name
  @parent        = parent
  @children_hash = children_hash
  @sections_hash = sections_hash
end

Instance Attribute Details

#children_hashObject

Returns the value of attribute children_hash.



9
10
11
# File 'lib/kayessess/node.rb', line 9

def children_hash
  @children_hash
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/kayessess/node.rb', line 8

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/kayessess/node.rb', line 8

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/kayessess/node.rb', line 8

def parent
  @parent
end

#sections_hashObject

Returns the value of attribute sections_hash.



9
10
11
# File 'lib/kayessess/node.rb', line 9

def sections_hash
  @sections_hash
end

Instance Method Details

#childrenObject



39
40
41
# File 'lib/kayessess/node.rb', line 39

def children
  @children_hash.values
end

#is_section?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/kayessess/node.rb', line 47

def is_section?
  false
end

#parents(&block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/kayessess/node.rb', line 30

def parents(&block)
  return to_enum(:parents) unless block_given?
  node = self
  while node = node.parent do
    break if node.nil?
    yield node
  end
end

#sectionsObject



43
44
45
# File 'lib/kayessess/node.rb', line 43

def sections
  @sections_hash.values
end

#to_paramObject



19
20
21
# File 'lib/kayessess/node.rb', line 19

def to_param
  name.to_slug
end

#to_pathObject



23
24
25
26
27
28
# File 'lib/kayessess/node.rb', line 23

def to_path
  File.join(parents.reduce([self.to_param]) {|paths, node|
    paths << node.to_param unless node.parent.nil?
    paths
  }.reverse)
end