Class: NodesCollection

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

Overview

A collection of Node objects representing a git status tree structure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes = []) ⇒ NodesCollection

Returns a new instance of NodesCollection.



65
66
67
68
69
70
71
72
73
# File 'lib/nodes_collection.rb', line 65

def initialize(nodes = [])
  nodes = [nodes].flatten(1)

  msg = '"nodes" must only contain Nodes.'
  are_nodes = ->(node) { node.is_a?(Node) }
  raise NodesCollectionTypeError, msg unless nodes.all?(&are_nodes)

  @nodes = nodes
end

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

Class Method Details

.create_from_array(ary_nodes, status) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/nodes_collection.rb', line 18

def self.create_from_array(ary_nodes, status)
  ary_nodes = [ary_nodes].flatten(1)

  msg = '"ary_nodes" must only contain Strings.'
  are_strings = ->(node) { node.is_a?(String) }
  raise NodesCollectionTypeError, msg unless ary_nodes.all?(&are_strings)

  create_from_valid_array(ary_nodes, status)
end

.create_from_string(str_nodes, status = ' ') ⇒ Object



10
11
12
13
14
15
16
# File 'lib/nodes_collection.rb', line 10

def self.create_from_string(str_nodes, status = '   ')
  msg = '"str_nodes" must be String.'
  raise NodesCollectionTypeError, msg unless str_nodes.is_a? String

  ary_nodes = str_nodes.split(%r{/})
  create_from_valid_array(ary_nodes, status)
end

.new_from_nodes_array(all_nodes) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/nodes_collection.rb', line 28

def self.new_from_nodes_array(all_nodes)
  raise msg unless all_nodes.all?(&Node.instances?)

  grouped_nodes = all_nodes.group_by(&:name)
  plain_nodes = plain_nodes(grouped_nodes)
  merged_nodes = merged_nodes(grouped_nodes)

  new(plain_nodes + merged_nodes)
end

Instance Method Details

#+(other) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/nodes_collection.rb', line 75

def +(other)
  raise 'not a Node or NodesCollection' unless other.is_a?(Node) || other.is_a?(self.class)

  all_nodes = merge_nodes_with other

  dir_nodes = dir_nodes(all_nodes)
  file_nodes = file_nodes(all_nodes)

  self.class.new(dir_nodes + file_nodes)
end

#<=>(other) ⇒ Object



86
87
88
# File 'lib/nodes_collection.rb', line 86

def <=>(other)
  to_primitive <=> other.to_primitive
end

#add_node(other) ⇒ Object



182
# File 'lib/nodes_collection.rb', line 182

def add_node(other); end

#dir_nodes(all_nodes) ⇒ Object



158
159
160
161
162
# File 'lib/nodes_collection.rb', line 158

def dir_nodes(all_nodes)
  all_dirs = all_nodes.select(&:dir?)
  dirs_collection = self.class.new_from_nodes_array all_dirs
  dirs_collection.sort!
end

#dirsObject



121
122
123
# File 'lib/nodes_collection.rb', line 121

def dirs
  nodes.select(&:dir?)
end

#file_nodes(all_nodes) ⇒ Object



152
153
154
155
156
# File 'lib/nodes_collection.rb', line 152

def file_nodes(all_nodes)
  all_files = all_nodes.select(&:file?)
  files_collection = self.class.new all_files
  files_collection.sort!
end

#filesObject



117
118
119
# File 'lib/nodes_collection.rb', line 117

def files
  nodes.select(&:file?)
end

#merge_common_nodes_with(other) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nodes_collection.rb', line 99

def merge_common_nodes_with(other)
  self_names = nodes.map(&:name)
  other_names = other.nodes.map(&:name)

  common_names = self_names & other_names
  all_nodes = nodes + other.nodes

  common_names.map do |name|
    all_nodes.select { |node| node.name == name }.reduce(&:+).nodes[0]
  end
end

#merge_nodes_with(other) ⇒ Object



111
112
113
114
115
# File 'lib/nodes_collection.rb', line 111

def merge_nodes_with(other)
  return merge_nodes_with_node(other) if other.is_a?(Node)

  merge_nodes_with_collection(other)
end

#merge_nodes_with_collection(other) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/nodes_collection.rb', line 164

def merge_nodes_with_collection(other)
  self_dedicated_nodes = nodes_not_in other
  other_dedicated_nodes = other.nodes_not_in self
  common_nodes = merge_common_nodes_with other

  self_dedicated_nodes + common_nodes + other_dedicated_nodes
end

#merge_nodes_with_node(other) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/nodes_collection.rb', line 172

def merge_nodes_with_node(other)
  if nodes.map(&:name).include?(other.name)
    equal_names = ->(node) { node.name == other.name }
    collection_merged = nodes.select(&equal_names)[0] + other
    other = collection_merged.nodes[0]
  end

  nodes + [other]
end

#nodes_not_in(other) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/nodes_collection.rb', line 90

def nodes_not_in(other)
  self_names = nodes.map(&:name)
  other_names = other.nodes.map(&:name)

  self_only_names = self_names - (self_names & other_names)

  nodes.select { |node| self_only_names.include?(node.name) }
end

#sort!Object



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

def sort!
  nodes.sort!
end

#to_primitiveObject



129
130
131
# File 'lib/nodes_collection.rb', line 129

def to_primitive
  nodes.map(&:to_primitive)
end

#to_tree_s(depth = 0, open_parents = []) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nodes_collection.rb', line 140

def to_tree_s(depth = 0, open_parents = [])
  tree_s = ''

  if nodes.length > 1
    to_tree_s = ->(node) { node.to_tree_s(depth, open_parents, last: false) }
    tree_s += nodes[0..-2].map(&to_tree_s) * ''
  end
  tree_s += nodes.last.to_tree_s(depth, open_parents)

  tree_s
end

#valid?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
# File 'lib/nodes_collection.rb', line 133

def valid?
  nodes.is_a?(Array) &&
    nodes.all? { |node| node.is_a?(Node) } &&
    nodes&.all?(&:valid?)
  # TODO: compare uniqueness of file and dir names.
end