Class: Hubtime::Activity::Period

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

Direct Known Subclasses

Day, Forever, Month, Year

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, example_time = nil) ⇒ Period

Returns a new instance of Period.



8
9
10
11
12
13
14
15
# File 'lib/hubtime/activity.rb', line 8

def initialize(label, example_time=nil)
  @example = example_time
  @label = label
  @children = {}
  @total_stats = default_stats
  @repo_stats  = Hash.new{ |hash, key| hash[key] = default_stats }
  @compiled = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#compiledObject (readonly)

Returns the value of attribute compiled.



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

def compiled
  @compiled
end

#exampleObject (readonly)

Returns the value of attribute example.



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

def example
  @example
end

#labelObject (readonly)

Returns the value of attribute label.



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

def label
  @label
end

#repo_statsObject (readonly)

Returns the value of attribute repo_stats.



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

def repo_stats
  @repo_stats
end

#total_statsObject (readonly)

Returns the value of attribute total_stats.



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

def total_stats
  @total_stats
end

Instance Method Details

#_first_child_key(parent) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/hubtime/activity.rb', line 142

def _first_child_key(parent)
  if first? && (!parent || parent.first?) && children.keys.size > 0
    children.keys.sort.first
  else
    first_child_key
  end
end

#_last_child_key(parent) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/hubtime/activity.rb', line 150

def _last_child_key(parent)
  if last? && (!parent || parent.last?) && children.keys.size > 0
    children.keys.sort.last
  else
    last_child_key
  end
end

#add(commit) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hubtime/activity.rb', line 17

def add(commit)
  self.commits << commit
  self.total_stats.keys.each do |key|
    self.total_stats[key] += commit.send(key)
  end
  self.repo_stats[commit.repo_name].keys.each do |key|
    self.repo_stats[commit.repo_name][key] += commit.send(key)
  end
  
  if self.class.child_class
    klass = self.class.child_class
    key = klass.display(commit.time)
    @children[key] ||= klass.new(key, commit.time)
    @children[key].add(commit)
  end
end

#additions(repo = nil) ⇒ Object



96
97
98
# File 'lib/hubtime/activity.rb', line 96

def additions(repo=nil)
  key_value("additions", repo)
end

#child_keys(parent) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/hubtime/activity.rb', line 134

def child_keys(parent)
  return [] if self.class.child_class.nil?

  first = self._first_child_key(parent)
  last = self._last_child_key(parent)
  (first..last).to_a
end

#commitsObject



84
85
86
# File 'lib/hubtime/activity.rb', line 84

def commits
  @commits ||= []
end

#compile!(parent) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hubtime/activity.rb', line 38

def compile!(parent)
  return if compiled?
  first = nil
  last = nil
  filled = {}
  child_keys(parent).each do |key|
    filled[key] = @children[key]
    if filled[key]
      first ||= filled[key]
      last = filled[key]
    else
      filled[key] = self.class.child_class.new(key)
    end
  end

  first.first! if first
  last.last! if last

  filled.values.each do |child|
    child.compile!(self)
  end

  @children = filled

  @compiled = {}
  @compiled["total_stats"] = self.total_stats
  @compiled["repo_stats"]  = self.repo_stats

  @compiled["children"] = {}
  @children.each do |key, period|
    @compiled["children"][key] = period.compiled
  end
end

#compiled?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/hubtime/activity.rb', line 34

def compiled?
  !!@compiled
end

#count(repo = nil) ⇒ Object



92
93
94
# File 'lib/hubtime/activity.rb', line 92

def count(repo=nil)
  key_value("count", repo)
end

#default_statsObject



158
159
160
# File 'lib/hubtime/activity.rb', line 158

def default_stats
  {"additions" => 0, "deletions" => 0, "impact" => 0, "count" => 0}
end

#deletions(repo = nil) ⇒ Object



100
101
102
# File 'lib/hubtime/activity.rb', line 100

def deletions(repo=nil)
  key_value("deletions", repo)
end

#first!Object



118
119
120
# File 'lib/hubtime/activity.rb', line 118

def first!
  @first = true
end

#first?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/hubtime/activity.rb', line 122

def first?
  !!@first
end

#impact(repo = nil) ⇒ Object



104
105
106
# File 'lib/hubtime/activity.rb', line 104

def impact(repo=nil)
  key_value("impact", repo)
end

#import(stats) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hubtime/activity.rb', line 72

def import(stats)
  child_list  = stats.delete("children")
  @compiled    = stats
  @total_stats = stats["total_stats"]
  @repo_stats  = stats["repo_stats"]
  @children = {}
  child_list.each do |key, child_stats|
    @children[key] = self.class.child_class.new(key)
    @children[key].import(child_stats)
  end
end

#key_value(key, repo = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/hubtime/activity.rb', line 108

def key_value(key, repo = nil)
  if repo.nil? || repo == "total"
    hash = total_stats
  else
    hash = repo_stats[repo]
  end
  return 0 unless hash
  hash[key.to_s]
end

#last!Object



126
127
128
# File 'lib/hubtime/activity.rb', line 126

def last!
  @last = true
end

#last?Boolean

Returns:

  • (Boolean)


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

def last?
  !!@last
end

#repositoriesObject



88
89
90
# File 'lib/hubtime/activity.rb', line 88

def repositories
  repo_stats.keys
end