box_packer 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6674a542f5f429666b9e47203e8ccc70dc84959c
4
- data.tar.gz: 7f85e16669af7c897a7ec754f0e1a72d05abbef5
3
+ metadata.gz: 577f9d185a95ed861e725040c6af624d2470fbea
4
+ data.tar.gz: e1959d0f2230fd26b464b5340d31cd45ea29c3b2
5
5
  SHA512:
6
- metadata.gz: fb646bc2431f217984f17203585dbe37eeaffc71e6537e223b201eba3f79dc68b084af5a812c95c508b7d931ce24c392e4c226665ae21b72ac0c82f17b95777a
7
- data.tar.gz: 4c0da12dd4b7dede43af119a0c00c291ad406ab994ae02359ee87ff303b2dfb6083679230a356c86edd0fa872d6610813f7cb3ff7bc0cb7fc5c4b78bc86bb64f
6
+ metadata.gz: 708d9ad7db2ca97d46c275e9545ff67e1aadd6788fc439dff8a78acc46d8b334a95d1c8d27a686b61357093a8a11bcf1f41b9c1bacc28ab72aa9d1933a88bcb6
7
+ data.tar.gz: 704c4c92f46af99ecf269347ecef930f41368922642c3447aed04e2dfdf0e77330a356da14bdcffd2b21eaa5cbb98bd5d985abb1e307849d7e926d3342983626
data/README.md CHANGED
@@ -30,7 +30,7 @@ BoxPacker.container [3, 6, 7] do
30
30
  add_item [1,3,5]
31
31
  add_item [4,3,5]
32
32
  add_item [3,5,5]
33
- pack! # returns 2
33
+ pack! # 2
34
34
 
35
35
  puts packed_successfully # true
36
36
  puts packings.count # 2
@@ -53,7 +53,7 @@ BoxPacker.container [15, 20, 13], label: 'Parcel', weight_limit: 50, packings_li
53
53
  add_item [2, 3, 5], label: 'Shoes', weight: 47, quantity: 2
54
54
  add_item [3, 3, 1], label: 'Watch', weight: 24
55
55
  add_item [1, 1, 4], label: 'Bag', weight: 7
56
- pack! # returns 2
56
+ pack! # 3
57
57
 
58
58
  puts self # |Container| Parcel 20x15x13 Weight Limit:50 Packings Limit:3
59
59
  # | Packing| Remaining Volume:3870 Remaining Weight:3
data/lib/box_packer.rb CHANGED
@@ -1,12 +1,2 @@
1
- require 'attire'
2
- require 'forwardable'
3
-
4
- require 'box_packer/position'
5
- require 'box_packer/dimensions'
6
- require 'box_packer/box'
7
- require 'box_packer/item'
8
- require 'box_packer/packer'
9
- require 'box_packer/packing'
10
- require 'box_packer/svg_exporter'
11
1
  require 'box_packer/container'
12
2
  require 'box_packer/builder'
@@ -1,7 +1,16 @@
1
+ require 'forwardable'
2
+ require_relative 'position'
3
+ require_relative 'dimensions'
4
+
1
5
  module BoxPacker
2
6
  class Box
3
7
  extend Forwardable
4
- attr_init :dimensions, position: Position[0, 0, 0]
8
+
9
+ def initialize(dimensions, opts = {})
10
+ @dimensions = dimensions
11
+ @position = opts[:position] || Position[0, 0, 0]
12
+ end
13
+
5
14
  def_delegators :dimensions, :volume, :each_rotation, :width, :height, :depth
6
15
  attr_accessor :dimensions, :position
7
16
 
@@ -1,3 +1,6 @@
1
+ require_relative 'container'
2
+ require_relative 'item'
3
+
1
4
  module BoxPacker
2
5
  def self.builder(&b)
3
6
  b.call(Builder.new) if block_given?
@@ -1,3 +1,10 @@
1
+ require_relative 'dimensions'
2
+ require_relative 'item'
3
+ require_relative 'packer'
4
+ require_relative 'packing'
5
+ require_relative 'svg_exporter'
6
+ require_relative 'box'
7
+
1
8
  module BoxPacker
2
9
  def self.container(*args, &b)
3
10
  Container.new(*args, &b)
@@ -1,43 +1,26 @@
1
- require 'matrix'
1
+ require_relative 'vector'
2
2
 
3
3
  module BoxPacker
4
4
  class Dimensions < Vector
5
- def +(other)
6
- Dimensions[*super]
7
- end
8
-
9
- def -(other)
10
- Dimensions[*super]
11
- end
12
-
13
- def width
14
- Dimensions[self[0], 0, 0]
15
- end
16
-
17
- def height
18
- Dimensions[0, self[1], 0]
19
- end
20
-
21
- def depth
22
- Dimensions[0, 0, self[2]]
23
- end
24
-
25
5
  def volume
26
- @volume ||= self[0] * self[1] * self[2]
6
+ @volume ||= to_a.reduce(&:*)
27
7
  end
28
8
 
