Class: ExampleGroupTimer::TimedItem

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

Direct Known Subclasses

TimedGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, parent = nil) ⇒ TimedItem

Returns a new instance of TimedItem.



5
6
7
8
# File 'lib/example_group_timer/timed_item.rb', line 5

def initialize(item, parent = nil)
  @item, @parent = item, parent
  start
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



3
4
5
# File 'lib/example_group_timer/timed_item.rb', line 3

def item
  @item
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/example_group_timer/timed_item.rb', line 3

def parent
  @parent
end

Instance Method Details

#descriptionObject



43
44
45
# File 'lib/example_group_timer/timed_item.rb', line 43

def description
  item.description.strip
end

#durationObject



18
19
20
# File 'lib/example_group_timer/timed_item.rb', line 18

def duration
  @finished_at - @started_at
end

#finishObject



14
15
16
# File 'lib/example_group_timer/timed_item.rb', line 14

def finish
  @finished_at = Time.now
end

#indentObject



22
23
24
# File 'lib/example_group_timer/timed_item.rb', line 22

def indent
  parent.indent + 1
end

#percentageObject



26
27
28
# File 'lib/example_group_timer/timed_item.rb', line 26

def percentage
  (duration.to_f / parent.duration.to_f) * 100
end

#puts(*args) ⇒ Object



47
48
49
# File 'lib/example_group_timer/timed_item.rb', line 47

def puts(*args)
  parent.puts *args
end

#reportObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/example_group_timer/timed_item.rb', line 30

def report
  template = <<-EOS
<li>
  <div class="example-group">
<span class="title">%s</span>
<span class="duration">%.4f</span>
<span class="share">%.1f%%</span>
  </div>
</li>
EOS
  puts template % [description, duration, percentage]
end

#startObject



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

def start
  @started_at = Time.now
end