Class: Workhours::Period

Inherits:
Object
  • Object
show all
Defined in:
lib/workhours/period.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wday, beginning, ending) ⇒ Period



4
5
6
7
8
9
10
11
12
# 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

Instance Attribute Details

#shiftObject

Returns the value of attribute shift.



3
4
5
# File 'lib/workhours/period.rb', line 3

def shift
  @shift
end

#wdayObject

Returns the value of attribute wday.



3
4
5
# File 'lib/workhours/period.rb', line 3

def wday
  @wday
end

Instance Method Details

#beginningObject



33
34
35
# File 'lib/workhours/period.rb', line 33

def beginning
  shift.beginning
end

#endingObject



36
37
38
# File 'lib/workhours/period.rb', line 36

def ending
  shift.ending
end

#ending_time(time) ⇒ Object



40
41
42
43
44
45
46
# 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

#inspectObject



14
15
16
# 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) ⇒ Boolean



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Boolean



18
19
20
21
# 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) ⇒ Boolean



23
24
25
# File 'lib/workhours/period.rb', line 23

def is_today?(time)
  Workhours.is_today?(wday, time)
end

#is_tomorrow?(time) ⇒ Boolean



26
27
28
# File 'lib/workhours/period.rb', line 26

def is_tomorrow?(time)
  Workhours.is_tomorrow?(wday, time)
end

#is_yesterday?(time) ⇒ Boolean



29
30
31
# File 'lib/workhours/period.rb', line 29

def is_yesterday?(time)
  Workhours.is_yesterday?(wday, time)
end

#overlaps?(other_day) ⇒ Boolean



72
73
74
75
76
77
78
79
80
81
82
83
84
# 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_sObject



68
69
70
# File 'lib/workhours/period.rb', line 68

def to_s
  "#{wday} #{beginning.to_s}-#{ending.to_s}"
end