29
9
  def >=(other)
30
- map2(other) { |v1, v2| v1 >= v2 }.reduce(&:&)
10
+ zip_map(other, :>=).reduce(&:&)
31
11
  end
32
12
 
33
13
  def each_rotation
34
- to_a.permutation.each do |perm|
35
- yield Dimensions[*perm]
36
- end
14
+ yield Dimensions[x, y, z]
15
+ yield Dimensions[x, z, y]
16
+ yield Dimensions[z, x, y]
17
+ yield Dimensions[z, y, x]
18
+ yield Dimensions[y, x, z]
19
+ yield Dimensions[y, z, x]
37
20
  end
38
21
 
39
22
  def to_s
40
- "#{self[0]}x#{self[1]}x#{self[2]}"
23
+ to_a.join('x')
41
24
  end
42
25
  end
43
26
  end
@@ -1,3 +1,6 @@
1
+ require_relative 'box'
2
+ require_relative 'dimensions'
3
+
1
4
  module BoxPacker
2
5
  class Item < Box
3
6
  attr_accessor :label, :weight
@@ -1,7 +1,15 @@
1
1
  module BoxPacker
2
2
  class Packer
3
3
  extend Forwardable
4
- attr_method :pack, :container
4
+
5
+ def self.pack(container)
6
+ new(container).pack
7
+ end
8
+
9
+ def initialize(container)
10
+ @container = container
11
+ end
12
+
5
13
  def_delegators :container, :new_packing!, :packings_limit, :packings, :packing
6
14
 
7
15
  def pack
@@ -17,6 +25,8 @@ module BoxPacker
17
25
 
18
26
  private
19
27
 
28
+ attr_reader :container
29
+
20
30
  def too_many_packings?
21
31
  packings.count >= packings_limit if packings_limit
22
32
  end
@@ -1,9 +1,9 @@
1
- require 'matrix'
1
+ require_relative 'vector'
2
2
 
3
3
  module BoxPacker
4
4
  class Position < Vector
5
5
  def to_s
6
- "(#{self[0]},#{self[1]},#{self[2]})"
6
+ "(#{to_a.join(',')})"
7
7
  end
8
8
  end
9
9
  end
@@ -53,7 +53,6 @@ module BoxPacker
53
53
 
54
54
  class Face
55
55
  attr_reader :width, :height, :axes
56
- attr_query :front?
57
56
 
58
57
  def self.reset(margin, scale, container_dimensions)
59
58
  @@coords_mapping = [0, 1, 2]
@@ -65,7 +64,7 @@ module BoxPacker
65
64
  end
66
65
 
67
66
  def iterate_class_variables
68
- if front?
67
+ if front
69
68
  @@axes[0] = width + @@margin * 2
70
69
  else
71
70
  @@coords_mapping.rotate!
@@ -110,7 +109,7 @@ module BoxPacker
110
109
 
111
110
  def sorted_items(packing)
112
111
  items = packing.sort_by { |i| i.position[k] }
113
- items.reverse! unless front?
112
+ items.reverse! unless front
114
113
  items
115
114
  end
116
115
  end
@@ -0,0 +1,51 @@
1
+ module BoxPacker
2
+ class Vector
3
+ def self.[](*args)
4
+ new(*args)
5
+ end
6
+
7
+ def initialize(x, y, z)
8
+ @x, @y, @z = x, y, z
9
+ end
10
+
11
+ attr_reader :x, :y, :z
12
+
13
+ def +(other)
14
+ self.class.new(*zip_map(other, :+))
15
+ end
16
+
17
+ def -(other)
18
+ self.class.new(*zip_map(other, :-))
19
+ end
20
+
21
+ def width
22
+ self.class.new(x, 0, 0)
23
+ end
24
+
25
+ def height
26
+ self.class.new(0, y, 0)
27
+ end
28
+
29
+ def depth
30
+ self.class.new(0, 0, z)
31
+ end
32
+
33
+ def to_a
34
+ [x, y, z]
35
+ end
36
+
37
+ def ==(other)
38
+ zip_map(other, :==).reduce(&:&)
39
+ end
40
+
41
+ def eql?(other)
42
+ zip_map(other, :eql?).reduce(&:&)
43
+ end
44
+
45
+ private
46
+
47
+ def zip_map(other, method)
48
+ to_a.zip(other.to_a).map { |a, b| a.send(method, b) }
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,35 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: box_packer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max White
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-27 00:00:00.000000000 Z
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: attire
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.0'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 0.0.3
23
- type: :runtime
24
- prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '0.0'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 0.0.3
33
13
  - !ruby/object:Gem::Dependency
34
14
  name: rasem
35
15
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +62,7 @@ files:
82
62
  - lib/box_packer/packing.rb
83
63
  - lib/box_packer/position.rb
84
64
  - lib/box_packer/svg_exporter.rb
65
+ - lib/box_packer/vector.rb
85
66
  homepage: https://github.com/mushishi78/box_packer
86
67
  licenses:
87
68
  - MIT