Class: BookingsyncPortal::BookingMap

Inherits:
Object
  • Object
show all
Defined in:
lib/bookingsync_portal/booking_map.rb

Defined Under Namespace

Classes: InvalidLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @from = options.fetch(:from) { Date.today.beginning_of_month }
  @from = @from.to_date

  @length = options.fetch(:length) { 1096 }
  @to = @from.advance(days: @length.to_i)
  @statuses = options.fetch(:statuses) { {
    available:    "0",
    tentative:    "0",
    booked:       "1",
    unavailable:  "1"
  } }
  @bookings = bookings_for_map(bookings, @from, @to)
end

Instance Attribute Details

#bookingsObject (readonly)

Returns the value of attribute bookings.



5
6
7
# File 'lib/bookingsync_portal/booking_map.rb', line 5

def bookings
  @bookings
end

#fromObject (readonly)

Returns the value of attribute from.



5
6
7
# File 'lib/bookingsync_portal/booking_map.rb', line 5

def from
  @from
end

#lengthObject (readonly)

Returns the value of attribute length.



5
6
7
# File 'lib/bookingsync_portal/booking_map.rb', line 5

def length
  @length
end

#statusesObject (readonly)

Returns the value of attribute statuses.



5
6
7
# File 'lib/bookingsync_portal/booking_map.rb', line 5

def statuses
  @statuses
end

#toObject (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

Parameters:

  • source (String)

    The source map (expecting a map of ‘1` and `0` only)

  • destination (String)

    The destination map (expecting a map of ‘1` and `0` only)

Returns:

  • (String)

    A map with the changes between both maps: _ - Identical 1 - Create (present on source only) 0 - Destroy (present on destination only)

Raises:



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.

Parameters:

  • map (String)

    A map String

  • from (Date)

    The day that the map starts

Returns:

  • (Array<Hash>)

    Returns an Array of Hashes with all the ranges



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

#mapObject



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