class Workhours::Period

Attributes

shift[RW]
wday[RW]

Public Class Methods

new(wday, beginning, ending) click to toggle source
# File lib/workhours/period.rb, line 4
def initialize(wday, beginning, ending)
  @wday = wday
  beginning = Tod::TimeOfDay.parse(beginning) unless beginning.is_a?(Tod::TimeOfDay)
  ending = Tod::TimeOfDay.parse(ending) unless ending.is_a?(Tod::TimeOfDay)
  if ending.second_of_day != 0 && ending < beginning
    raise MultidayPeriodError.new()
  end
  @shift = Tod::Shift.new(beginning, ending)
end

Public Instance Methods

beginning() click to toggle source
# File lib/workhours/period.rb, line 33
def beginning
  shift.beginning
end
ending() click to toggle source
# File lib/workhours/period.rb, line 36
def ending
  shift.ending
end
ending_time(time) click to toggle source
# File lib/workhours/period.rb, line 40
def ending_time(time)
  if shift.ending.second_of_day == 0
    (time.to_date + 1).at(shift.ending)
  else
    time.to_date.at(shift.ending)
  end
end
inspect() click to toggle source
# File lib/workhours/period.rb, line 14
def inspect
  "<Workhours::Period wday:#{wday} beginning:#{beginning.to_s} ending:#{ending.to_s}>"
end
is_active?(time) click to toggle source
# File lib/workhours/period.rb, line 48
def is_active?(time)
  tod = time.to_time_of_day
  if is_today?(time)
    if tod.second_of_day == 0 && ending.second_of_day == 0 && beginning.second_of_day > 0
      # 10:00-0:00 is NOT active on 0:00 of current day, but on 0:00 of next day
      false
    else
      is_inside_range?(tod)
    end
  else
    if tod.second_of_day == 0 && ending.second_of_day == 0 && is_tomorrow?(time)
      # 10:00-0:00 is active on 0:00 of next day
      # 00:00-0:00 is NOT active on 0:00 of next day
      beginning.second_of_day != 0
    else
      false
    end
  end
end
is_inside_range?(tod) click to toggle source
# File lib/workhours/period.rb, line 18
def is_inside_range?(tod)
  return true if shift.beginning.second_of_day == 0 && shift.ending.second_of_day == 0
  shift.include?(tod)
end
is_today?(time) click to toggle source
# File lib/workhours/period.rb, line 23
def is_today?(time)
  Workhours.is_today?(wday, time)
end
is_tomorrow?(time) click to toggle source
# File lib/workhours/period.rb, line 26
def is_tomorrow?(time)
  Workhours.is_tomorrow?(wday, time)
end
is_yesterday?(time) click to toggle source
# File lib/workhours/period.rb, line 29
def is_yesterday?(time)
  Workhours.is_yesterday?(wday, time)
end
overlaps?(other_day) click to toggle source
# File lib/workhours/period.rb, line 72
def overlaps?(other_day)
  if wday != other_day.wday
    return false
  end

  return true if other_day.shift.beginning.second_of_day == 0 && other_day.shift.ending.second_of_day == 0

  open_inside = beginning > other_day.beginning && beginning < other_day.ending
  close_inside = ending > other_day.beginning && ending < other_day.ending
  outside = beginning < other_day.beginning && ending > other_day.ending
  #puts "#{to_s} vs #{other_day.to_s}: oi:#{open_inside} ci:#{close_inside} ou:#{outside}"
  open_inside || close_inside || outside
end
to_s() click to toggle source
# File lib/workhours/period.rb, line 68
def to_s
  "#{wday} #{beginning.to_s}-#{ending.to_s}"
end