Class: Artic::Occupation

Inherits:
Object
  • Object
show all
Defined in:
lib/artic/occupation.rb

Overview

An occupation represents a slot of time in a given date where you are not available.

Author:

  • Alessandro Desantis

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, time_range) ⇒ Occupation

Initializes the occupation.

Parameters:

  • date (Date)

    the date of the occupation

  • time_range (TimeRange|Range)

    the time range of the occupation



13
14
15
16
# File 'lib/artic/occupation.rb', line 13

def initialize(date, time_range)
  @date = date
  @time_range = TimeRange.build(time_range)
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



7
8
9
# File 'lib/artic/occupation.rb', line 7

def date
  @date
end

#time_rangeObject (readonly)

Returns the value of attribute time_range.



7
8
9
# File 'lib/artic/occupation.rb', line 7

def time_range
  @time_range
end

Instance Method Details

#<=>(other) ⇒ Fixnum

Compares this occupation with another one by comparing their ranges’ start.

Parameters:

Returns:

  • (Fixnum)

    -1 if this occupation should come before the other one, 0 if it should be in the same position, 1 if it should come after the other one



70
71
72
# File 'lib/artic/occupation.rb', line 70

def <=>(other)
  to_range.min <=> other.to_range.min
end

#==(other) ⇒ Boolean

Determines whether this occupation and the one passed as an argument represent the same day/time range combination, by checking for equality of both the date and the time range.

Parameters:

Returns:

  • (Boolean)


60
61
62
# File 'lib/artic/occupation.rb', line 60

def ==(other)
  date == other.date && time_range == other.time_range
end

#bisect(availability) ⇒ Object

Reduces the time range of the given availability or bisects it into two availabilities, depending on where the occupation falls within the availability.

If the occupation completely covers the availability, returns an empty collection.

If the occupation does not overlap the availability at all, returns a collection with the original availability.

Examples:

occupation = Artic::Occupation.new(Date.today, '08:00'..'14:00')
availability = Artic::Availability.new(Date.today, '09:00'..'18:00')

occupation.bisect(availability)
# => [
#   #<Artic::Availability 14:00..18:00>
# ]
occupation = Artic::Occupation.new(Date.today, '12:00'..'14:00')
availability = Artic::Availability.new(Date.today, '09:00'..'18:00')

occupation.bisect(availability)
# => [
#   #<Artic::Availability 09:00..12:00>,
#   #<Artic::Availability 14:00..18:00>
# ]
occupation = Artic::Occupation.new(Date.today, '16:00'..'20:00')
availability = Artic::Availability.new(Date.today, '09:00'..'18:00')

occupation.bisect(availability)
# => [
#   #<Artic::Availability 09:00..16:00>
# ]

Parameters:

Returns:

  • Collection::AvailabilityCollection a collection of availabilities where the time slot assigned to the occupation has been removed



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/artic/occupation.rb', line 114

def bisect(availability)
  return Collection::AvailabilityCollection.new if covers?(availability)
  return Collection::AvailabilityCollection.new([availability]) unless overlaps?(availability)

  bisected_ranges = time_range.bisect(availability.time_range)

  availabilities = bisected_ranges.map do |bisected_range|
    Availability.new(date, bisected_range)
  end

  Collection::AvailabilityCollection.new availabilities
end

#covers?(availability) ⇒ Boolean

Returns whether the occupation covers the availability (i.e. whether all moments of the availability are also part of the occupation).

Parameters:

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/artic/occupation.rb', line 46

def covers?(availability)
  return false unless availability.for_date?(date)

  availability_range = availability.time_range.with_date(date)
  to_range.min <= availability_range.min && to_range.max >= availability_range.max
end

#overlaps?(availability) ⇒ Boolean

Returns whether the occupation overlaps the availability (i.e. whether there’s at least one moment in time shared by both the availability and the occupation).

Parameters:

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/artic/occupation.rb', line 33

def overlaps?(availability)
  return false unless availability.for_date?(date)

  availability_range = availability.time_range.with_date(date)
  (availability_range.min <= to_range.max) && (availability_range.max >= to_range.min)
end

#to_rangeRange

Converts the occupation to a range of DateTime objects.

Returns:

  • (Range)

See Also:



23
24
25
# File 'lib/artic/occupation.rb', line 23

def to_range
  time_range.with_date(date)
end