Class: Lookbook::TreeNode

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/lookbook/support/tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, content = nil, priority: 10000) ⇒ TreeNode

Returns a new instance of TreeNode.



11
12
13
14
15
16
# File 'lib/lookbook/support/tree_node.rb', line 11

def initialize(path = nil, content = nil, priority: 10000)
  @path = path.to_s
  @content = content
  @priority = priority
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)



9
10
11
# File 'lib/lookbook/support/tree_node.rb', line 9

def children
  @children
end

#contentObject



8
9
10
# File 'lib/lookbook/support/tree_node.rb', line 8

def content
  @content
end

#pathObject



8
9
10
# File 'lib/lookbook/support/tree_node.rb', line 8

def path
  @path
end

Instance Method Details

#<=>(other) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/lookbook/support/tree_node.rb', line 68

def <=>(other)
  if content?
    content <=> (other.content? ? other.content : other)
  else
    [priority, label] <=> [other.priority, other.label]
  end
end

#add_child(name, content = nil, priority: 10000) ⇒ Object



42
43
44
# File 'lib/lookbook/support/tree_node.rb', line 42

def add_child(name, content = nil, priority: 10000)
  children << TreeNode.new("#{path}/#{name}", content, priority: priority)
end

#content?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/lookbook/support/tree_node.rb', line 54

def content?
  content.present?
end

#depthObject



38
39
40
# File 'lib/lookbook/support/tree_node.rb', line 38

def depth
  path.split("/").size
end

#each(&block) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/lookbook/support/tree_node.rb', line 58

def each(&block)
  if block
    children.sort.each do |child|
      yield child
    end
  else
    to_enum(:each)
  end
end

#get_child(name) ⇒ Object



50
51
52
# File 'lib/lookbook/support/tree_node.rb', line 50

def get_child(name)
  children.find { |child| child.name == name }
end

#has_child?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/lookbook/support/tree_node.rb', line 46

def has_child?(name)
  !!get_child(name)
end

#idObject



18
19
20
# File 'lib/lookbook/support/tree_node.rb', line 18

def id
  Utils.id(content_value(:id, path))
end

#labelObject



26
27
28
# File 'lib/lookbook/support/tree_node.rb', line 26

def label
  content_value(:label, name.titleize)
end

#nameObject



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

def name
  segments.last
end

#priorityObject



30
31
32
# File 'lib/lookbook/support/tree_node.rb', line 30

def priority
  content_value(:priority, @priority)
end

#typeObject



34
35
36
# File 'lib/lookbook/support/tree_node.rb', line 34

def type
  content_value(:type, :directory)
end