Class: SpaceCalculator::LocationPacker

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/space_calculator.rb

Defined Under Namespace

Classes: Container, Pile

Instance Method Summary collapse

Constructor Details

#initialize(dimensions) ⇒ LocationPacker

Returns a new instance of LocationPacker.



226
227
228
229
# File 'backend/app/model/space_calculator.rb', line 226

def initialize(dimensions)
  @dimensions = dimensions
  @piles = []
end

Instance Method Details

#add_container(name, dimensions, max_tower_count) ⇒ Object

Some types of containers can’t be stacked on top of each other. The max_tower_count parameter lets you specify the maximum number of levels a container can be stacked on top of each other.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'backend/app/model/space_calculator.rb', line 235

def add_container(name, dimensions, max_tower_count)
  container = Container.new(name, dimensions, (max_tower_count || :unlimited))

  pile = find_pile_for(container)

  if pile
    pile.add(container)
  else
    pile = Pile.new(@dimensions)
    pile.add(container)
    if pile_fits?(pile)
      @piles << pile
    else
      return false
    end
  end

  true
end

#find_pile_for(container) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'backend/app/model/space_calculator.rb', line 256

def find_pile_for(container)
  @piles.each do |pile|
    if pile.will_fit(container)
      return pile
    end
  end

  false
end

#pile_fits?(pile) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
273
274
275
# File 'backend/app/model/space_calculator.rb', line 267

def pile_fits?(pile)
  return false if pile.height > @dimensions.height || pile.depth > @dimensions.depth
  piles_width = 0
  @piles.each do |pile|
    piles_width += pile.width
  end

  (piles_width + pile.width) <= @dimensions.width
end