Class: Chronolog::Engine
- Inherits:
-
Object
- Object
- Chronolog::Engine
- Defined in:
- lib/chronolog/engine.rb
Instance Method Summary collapse
-
#initialize(file) ⇒ Engine
constructor
A new instance of Engine.
- #print(unit = "hours") ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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
#print(unit = "hours") ⇒ Object
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 |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |