Class: Artic::Collection::AvailabilityCollection
- Inherits:
-
Array
- Object
- Array
- Artic::Collection::AvailabilityCollection
- Defined in:
- lib/artic/collection/availability_collection.rb
Overview
Keeps a collection of availabilities and performs calculations on them.
Instance Method Summary collapse
-
#by_identifier(identifier) ⇒ Object
Returns all the availabilities with the given identifier, without normalizing them.
-
#identifier?(identifier) ⇒ Boolean
Returns whether an identifier exists in this collection.
-
#identifiers ⇒ Array<Symbol|Date>
Returns all the identifiers in this collection.
-
#normalize(identifier) ⇒ Object
Normalizes all the availabilities with the given identifier in this collection by sorting them and merging any contiguous availability slots.
-
#normalize_all ⇒ Object
Normalizes all the availabilities in this collection by sorting them and merging any contiguous availability slots.
Instance Method Details
#by_identifier(identifier) ⇒ Object
Returns all the availabilities with the given identifier, without normalizing them.
30 31 32 33 34 |
# File 'lib/artic/collection/availability_collection.rb', line 30 def by_identifier(identifier) identifier = cast_identifier identifier availabilities = select { |availability| availability.identifier == identifier } self.class.new availabilities end |
#identifier?(identifier) ⇒ Boolean
Returns whether an identifier exists in this collection.
21 22 23 |
# File 'lib/artic/collection/availability_collection.rb', line 21 def identifier?(identifier) identifiers.include? cast_identifier(identifier) end |
#identifiers ⇒ Array<Symbol|Date>
Returns all the identifiers in this collection.
12 13 14 |
# File 'lib/artic/collection/availability_collection.rb', line 12 def identifiers map(&:identifier).uniq end |
#normalize(identifier) ⇒ Object
Normalizes all the availabilities with the given identifier in this collection by sorting them and merging any contiguous availability slots.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/artic/collection/availability_collection.rb', line 42 def normalize(identifier) availabilities = by_identifier(identifier).sort normalized_availabilities = availabilities.inject([]) do |accumulator, availability| next (accumulator << availability) if accumulator.empty? last_availability = accumulator.pop unless last_availability.time_range.overlaps?(availability.time_range) next ( accumulator + [last_availability, availability] ) end new_time_range = Range.new( [last_availability.time_range.min, availability.time_range.min].min, [last_availability.time_range.max, availability.time_range.max].max ) accumulator << Availability.new(availability.identifier, new_time_range) end self.class.new normalized_availabilities end |
#normalize_all ⇒ Object
Normalizes all the availabilities in this collection by sorting them and merging any contiguous availability slots.
73 74 75 76 77 78 79 |
# File 'lib/artic/collection/availability_collection.rb', line 73 def normalize_all normalized_availabilities = identifiers.flat_map do |identifier| normalize identifier end self.class.new normalized_availabilities.sort end |