Class: VirtualTree

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff_tree, commit) ⇒ VirtualTree

Returns a new instance of VirtualTree.



14
15
16
17
18
19
20
21
22
# File 'lib/virtual_tree.rb', line 14

def initialize(diff_tree, commit)
  @path = diff_tree[:path]
  @tree_id = nil
  @previous_tree_id = nil
  @commits = []
  @issues = []
  @files = {}
  @trees = {}
end

Instance Attribute Details

#commitsObject (readonly)

Returns the value of attribute commits.



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

def commits
  @commits
end

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/virtual_tree.rb', line 11

def files
  @files
end

#issuesObject (readonly)

Returns the value of attribute issues.



9
10
11
# File 'lib/virtual_tree.rb', line 9

def issues
  @issues
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/virtual_tree.rb', line 6

def path
  @path
end

#previous_tree_idObject (readonly)

Returns the value of attribute previous_tree_id.



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

def previous_tree_id
  @previous_tree_id
end

#tree_idObject (readonly)

Returns the value of attribute tree_id.



7
8
9
# File 'lib/virtual_tree.rb', line 7

def tree_id
  @tree_id
end

#treesObject (readonly)

Returns the value of attribute trees.



12
13
14
# File 'lib/virtual_tree.rb', line 12

def trees
  @trees
end

Class Method Details

.from_json(tree_json) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/virtual_tree.rb', line 24

def self.from_json(tree_json)
  new_tree = VirtualTree.new({path: tree_json["path"]}, {})
  new_tree.instance_variable_set(:@commits, tree_json["commits"])
  new_tree.instance_variable_set(:@issues, tree_json["issues"])
  new_tree.instance_variable_set(:@tree_id, tree_json["tree_id"])
  new_tree.instance_variable_set(:@previous_tree_id, tree_json["previous_tree_id"])
  new_tree
end

Instance Method Details

#add_tree(tree) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/virtual_tree.rb', line 117

def add_tree(tree)
  tree_name = tree_name(tree.path)
  subtree = subtree(tree.path)
  if subtree.path == tree.path
    Loggr.instance.warn("Cannot add pre-existing tree: #{tree.path}")
  else
    subtree.trees[tree_name] = tree
  end
end

#all_filesObject



74
75
76
77
78
79
# File 'lib/virtual_tree.rb', line 74

def all_files()
  @trees
    .values
    .flat_map { |tree| tree.all_files() }
    .concat(@files.values)
end

#as_jsonObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/virtual_tree.rb', line 127

def as_json()
  stats = @files
    .values
    .reduce(new_context()) do |acc, file|
      acc[:business_value] += file.business_value
      acc[:lines] += file.lines.count
      acc[:covered_lines] += file.covered_lines
      acc[:uncovered_lines] += file.uncovered_lines
      acc[:buggy_lines] += file.buggy_lines_total
      acc[:changes] += file.revisions_total
      acc[:errors] += file.errors_total
      acc
    end

  {
    path: @path,
    tree_id: @tree_id,
    previous_tree_id: @previous_tree_id,
    issues: @issues,
    commits: @commits,
    files: @files.values.map(&method(:file_json))
  }.merge(stats)
end

#delete_file(file_path) ⇒ Object



41
42
43
44
45
# File 'lib/virtual_tree.rb', line 41

def delete_file(file_path)
  file_subtree(file_path)
    .files
    .delete(file_path)
end

#get_file(file_path) ⇒ Object



33
34
35
# File 'lib/virtual_tree.rb', line 33

def get_file(file_path)
  file_subtree(file_path).files[file_path]
end

#move_file(a_file_name, b_file_name) ⇒ Object



47
48
49
50
51
# File 'lib/virtual_tree.rb', line 47

def move_file(a_file_name, b_file_name)
  file = get_file(a_file_name)
  delete_file(a_file_name)
  set_file(b_file_name, file)
end

#prune(diff_tree, commit) ⇒ Object



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

def prune(diff_tree, commit)
  return if diff_tree[:operation] != :delete

  tree_name = tree_name(diff_tree[:path])
  subtree(file_tree_path(diff_tree[:path]))
    .trees
    .delete(tree_name)
end

#save(commit) ⇒ Object

TODO: Figure out how to save only when something changes.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/virtual_tree.rb', line 82

def save(commit)
  trees_json = @trees
    .values
    .map { |tree| tree.save(commit) }

  json = trees_json
    .reduce(as_json()) do |acc, tree_json|
      [
          :lines,
          :covered_lines,
          :uncovered_lines,
          :changes,
          :errors,
          :buggy_lines,
          :business_value
        ].each do |key|
          acc[key] += tree_json[key]
        end

      acc
    end

  relevant_lines = json[:covered_lines] + json[:uncovered_lines]
  json[:coverage_percent] = relevant_lines > 0 ?
    (json[:covered_lines] / relevant_lines.to_f * 100).round :
    0

  json[:trees] = trees_json.map(&method(:slim_tree_json))

  tree_id = @tree_id.present? ? @tree_id : commit.id
  Cache.write_object(tree_id, json)

  json
end

#set_file(file_path, file) ⇒ Object



37
38
39
# File 'lib/virtual_tree.rb', line 37

def set_file(file_path, file)
  file_subtree(file_path).files[file_path] = file
end

#touch(diff_tree, commit) ⇒ Object

TODO: What happens when a tree moves??



54
55
56
57
58
59
60
61
62
63
# File 'lib/virtual_tree.rb', line 54

def touch(diff_tree, commit)
  if @path == diff_tree[:path]
    @previous_tree_id = diff_tree[:a_tree_id]
    @tree_id = diff_tree[:b_tree_id]
    @commits << commit.id
    @issues << commit.issue_id if commit.issue_id.present?
  else
    find_or_create_subtree!(diff_tree, commit).touch(diff_tree, commit)
  end
end