Class: SeatSelector::Finder
- Inherits:
-
Object
- Object
- SeatSelector::Finder
- Defined in:
- lib/seat_selector/finder.rb
Instance Attribute Summary collapse
-
#seats ⇒ Object
readonly
Returns the value of attribute seats.
Instance Method Summary collapse
- #get_best_seats(seats_needed = 1) ⇒ Object
-
#initialize(venue) ⇒ Finder
constructor
A new instance of Finder.
Constructor Details
#initialize(venue) ⇒ Finder
Returns a new instance of Finder.
5 6 7 8 |
# File 'lib/seat_selector/finder.rb', line 5 def initialize(venue) @seats = venue.available_seats set_distance!(venue.total_columns) end |
Instance Attribute Details
#seats ⇒ Object (readonly)
Returns the value of attribute seats.
3 4 5 |
# File 'lib/seat_selector/finder.rb', line 3 def seats @seats end |
Instance Method Details
#get_best_seats(seats_needed = 1) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/seat_selector/finder.rb', line 10 def get_best_seats(seats_needed = 1) return [] if seats_needed < 1 best_seat = nil best_distance = nil @seats.each do |r, seats_in_row| # skip rows with no open seats # search the current row seats_in_row.each do |c, seat| if seats_needed == 1 if best_seat.nil? || best_seat.distance > seat.distance best_seat = seat best_distance = seat.distance end else # check rightward from current seat valid_group = (c + 1).upto(c + seats_needed - 1).all?{|i| seats_in_row.key?(i) } if valid_group distance = (c).upto(c + seats_needed - 1).inject(0) do |sum, i| sum + seats_in_row[i].distance end if best_seat.nil? || best_distance > distance best_seat = seat best_distance = distance end end end end end return [] if best_seat.nil? if seats_needed > 1 seats_needed.times.map {|i| @seats[best_seat.row][best_seat.column + i] } else [best_seat] end end |