Class: Artic::Occupation
- Inherits:
-
Object
- Object
- Artic::Occupation
- Defined in:
- lib/artic/occupation.rb
Overview
An occupation represents a slot of time in a given date where you are not available.
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#time_range ⇒ Object
readonly
Returns the value of attribute time_range.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
Compares this occupation with another one by comparing their ranges’ start.
-
#==(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.
-
#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.
-
#covers?(availability) ⇒ Boolean
Returns whether the occupation covers the availability (i.e. whether all moments of the availability are also part of the occupation).
-
#initialize(date, time_range) ⇒ Occupation
constructor
Initializes the occupation.
-
#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).
-
#to_range ⇒ Range
Converts the occupation to a range of
DateTime
objects.
Constructor Details
#initialize(date, time_range) ⇒ Occupation
Initializes 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
#date ⇒ Object (readonly)
Returns the value of attribute date.
7 8 9 |
# File 'lib/artic/occupation.rb', line 7 def date @date end |
#time_range ⇒ Object (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.
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.
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.
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).
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).
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_range ⇒ Range
Converts the occupation to a range of DateTime
objects.
23 24 25 |
# File 'lib/artic/occupation.rb', line 23 def to_range time_range.with_date(date) end |