Class: Availabiliter::TimeSlot

Inherits:
Object
  • Object
show all
Defined in:
lib/availabiliter/time_slot.rb

Overview

A TimeSlot represent a slot in time with a start and an end that can be finite or infinite. It can also act as a boundary in availabilities calculation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starting_time:, ending_time:, boundary: false) ⇒ TimeSlot

Returns a new instance of TimeSlot.



7
8
9
10
11
12
# File 'lib/availabiliter/time_slot.rb', line 7

def initialize(starting_time:, ending_time:, boundary: false)
  @starting_time = starting_time
  @ending_time = ending_time
  @boundary = boundary
  validate
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



5
6
7
# File 'lib/availabiliter/time_slot.rb', line 5

def boundary
  @boundary
end

#ending_timeObject (readonly)

Returns the value of attribute ending_time.



5
6
7
# File 'lib/availabiliter/time_slot.rb', line 5

def ending_time
  @ending_time
end

#starting_timeObject (readonly)

Returns the value of attribute starting_time.



5
6
7
# File 'lib/availabiliter/time_slot.rb', line 5

def starting_time
  @starting_time
end

Instance Method Details

#adjacent?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/availabiliter/time_slot.rb', line 38

def adjacent?(other)
  other.ending_time == previous_second || other.starting_time == next_second
end

#dependent?(other) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/availabiliter/time_slot.rb', line 18

def dependent?(other)
  adjacent?(other) || overlaps?(other)
end

#does_not_overlap?(other) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/availabiliter/time_slot.rb', line 34

def does_not_overlap?(other)
  starting_time > other.ending_time || ending_time < other.starting_time
end

#furthest(other) ⇒ Object



14
15
16
# File 'lib/availabiliter/time_slot.rb', line 14

def furthest(other)
  [self, other].max_by(&:ending_time)
end

#next_secondObject



22
23
24
# File 'lib/availabiliter/time_slot.rb', line 22

def next_second
  ending_time + 1
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/availabiliter/time_slot.rb', line 30

def overlaps?(other)
  !does_not_overlap?(other)
end

#previous_secondObject



26
27
28
# File 'lib/availabiliter/time_slot.rb', line 26

def previous_second
  starting_time - 1
end