Class: WorkingTimes::Record

Inherits:
Object
  • Object
show all
Includes:
State
Defined in:
lib/working_times/record.rb

Constant Summary collapse

OPTIONS =
{ headers: true, return_headers: true, write_headers: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp:, comment: nil, duration: nil) ⇒ Record

Returns a new instance of Record.



12
13
14
15
16
# File 'lib/working_times/record.rb', line 12

def initialize(timestamp:, comment: nil, duration: nil)
  @timestamp = timestamp
  @comment   = comment
  @duration  = duration
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



10
11
12
# File 'lib/working_times/record.rb', line 10

def comment
  @comment
end

#durationObject (readonly)

Returns the value of attribute duration.



10
11
12
# File 'lib/working_times/record.rb', line 10

def duration
  @duration
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



10
11
12
# File 'lib/working_times/record.rb', line 10

def timestamp
  @timestamp
end

Instance Method Details

#finishObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/working_times/record.rb', line 24

def finish
  updated_csv = ''
  CSV.filter(File.open(path_current_term), updated_csv, OPTIONS) do |row|
    next if row.header_row?
    next unless row['finished_at'].empty?

    row['finished_at'] = timestamp.rfc3339
    row['comment'] = comment
  end
  File.write(path_current_term, updated_csv)
end

#restObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/working_times/record.rb', line 36

def rest
  parse_rest_sec

  updated_csv = ''
  CSV.filter(File.open(path_current_term), updated_csv, OPTIONS) do |row|
    next if row.header_row?
    next unless row['finished_at'].empty?

    row['rest_sec'] = @rest_sec
  end
  File.write(path_current_term, updated_csv)
end

#startObject



18
19
20
21
22
# File 'lib/working_times/record.rb', line 18

def start
  CSV.open(path_current_term, 'a+', OPTIONS) do |csv|
    csv.puts([timestamp.rfc3339, '', 0, comment])
  end
end