Class: Tod::Shift

Inherits:
Object
  • Object
show all
Defined in:
lib/tod/shift.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beginning, ending) ⇒ Shift

Returns a new instance of Shift.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
# File 'lib/tod/shift.rb', line 5

def initialize(beginning, ending)
  raise ArgumentError, "beginning can not be nil" unless beginning
  raise ArgumentError, "ending can not be nil" unless ending

  @beginning = beginning
  @ending = ending
  freeze # Shift instances are value objects
end

Instance Attribute Details

#beginningObject (readonly)

Returns the value of attribute beginning.



3
4
5
# File 'lib/tod/shift.rb', line 3

def beginning
  @beginning
end

#endingObject (readonly)

Returns the value of attribute ending.



3
4
5
# File 'lib/tod/shift.rb', line 3

def ending
  @ending
end

Instance Method Details

#durationObject

Return shift duration in seconds. if ending is lower than beginning this method will calculate the duration as the ending time is from the following day



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tod/shift.rb', line 27

def duration
  if ending >= beginning
    (ending.to_i - beginning.to_i)
  else
    start_of_day   = TimeOfDay.new(0,0,0)
    end_of_day     = TimeOfDay.new(23,59,59)
    duration_day_1 = (end_of_day.to_i - beginning.to_i) + 1
    duration_day_2 = (ending.to_i - start_of_day.to_i)
    duration_day_1 + duration_day_2
  end
end

#include?(tod) ⇒ Boolean

Returns true if the time of day is inside the shift (inclusive range), false otherwise

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/tod/shift.rb', line 15

def include?(tod)
  if ending >= beginning
    tod >= beginning && tod <= ending
  else
    start_of_day   = TimeOfDay.new(0,0,0)
    end_of_day     = TimeOfDay.new(23,59,59)
    (tod >= beginning && tod <= end_of_day) || (tod >= start_of_day && tod <= ending)
  end
end