Class: Node
- Inherits:
-
Object
- Object
- Node
- Defined in:
- lib/node.rb
Overview
A Node represents a file or directory in the git-status-tree
Class Attribute Summary collapse
-
.indent ⇒ Object
Returns the value of attribute indent.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ NodesCollection
- #<=>(other) ⇒ Object
-
#added? ⇒ Boolean
‘A’ added.
-
#copied? ⇒ Boolean
‘C’ copied.
-
#deleted? ⇒ Boolean
‘D’ deleted.
- #dir? ⇒ Boolean
- #file? ⇒ Boolean
-
#initialize(name, children = nil, status = nil) ⇒ Node
constructor
A new instance of Node.
-
#modified? ⇒ Boolean
‘M’ modified.
-
#new? ⇒ Boolean
‘?’ new.
-
#renamed? ⇒ Boolean
‘R’ renamed.
-
#staged? ⇒ Boolean
‘+’ staged.
- #to_primitive ⇒ Object
- #to_tree_s(depth = 0, open_parents = [0], last: true) ⇒ Object
-
#unmerged? ⇒ Boolean
‘U’ updated but unmerged.
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, children = nil, status = nil) ⇒ Node
Returns a new instance of Node.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/node.rb', line 15 def initialize(name, children = nil, status = nil) self.class.indent ||= 4 validate_name!(name) msg = '"children" must be a NodesCollection or nil.' valid_nodes_collection = children.nil? || children.is_a?(NodesCollection) raise NodeChildrenError, msg unless valid_nodes_collection @name = name @children = children @status = status || '??' end |
Class Attribute Details
.indent ⇒ Object
Returns the value of attribute indent.
10 11 12 |
# File 'lib/node.rb', line 10 def indent @indent end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
13 14 15 |
# File 'lib/node.rb', line 13 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/node.rb', line 13 def name @name end |
#status ⇒ Object
Returns the value of attribute status.
13 14 15 |
# File 'lib/node.rb', line 13 def status @status end |
Class Method Details
.create_from_string(gs_porcelain) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/node.rb', line 28 def self.create_from_string(gs_porcelain) msg = '"str_node" must be String.' raise NodeTypeError, msg unless gs_porcelain.is_a? String raise NodeNameError, '"str_node" too short.' if gs_porcelain.length < 4 node_from_gs(gs_porcelain) end |
.instances? ⇒ Boolean
36 37 38 |
# File 'lib/node.rb', line 36 def self.instances? ->(node) { node.is_a?(Node) } end |
Instance Method Details
#+(other) ⇒ NodesCollection
86 87 88 89 90 91 92 93 |
# File 'lib/node.rb', line 86 def +(other) raise 'not valid' unless valid? && other.valid? raise "not a #{self.class}" unless other.is_a?(self.class) tmp_children = [children, other.children].compact.inject(&:+) NodesCollection.new([self.class.new(name, tmp_children)]) end |
#<=>(other) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/node.rb', line 95 def <=>(other) return (name <=> other.name) if file? == other.file? return -1 if dir? && other.file? return 1 if file? && other.dir? 0 end |
#added? ⇒ Boolean
‘A’ added
120 121 122 |
# File 'lib/node.rb', line 120 def added? status.include?('A') end |
#copied? ⇒ Boolean
‘C’ copied
135 136 137 |
# File 'lib/node.rb', line 135 def copied? status.include?('C') end |
#deleted? ⇒ Boolean
‘D’ deleted
125 126 127 |
# File 'lib/node.rb', line 125 def deleted? status.include?('D') end |
#dir? ⇒ Boolean
74 75 76 |
# File 'lib/node.rb', line 74 def dir? !file? end |
#file? ⇒ Boolean
70 71 72 |
# File 'lib/node.rb', line 70 def file? children.nil? end |
#modified? ⇒ Boolean
‘M’ modified
115 116 117 |
# File 'lib/node.rb', line 115 def modified? status.include?('M') end |
#new? ⇒ Boolean
‘?’ new
145 146 147 |
# File 'lib/node.rb', line 145 def new? status.include?('?') end |
#renamed? ⇒ Boolean
‘R’ renamed
130 131 132 |
# File 'lib/node.rb', line 130 def renamed? status.include?('R') end |
#staged? ⇒ Boolean
‘+’ staged
150 151 152 |
# File 'lib/node.rb', line 150 def staged? status.include?('+') end |
#to_primitive ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/node.rb', line 62 def to_primitive if dir? { name => children.to_primitive } else name end end |
#to_tree_s(depth = 0, open_parents = [0], last: true) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/node.rb', line 103 def to_tree_s(depth = 0, open_parents = [0], last: true) open_parents << depth pre = pre_tree(depth, open_parents, last) str_tree = "#{pre}#{color_name}\n" str_tree += children.to_tree_s(depth + 1, open_parents) if children str_tree end |
#unmerged? ⇒ Boolean
‘U’ updated but unmerged
140 141 142 |
# File 'lib/node.rb', line 140 def unmerged? status.include?('U') end |
#valid? ⇒ Boolean
78 79 80 81 82 83 |
# File 'lib/node.rb', line 78 def valid? return valid_dir? if dir? return valid_file? if file? false end |