Class: Node

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

Overview

A Node represents a file or directory in the git-status-tree

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, children = nil, status = nil) ⇒ Node

Returns a new instance of Node.

Raises:



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

.indentObject

Returns the value of attribute indent.



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

def indent
  @indent
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



13
14
15
# File 'lib/node.rb', line 13

def children
  @children
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/node.rb', line 13

def name
  @name
end

#statusObject

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

Raises:



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

Returns:

  • (Boolean)


36
37
38
# File 'lib/node.rb', line 36

def self.instances?
  ->(node) { node.is_a?(Node) }
end

Instance Method Details

#+(other) ⇒ NodesCollection

Returns:



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

Returns:

  • (Boolean)


120
121
122
# File 'lib/node.rb', line 120

def added?
  status.include?('A')
end

#copied?Boolean

‘C’ copied

Returns:

  • (Boolean)


135
136
137
# File 'lib/node.rb', line 135

def copied?
  status.include?('C')
end

#deleted?Boolean

‘D’ deleted

Returns:

  • (Boolean)


125
126
127
# File 'lib/node.rb', line 125

def deleted?
  status.include?('D')
end

#dir?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/node.rb', line 74

def dir?
  !file?
end

#file?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/node.rb', line 70

def file?
  children.nil?
end

#modified?Boolean

‘M’ modified

Returns:

  • (Boolean)


115
116
117
# File 'lib/node.rb', line 115

def modified?
  status.include?('M')
end

#new?Boolean

‘?’ new

Returns:

  • (Boolean)


145
146
147
# File 'lib/node.rb', line 145

def new?
  status.include?('?')
end

#renamed?Boolean

‘R’ renamed

Returns:

  • (Boolean)


130
131
132
# File 'lib/node.rb', line 130

def renamed?
  status.include?('R')
end

#staged?Boolean

‘+’ staged

Returns:

  • (Boolean)


150
151
152
# File 'lib/node.rb', line 150

def staged?
  status.include?('+')
end

#to_primitiveObject



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

Returns:

  • (Boolean)


140
141
142
# File 'lib/node.rb', line 140

def unmerged?
  status.include?('U')
end

#valid?Boolean

Returns:

  • (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