class BestSeats::Finder

Constants

DEFAULT_OPTIONS
INITIAL_SEAT_INDEX

Attributes

input[R]
seats_requested[R]

Public Class Methods

new(input, seats_requested, options = {}) click to toggle source
# File lib/best_seats/finder.rb, line 32
def initialize(input, seats_requested, options = {})
  @input = input
  @seats_requested = seats_requested
  @options = DEFAULT_OPTIONS.merge(options)
end

Public Instance Methods

all() click to toggle source
# File lib/best_seats/finder.rb, line 38
def all
  selected_seats = []

  available_seats_matrix.each do |line|
    next if insuficient_seats?(line.size)

    sorted_seats = selected_seats.sort

    return sorted_seats if all_seats_found?(sorted_seats)

    selected_seats = []

    (INITIAL_SEAT_INDEX...seats_requested).each do |seat|
      index = index_to_remove(line.size)
      value = line.delete_at(index)
      selected_seats << value
    end
  end

  selected_seats.sort
end

Private Instance Methods

all_seats_found?(selected_seats) click to toggle source
# File lib/best_seats/finder.rb, line 74
def all_seats_found?(selected_seats)
  enough_selected?(selected_seats.size) && consecutive_values?(selected_seats)
end
available_seats_matrix() click to toggle source
# File lib/best_seats/finder.rb, line 62
def available_seats_matrix
  @_available_seats_matrix ||= matrix_builder.new(
    rows,
    columns,
    seats
  ).available
end
consecutive_values?(seats) click to toggle source
# File lib/best_seats/finder.rb, line 82
def consecutive_values?(seats)
  seat_group.new(
    seats.sort
  ).consecutive_values?
end
enough_selected?(selected_count) click to toggle source
# File lib/best_seats/finder.rb, line 78
def enough_selected?(selected_count)
  selected_count == seats_requested
end
index_to_remove(collection_size) click to toggle source
# File lib/best_seats/finder.rb, line 88
def index_to_remove(collection_size)
  index_finder.new(collection_size).call
end
insuficient_seats?(total) click to toggle source
# File lib/best_seats/finder.rb, line 70
def insuficient_seats?(total)
  total < seats_requested
end
options() click to toggle source
# File lib/best_seats/finder.rb, line 96
def options
  @_options ||= OpenStruct.new(@options)
end
venue() click to toggle source
# File lib/best_seats/finder.rb, line 92
def venue
  @_venue ||= venue_builder.new(input)
end