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

Returns a new instance of 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 = TimeOfDay.parse(beginning) unless beginning.is_a?(TimeOfDay)
  ending = TimeOfDay.parse(ending) unless ending.is_a?(TimeOfDay)
  if ending.second_of_day != 0 && ending < beginning
    raise MultidayPeriodError.new()
  end
  @shift = 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



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

def beginning
  shift.beginning
end

#endingObject



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

def ending
  shift.ending
end

#ending_time(time) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/workhours/period.rb', line 39

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

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/workhours/period.rb', line 47

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
      true
    else
      false
    end
  end
end

#is_inside_range?(tod) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/workhours/period.rb', line 18

def is_inside_range?(tod)
  shift.include?(tod)
end

#is_today?(time) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_tomorrow?(time) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_yesterday?(time) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#overlaps?(other_day) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/workhours/period.rb', line 70

def overlaps?(other_day)
  if wday != other_day.wday
    return false 
  end
  
  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



66
67
68
# File 'lib/workhours/period.rb', line 66

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