Class: BookingsyncPortal::BookingMap
- Inherits:
-
Object
- Object
- BookingsyncPortal::BookingMap
- Defined in:
- lib/bookingsync_portal/booking_map.rb
Defined Under Namespace
Classes: InvalidLength
Instance Attribute Summary collapse
-
#bookings ⇒ Object
readonly
Returns the value of attribute bookings.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#statuses ⇒ Object
readonly
Returns the value of attribute statuses.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
-
.diff(source, destination) ⇒ String
Diff maps.
-
.extract_ranges(map, from) ⇒ Array<Hash>
Extract ranges.
Instance Method Summary collapse
-
#initialize(bookings, options = {}) ⇒ BookingMap
constructor
A new instance of BookingMap.
- #map ⇒ Object
Constructor Details
#initialize(bookings, options = {}) ⇒ BookingMap
Returns a new instance of BookingMap.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/bookingsync_portal/booking_map.rb', line 7 def initialize(bookings, = {}) @from = .fetch(:from) { Date.today.beginning_of_month } @from = @from.to_date @length = .fetch(:length) { 1096 } @to = @from.advance(days: @length.to_i) @statuses = .fetch(:statuses) { { available: "0", tentative: "0", booked: "1", unavailable: "1" } } @bookings = bookings_for_map(bookings, @from, @to) end |
Instance Attribute Details
#bookings ⇒ Object (readonly)
Returns the value of attribute bookings.
5 6 7 |
# File 'lib/bookingsync_portal/booking_map.rb', line 5 def bookings @bookings end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
5 6 7 |
# File 'lib/bookingsync_portal/booking_map.rb', line 5 def from @from end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
5 6 7 |
# File 'lib/bookingsync_portal/booking_map.rb', line 5 def length @length end |
#statuses ⇒ Object (readonly)
Returns the value of attribute statuses.
5 6 7 |
# File 'lib/bookingsync_portal/booking_map.rb', line 5 def statuses @statuses end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
5 6 7 |
# File 'lib/bookingsync_portal/booking_map.rb', line 5 def to @to end |
Class Method Details
.diff(source, destination) ⇒ String
Diff maps
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bookingsync_portal/booking_map.rb', line 44 def diff(source, destination) raise InvalidLength if source.size != destination.size new_result = "_" * source.size source.chars.each_with_index do |char, index| new_result[index] = char if destination[index] != char end new_result end |
.extract_ranges(map, from) ⇒ Array<Hash>
Extract ranges
Returns all the ranges with their statuses. The ‘_` is considered as empty and won’t be returned as a range.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bookingsync_portal/booking_map.rb', line 62 def extract_ranges(map, from) ranges = [] previous_char = nil range = nil map_length = map.length map.chars.each_with_index do |char, index| if char != previous_char && char != "_" # finished ranges << range if range range = { start_at: from.advance(days: index), end_at: from.advance(days: index + 1) }.merge(status: char) elsif char != "_" # continuing range[:end_at] = from.advance(days: index + 1) end if index == map_length - 1 ranges << range if range end previous_char = char end ranges end |
Instance Method Details
#map ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bookingsync_portal/booking_map.rb', line 22 def map days = get_days_hash(from, to, statuses[:available].to_s) bookings.each do |booking| (booking.start_at.to_date...booking.end_at.to_date).each do |day| if day >= from && day < to days[day] = statuses[booking.status.downcase.to_sym] end end end days.values.join end |