From 5527c7be1170dcbde70266bbbac09b77ebf6b804 Mon Sep 17 00:00:00 2001 From: tkfridell <52042136+tkfridell@users.noreply.github.com> Date: Thu, 20 Jun 2019 16:49:45 +0200 Subject: [PATCH 1/5] Add files via upload The purpose of this change is to allow inclusion of custom attributes for items to be retained in final packing results. Purposes include identifying packed items, additional calculation etc. --- easy-box-packer.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/easy-box-packer.rb b/easy-box-packer.rb index 6c1870e..5aba23d 100644 --- a/easy-box-packer.rb +++ b/easy-box-packer.rb @@ -183,11 +183,14 @@ def place(item, space) # select rotation with smallest margin final = possible_rotations_and_margins.sort_by { |a| a[:margin].sort }.first return unless final - return { + placement = { dimensions: final[:rotation], position: space[:position], weight: item[:weight].to_f } + # retain optional attributes, i.e. item identifiers, from item to placement + item.each { |key, value| placement[key] ||= value } + return placement end def break_up_space(space, placement) From ff7eb36e3d90f33010ba226b41cb250b0f66051c Mon Sep 17 00:00:00 2001 From: tkfridell <52042136+tkfridell@users.noreply.github.com> Date: Thu, 20 Jun 2019 19:16:42 +0200 Subject: [PATCH 2/5] Changed error return type from string to hash Enables easier handling when errors occur. I.e. the properties of the item related to the error can be more easily inspected in order to take appropriate action. --- easy-box-packer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/easy-box-packer.rb b/easy-box-packer.rb index 5aba23d..251229e 100644 --- a/easy-box-packer.rb +++ b/easy-box-packer.rb @@ -7,7 +7,7 @@ def pack(container:, items:) items.sort_by { |h| h[:dimensions].sort.reverse }.reverse.each do |item| # If the item is just too big for the container lets give up on this if item[:weight].to_f > container[:weight_limit].to_f - errors << "Item: #{item} is too heavy for container" + errors << { item: item, reason: 'too heavy for container' } next end @@ -49,7 +49,7 @@ def pack(container:, items:) # If it can't be placed in this space, then it's just # too big for the container and we should abandon hope unless placement - errors << "Item: #{item} cannot be placed in container" + errors << { item: item, reason: 'cannot be placed in container' } next end # Otherwise lets put the item in a new packing From 29cabefa1f4a609cdb4a6927b58f7d65a8bfd2be Mon Sep 17 00:00:00 2001 From: tkfridell <52042136+tkfridell@users.noreply.github.com> Date: Thu, 20 Jun 2019 19:19:50 +0200 Subject: [PATCH 3/5] Removed left overcode from separate branch --- easy-box-packer.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/easy-box-packer.rb b/easy-box-packer.rb index 251229e..b630c68 100644 --- a/easy-box-packer.rb +++ b/easy-box-packer.rb @@ -183,14 +183,11 @@ def place(item, space) # select rotation with smallest margin final = possible_rotations_and_margins.sort_by { |a| a[:margin].sort }.first return unless final - placement = { + return { dimensions: final[:rotation], position: space[:position], weight: item[:weight].to_f } - # retain optional attributes, i.e. item identifiers, from item to placement - item.each { |key, value| placement[key] ||= value } - return placement end def break_up_space(space, placement) From cfc805b5c1c8371e4ff56340bab630b099c24781 Mon Sep 17 00:00:00 2001 From: tkfridell <52042136+tkfridell@users.noreply.github.com> Date: Thu, 20 Jun 2019 19:29:11 +0200 Subject: [PATCH 4/5] Reinstated code accidently overwritten --- easy-box-packer.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/easy-box-packer.rb b/easy-box-packer.rb index b630c68..251229e 100644 --- a/easy-box-packer.rb +++ b/easy-box-packer.rb @@ -183,11 +183,14 @@ def place(item, space) # select rotation with smallest margin final = possible_rotations_and_margins.sort_by { |a| a[:margin].sort }.first return unless final - return { + placement = { dimensions: final[:rotation], position: space[:position], weight: item[:weight].to_f } + # retain optional attributes, i.e. item identifiers, from item to placement + item.each { |key, value| placement[key] ||= value } + return placement end def break_up_space(space, placement) From 46faa4d88092ffca013858b330250d5b759cb762 Mon Sep 17 00:00:00 2001 From: tkfridell <52042136+tkfridell@users.noreply.github.com> Date: Thu, 20 Jun 2019 19:46:06 +0200 Subject: [PATCH 5/5] now passing validations --- easy-box-packer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easy-box-packer_spec.rb b/easy-box-packer_spec.rb index 4236e40..ede8014 100644 --- a/easy-box-packer_spec.rb +++ b/easy-box-packer_spec.rb @@ -106,7 +106,7 @@ ] ) expect(packings[:packings].length).to eql(1) - expect(packings[:errors]).to eql(["Item: {:dimensions=>[40, 10, 34]} cannot be placed in container"]) + expect(packings[:errors]).to eql([{:item =>{:dimensions=>[40, 10, 34]},:reason=>'cannot be placed in container'}]) end it 'case 5' do