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.
29 30 31 32 33 |
# File 'lib/artic/collection/availability_collection.rb', line 29 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.
20 21 22 |
# File 'lib/artic/collection/availability_collection.rb', line 20 def identifier?(identifier) identifiers.include? cast_identifier(identifier) end |
#identifiers ⇒ Array<Symbol|Date>
Returns all the identifiers in this collection.
11 12 13 |
# File 'lib/artic/collection/availability_collection.rb', line 11 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/artic/collection/availability_collection.rb', line 41 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 next ( accumulator + [last_availability, availability] ) unless last_availability.time_range.overlaps?(availability.time_range) 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.
70 71 72 73 74 75 76 |
# File 'lib/artic/collection/availability_collection.rb', line 70 def normalize_all normalized_availabilities = identifiers.flat_map do |identifier| normalize identifier end self.class.new normalized_availabilities.sort end |