Class: Lookbook::TreeNode
- Inherits:
-
Object
- Object
- Lookbook::TreeNode
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/lookbook/support/tree_node.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add_child(name, content = nil, priority: 10000) ⇒ Object
- #content? ⇒ Boolean
- #depth ⇒ Object
- #each(&block) ⇒ Object
- #get_child(name) ⇒ Object
- #has_child?(name) ⇒ Boolean
- #id ⇒ Object
-
#initialize(path = nil, content = nil, priority: 10000) ⇒ TreeNode
constructor
A new instance of TreeNode.
- #label ⇒ Object
- #name ⇒ Object
- #priority ⇒ Object
- #type ⇒ Object
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
#children ⇒ Object (readonly)
9 10 11 |
# File 'lib/lookbook/support/tree_node.rb', line 9 def children @children end |
#content ⇒ Object
8 9 10 |
# File 'lib/lookbook/support/tree_node.rb', line 8 def content @content end |
#path ⇒ Object
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
54 55 56 |
# File 'lib/lookbook/support/tree_node.rb', line 54 def content? content.present? end |
#depth ⇒ Object
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
46 47 48 |
# File 'lib/lookbook/support/tree_node.rb', line 46 def has_child?(name) !!get_child(name) end |
#id ⇒ Object
18 19 20 |
# File 'lib/lookbook/support/tree_node.rb', line 18 def id Utils.id(content_value(:id, path)) end |
#label ⇒ Object
26 27 28 |
# File 'lib/lookbook/support/tree_node.rb', line 26 def label content_value(:label, name.titleize) end |
#name ⇒ Object
22 23 24 |
# File 'lib/lookbook/support/tree_node.rb', line 22 def name segments.last end |
#priority ⇒ Object
30 31 32 |
# File 'lib/lookbook/support/tree_node.rb', line 30 def priority content_value(:priority, @priority) end |
#type ⇒ Object
34 35 36 |
# File 'lib/lookbook/support/tree_node.rb', line 34 def type content_value(:type, :directory) end |