Class: Migration

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/migration/migrate_old_entries.rb

Instance Method Summary collapse

Methods included from Utils

#get_filename, #minutes_to_time

Instance Method Details

#transform_file(old_fname) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/migration/migrate_old_entries.rb', line 10

def transform_file(old_fname)
  config = WorkLoggerConfiguration.load

  old_file_match = old_fname.match(/(?<year>\d\d\d\d)-(?<month>\d\d)-(?<day>\d\d).txt/)

  unless old_file_match.nil?
    year = old_file_match[:year]
    month = old_file_match[:month]
    day = old_file_match[:day]

    new_fname = get_filename(config, year, month, day)

    old_file = File.open(old_fname, 'r')
    new_file = File.open(new_fname, 'w')

    old_file.each_line do |line|
      match = line.match(/(\d\d?:\d\d).+/)&.captures

      new_file.write("#{match[0]}\n") unless match.nil? || match.empty?
    end

    old_file.close
    new_file.close
  end
end