class TimesheetReader::Core

Core class

Attributes

converter[R]
file_paths[R]
parser[R]

Public Class Methods

new(file_paths) click to toggle source
# File lib/timesheet_reader/core.rb, line 8
def initialize(file_paths)
  @file_paths = file_paths
  @parser = TimesheetReader::Parser.new
  @converter = TimesheetReader::Converter.new
end

Public Instance Methods

run() click to toggle source
# File lib/timesheet_reader/core.rb, line 14
def run
  timesheets = transform_timesheets(
    generate_timesheets
  )

  total_time = sum_time(timesheets)
  total_hours, total_minutes = @converter.minutes_to_time(total_time)

  {
    total_hours: total_hours,
    total_minutes: total_minutes,
    timesheets: timesheets
  }
end

Private Instance Methods

calculate_time_diff(lines_in_minutes) click to toggle source
# File lib/timesheet_reader/core.rb, line 55
def calculate_time_diff(lines_in_minutes)
  diffs = []

  lines_in_minutes.each_with_index do |time, index|
    next if index.odd?

    next_time = lines_in_minutes[index + 1]
    diff = next_time - time

    diffs.push(diff)
  end

  diffs.reduce(0) { |acc, time_diff| acc + time_diff }
end
generate_timesheets() click to toggle source
# File lib/timesheet_reader/core.rb, line 31
def generate_timesheets
  @file_paths.map { |path| @parser.run(path) }
end
map_time_to_minutes(timesheet_lines) click to toggle source
# File lib/timesheet_reader/core.rb, line 49
def map_time_to_minutes(timesheet_lines)
  timesheet_lines.map do |line|
    line[:minutes] + @converter.hour_to_minutes(line[:hours])
  end
end
sum_time(timesheets) click to toggle source
# File lib/timesheet_reader/core.rb, line 70
def sum_time(timesheets)
  timesheets.reduce(0) { |acc, timesheet| acc + timesheet[:time] }
end
transform_timesheets(timesheets) click to toggle source
# File lib/timesheet_reader/core.rb, line 35
def transform_timesheets(timesheets)
  timesheets.map do |timesheet|
    lines = map_time_to_minutes(timesheet[:lines])
    time = calculate_time_diff(lines)
    hours, minutes = @converter.minutes_to_time(time)

    {
      filename: timesheet[:filename],
      time: time, hours: hours,
      minutes: minutes
    }
  end
end