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

#durationObject



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

def duration
  '%.5f' % [@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
  ('%.1f%' % [(duration.to_f / parent.duration.to_f) * 100]).rjust(5)
end

#report_headerObject



30
31
32
33
# File 'lib/example_group_timer/timed_item.rb', line 30

def report_header
  truncated = (('  ' * indent) + item.description.strip)[0..59].ljust(60)
  puts "#{truncated} #{duration} #{percentage}"
end

#startObject



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

def start
  @started_at = Time.now
end