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
Time
objects.
Constructor Details
#initialize(date, time_range) ⇒ Occupation
Initializes the occupation.
14 15 16 17 |
# File 'lib/artic/occupation.rb', line 14 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.
8 9 10 |
# File 'lib/artic/occupation.rb', line 8 def date @date end |
#time_range ⇒ Object (readonly)
Returns the value of attribute time_range.
8 9 10 |
# File 'lib/artic/occupation.rb', line 8 def time_range @time_range end |
Instance Method Details
#<=>(other) ⇒ Fixnum
Compares this occupation with another one by comparing their ranges’ start.
71 72 73 |
# File 'lib/artic/occupation.rb', line 71 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.
61 62 63 |
# File 'lib/artic/occupation.rb', line 61 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.
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/artic/occupation.rb', line 115 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).
47 48 49 50 51 52 |
# File 'lib/artic/occupation.rb', line 47 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).
34 35 36 37 38 39 |
# File 'lib/artic/occupation.rb', line 34 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 Time
objects.
24 25 26 |
# File 'lib/artic/occupation.rb', line 24 def to_range time_range.with_date(date) end |