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.
85 86 87 88 89 90 91 92 |
# File 'lib/artic/collection/occupation_collection.rb', line 85 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.
29 30 31 32 |
# File 'lib/artic/collection/occupation_collection.rb', line 29 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.
20 21 22 |
# File 'lib/artic/collection/occupation_collection.rb', line 20 def date?(date) dates.include? date end |
#dates ⇒ Array<Date>
Returns all the dates in this collection.
11 12 13 |
# File 'lib/artic/collection/occupation_collection.rb', line 11 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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/artic/collection/occupation_collection.rb', line 40 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 next ( accumulator + [last_occupation, occupation] ) unless last_occupation.time_range.overlaps?(occupation.time_range) 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.
69 70 71 72 73 74 75 |
# File 'lib/artic/collection/occupation_collection.rb', line 69 def normalize_all normalized_occupations = dates.sort.flat_map do |date| normalize date end self.class.new normalized_occupations end |