forked from edendevelopment/edash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress_report.rb
42 lines (37 loc) · 932 Bytes
/
progress_report.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module EDash
class ProgressReport
class Phase
attr_accessor :name, :value, :width
def initialize(name, value, width)
@name = name
@value = value
@width = width
end
def adjust_width_to(width)
(width * @width / 100).to_i
end
end
def initialize(json)
phase_pairs = JSON.parse(json)
@phases = create_phases_from_pairs(phase_pairs)
end
def each(&block)
@phases.each(&block)
end
private
def create_phases_from_pairs(pairs)
total_width = calculate_total_width(pairs)
pairs.collect do |pair|
Phase.new(pair.first, pair.last.to_i, width_for(pair.last.to_i, total_width))
end
end
def calculate_total_width(pairs)
pairs.inject(0) do |sum, pair|
sum += pair.last.to_i
end
end
def width_for(value, total_width)
value * 100 / total_width.to_f
end
end
end