class Times::TimeProcesser

processes new times and dates with current times and dates

Attributes

date[RW]
et[RW]
st[RW]

Public Class Methods

reset() click to toggle source

resets the class variables so that a new file can be parsed is called by LearningDiary (through Paragraph) when preparing to parse a new txt file

# File lib/docfolio/paragraph_modules/times.rb, line 21
def self.reset
  TimeProcesser.date = TimeProcesser.st = TimeProcesser.et = nil
end

Public Instance Methods

process_times(new_times) click to toggle source

Takes class start end times and dates as Time objects and amends the times, advancing the date if the start time has crossed midnight. @param [Array] new_times An array containing the from hour, from min,

to hour, to min
# File lib/docfolio/paragraph_modules/times.rb, line 8
def process_times(new_times)
  f_hour, f_min, t_hour, t_min = new_times
  to_start_time(f_hour, f_min) if has f_hour
  to_end_time(t_hour, t_min) if has t_hour
end

Private Instance Methods

a_day() click to toggle source

returns one day in seconds and adds a day to the @date

# File lib/docfolio/paragraph_modules/times.rb, line 55
def a_day
  if TimeProcesser.date.nil?
    fail('needs date')
  else
    TimeProcesser.date += 86_400
  end
  86_400
end
extract_time_object(hour, min, from) click to toggle source

Adds a given number and hours and minutes to a Time object @param [Time] from time to which hours and minutes are added @param [Number] hour hours to add to from @param [Number] min minutes to add to from @return [Time] the result of hours and minutes after from

# File lib/docfolio/paragraph_modules/times.rb, line 69
def extract_time_object(hour, min, from)
  seconds = (hour.to_i * 3600) + (min.to_i * 60)
  Time.at(from.to_i + seconds)
end
has(time_date_component) click to toggle source

Improves readability of boolean condition statements. Is used from Time objects and integer hour component, but it works for any object @param [Object] time_date_component Any object @return [Boolean] true if the parameter is not nil

# File lib/docfolio/paragraph_modules/times.rb, line 50
def has(time_date_component)
  !time_date_component.nil?
end
to_end_time(t_hour, t_min) click to toggle source

Set the end time to the current paragraph date at t_hour and t_min. If the end time is before the start time, assume the end time is for the next day and add a day. @param [Number] t_hour To hours @param [Number] t_min To minutes

# File lib/docfolio/paragraph_modules/times.rb, line 32
def to_end_time(t_hour, t_min)
  # extract_time_object simply adds hours and mins to date
  TimeProcesser.et = extract_time_object(t_hour, t_min, TimeProcesser.date)
  # if end_time before start_time, assume it is the following day
  TimeProcesser.et += a_day if TimeProcesser.et < TimeProcesser.st
end
to_start_time(hour, min) click to toggle source

Adds hours and mins to date (Time). Then adds a day if the start time is before the end time. Finally, makes end time nil

# File lib/docfolio/paragraph_modules/times.rb, line 41
def to_start_time(hour, min)
  # extract_time_object simply adds hours and mins to date
  TimeProcesser.st = extract_time_object(hour, min, TimeProcesser.date)
end