Class: Chronolog::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/chronolog/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Engine

Returns a new instance of Engine.



5
6
7
8
9
10
# File 'lib/chronolog/engine.rb', line 5

def initialize(file)
  @csv = CSV.new(file, col_sep: " ")

  setup_variables
  read_csv
end

Instance Method Details



26
27
28
29
30
31
32
33
34
# File 'lib/chronolog/engine.rb', line 26

def print(unit = "hours")
  started_at = @started_at || Time.now
  stopped_at = Time.now
  current = stopped_at - started_at
  %w[year month day session].each do |period|
    duration = (current + previous(period, started_at)) / unit_length(unit)
    puts format("This %- 10s % 7.2f %s", "#{period}:", duration, unit)
  end
end

#startObject



12
13
14
15
16
# File 'lib/chronolog/engine.rb', line 12

def start
  raise "Received 'start' after 'start'" unless @started_at.nil?
  @started_at = Time.now
  @csv << ["started", "at", @started_at.to_i]
end

#stopObject



18
19
20
21
22
23
24
# File 'lib/chronolog/engine.rb', line 18

def stop
  raise "Received 'stop' before 'start'" if @started_at.nil?
  @stopped_at = Time.now
  @csv << ["stopped", "at", @stopped_at.to_i]
  update_variables
  @started_at = nil
end