Class: GitReporting::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/git_reporting/report.rb,
lib/git_reporting/report/type.rb,
lib/git_reporting/report/group.rb,
lib/git_reporting/report/commit.rb

Direct Known Subclasses

Commit, Group::Base, Type::Base

Defined Under Namespace

Modules: Group, Type Classes: Commit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Report

Returns a new instance of Report.



27
28
29
30
# File 'lib/git_reporting/report.rb', line 27

def initialize(key = nil)
  @key = key
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.build(commits) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/git_reporting/report.rb', line 15

def self.build(commits)
  hash = Hash.new { |h, k| h[k] = [] }
  commits.each { |commit| hash[key_for_commit(commit)] << commit }
  hash.map do |key, commits|
    new(key) << (block_given? ? yield(commits) : Commit.new_from_collection(commits))
  end
end

.key_for_commit(commit) ⇒ Object



23
24
25
# File 'lib/git_reporting/report.rb', line 23

def self.key_for_commit(commit)
  nil
end

.new_from_collection(collection) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/git_reporting/report.rb', line 9

def self.new_from_collection(collection)
  raise ArgumentError, "#{collection.inspect} is not a collection" unless collection.respond_to?(:each)

  collection.map { |item| new(item) }
end

Instance Method Details

#append_child(child) ⇒ Object Also known as: <<



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git_reporting/report.rb', line 32

def append_child(child)
  if child.respond_to?(:each)
    child.each { |c| self.append_child(c) }
  else
    raise ArgumentError, "Attempting to add a nil node" unless child
    raise ArgumentError, "Only Report and its descendants can be added as a children" unless child.is_a?(Report)
    raise ArgumentError, "Attempting add node to itself" if self.equal?(child)

    children << child
  end
  self
end


54
55
56
57
58
59
# File 'lib/git_reporting/report.rb', line 54

def print
  puts to_s
  children.each do |child|
    child.print
  end
end

#timeObject



46
47
48
# File 'lib/git_reporting/report.rb', line 46

def time
  @time ||= children.inject(0) { |time, child| time + child.time }
end

#to_sObject



50
51
52
# File 'lib/git_reporting/report.rb', line 50

def to_s
  "#{self.class.name} : #{key} : #{time}"
end