Class: Artic::Collection::OccupationCollection
- Inherits:
-
Array
- Object
- Array
- Artic::Collection::OccupationCollection
- Defined in:
- lib/artic/collection/occupation_collection.rb
Overview
Keeps a collection of occupations.
Instance Method Summary collapse
-
#bisect(availability) ⇒ Object
Sorts the occupations in the collection, then bisects the availability until all the occupations have been accounted for.
-
#by_date(date) ⇒ Object
Returns all the occupations for the given date, without normalizing them.
-
#date?(date) ⇒ Boolean
Returns whether a date exists in this collection.
-
#dates ⇒ Array<Date>
Returns all the dates in this collection.
-
#normalize(date) ⇒ Object
Normalizes all the occupations with the given identifier in this collection by sorting them and merging any contiguous availability slots.
-
#normalize_all ⇒ Object
Normalizes all the occupations in this collection by sorting them and merging any contiguous availability slots.
Instance Method Details
#bisect(availability) ⇒ Object
Sorts the occupations in the collection, then bisects the availability until all the occupations have been accounted for.
88 89 90 91 92 93 94 95 |
# File 'lib/artic/collection/occupation_collection.rb', line 88 def bisect(availability) availabilities = normalize_all.inject([availability]) do |accumulator, occupation| break accumulator if accumulator.empty? accumulator + occupation.bisect(accumulator.pop) end AvailabilityCollection.new availabilities end |
#by_date(date) ⇒ Object
Returns all the occupations for the given date, without normalizing them.
30 31 32 33 |
# File 'lib/artic/collection/occupation_collection.rb', line 30 def by_date(date) occupations = select { |occupation| occupation.date == date } self.class.new occupations end |
#date?(date) ⇒ Boolean
Returns whether a date exists in this collection.
21 22 23 |
# File 'lib/artic/collection/occupation_collection.rb', line 21 def date?(date) dates.include? date end |
#dates ⇒ Array<Date>
Returns all the dates in this collection.
12 13 14 |
# File 'lib/artic/collection/occupation_collection.rb', line 12 def dates map(&:date).uniq end |
#normalize(date) ⇒ Object
Normalizes all the occupations with the given identifier in this collection by sorting them and merging any contiguous availability slots.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/artic/collection/occupation_collection.rb', line 41 def normalize(date) occupations = by_date(date).sort normalized_occupations = occupations.inject([]) do |accumulator, occupation| next (accumulator << occupation) if accumulator.empty? last_occupation = accumulator.pop unless last_occupation.time_range.overlaps?(occupation.time_range) next ( accumulator + [last_occupation, occupation] ) end new_time_range = Range.new( [last_occupation.time_range.min, occupation.time_range.min].min, [last_occupation.time_range.max, occupation.time_range.max].max ) accumulator << Occupation.new(occupation.date, new_time_range) end self.class.new normalized_occupations end |
#normalize_all ⇒ Object
Normalizes all the occupations in this collection by sorting them and merging any contiguous availability slots.
72 73 74 75 76 77 78 |
# File 'lib/artic/collection/occupation_collection.rb', line 72 def normalize_all normalized_occupations = dates.sort.flat_map do |date| normalize date end self.class.new normalized_occupations end |