boxxer 0.1.0 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 704b41b95d02827f07d78de7ce6a5937bd99b4fa2cffdf3c163222ab7f5c230c
4
- data.tar.gz: 91d5b7481ff8b9b1240811c31fa645ba1e841f99389f456d2092f20f44d2bae1
3
+ metadata.gz: d7737f168e9e937115ec24749076825f5fc4f887d114622e39587da103c6c9b0
4
+ data.tar.gz: ba7ee022f7852be525ef5c43ca25adfd8ec80d9a47db25da038f36fe0894ef2e
5
5
  SHA512:
6
- metadata.gz: ff6ee3c75dbffd1574ece0d78236d6832627342bdbd2790a4957e81abf2fadb0d886ce637aa339716037d67aea15dba1a1354f24572be6aab51d02f1542a3586
7
- data.tar.gz: 37edf969362276facce8ad5d4918524c4d0549c33a43b0fece0b0319d7ad7ffc972f79c493b7d2d28a8e52334815937b8774c8d8d785e7ec226fd4e50b9d981f
6
+ metadata.gz: 0d13897fd62c410b65d5101bc280c5ce2ffb8070015c22296bb5794d19bbc15a397695855acca9e352f9550588ff915a00f3fc6c84384ec583f112f620e65033
7
+ data.tar.gz: 02a5c9778d2a6a34fe6fff2c8c3dfa226e795008d674bc19633e5fc0a515dcb0cd8cbca6d70732bddeebacb3c42b9627c1e635cd5b42b531b3bd29254a6dc194
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boxxer (0.1.0)
4
+ boxxer (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -5,14 +5,18 @@ This gem intended to pick containers(boxes, packages, cartons), based on item we
5
5
  ## Usage
6
6
 
7
7
  ```
8
- weights = []
9
- 20.times { weights << rand(0.5..1).round(3) }
8
+ gem install boxxer
9
+ irb
10
+ ```
11
+
12
+ ```
13
+ require 'securerandom'
14
+ require 'boxxer'
15
+ contents = 20.times.with_object([]) { |_, obj| obj.push(item: SecureRandom.alphanumeric(10), weight: rand(0.5..1).round(3)) }
10
16
  containers = [{ length: 47, width: 38, height: 10, tare_weight: 0.019, net_limit: 0.481 },
11
17
  { length: 40, width: 28, height: 13, tare_weight: 0.34, net_limit: 0.66 },
12
18
  { length: 40, width: 28, height: 80, tare_weight: 0.1, net_limit: 0.7 },
13
19
  { length: 40, width: 28, height: 18, tare_weight: 0.5, net_limit: 1.5 },
14
20
  { length: 40, width: 28, height: 20, tare_weight: 0.52, net_limit: 2.48 }]
15
- arranger = Boxxer::Arranger.new(containers: containers, weights: weights)
16
- arranger.call
17
- arranger.containers
21
+ boxxer = Boxxer.call(containers: containers, contents: contents)
18
22
  ```
@@ -1,47 +1,10 @@
1
- require 'boxxer/container'
1
+ require 'boxxer/handler'
2
2
  require 'boxxer/version'
3
3
 
4
4
  module Boxxer
5
5
  class Error < StandardError; end
6
6
 
7
- class Arranger
8
- attr_reader :containers, :container_count
9
-
10
- def initialize(containers:, weights:)
11
- @available_containers = containers.sort { |container| container[:net_limit] }.reverse
12
- @largest_container = @available_containers.last
13
- @weights = weights.sort.reverse
14
- @containers = []
15
- end
16
-
17
- def call
18
- raise Error, "Largest container net_limit is #{@largest_container[:net_limit]}, but maximum weight is #{@weights.max}" if invalid_options?
19
- until @weights.empty? do
20
- container = Container.new(matching_container)
21
- complete_container(container)
22
- @containers << container
23
- end
24
- end
25
-
26
- private
27
-
28
- def container_count
29
- @containers.count
30
- end
31
-
32
- def complete_container(container)
33
- loop do
34
- fittable_weight = @weights.find { |weight| container.fittable?(weight) }
35
- fittable_weight.nil? ? break : container.add_weight(@weights.delete_at(@weights.index(fittable_weight)))
36
- end
37
- end
38
-
39
- def invalid_options?
40
- @weights.max > @largest_container[:net_limit]
41
- end
42
-
43
- def matching_container
44
- @available_containers.find { |container| container[:net_limit] >= @weights.sum } || @largest_container
45
- end
7
+ def self.call(options = {})
8
+ Handler.new(options).call
46
9
  end
47
10
  end
@@ -1,7 +1,7 @@
1
1
  module Boxxer
2
2
  class Container
3
- attr_reader :dimensions, :tare_weight, :net_weight, :gross_weight
4
- attr_writer :weights
3
+ attr_reader :tare_weight, :width, :height, :length
4
+ attr_writer :contents
5
5
 
6
6
  def initialize(width:, height:, length:, tare_weight:, net_limit:)
7
7
  @width = width
@@ -9,15 +9,11 @@ module Boxxer
9
9
  @length = length
10
10
  @net_limit = net_limit
11
11
  @tare_weight = tare_weight
12
- @weights = []
13
- end
14
-
15
- def dimensions
16
- { width: width, height: height, length: length }
12
+ @contents = []
17
13
  end
18
14
 
19
15
  def net_weight
20
- (@weights.sum).truncate(3)
16
+ @contents.sum { |content| content[:weight] }.truncate(3)
21
17
  end
22
18
 
23
19
  def gross_weight
@@ -28,8 +24,8 @@ module Boxxer
28
24
  @net_limit >= net_weight + weight
29
25
  end
30
26
 
31
- def add_weight(weight)
32
- @weights.push(weight)
27
+ def add_content(content)
28
+ @contents.push(content)
33
29
  end
34
30
  end
35
31
  end
@@ -0,0 +1,58 @@
1
+ require 'boxxer/container'
2
+
3
+ module Boxxer
4
+ class Handler
5
+ attr_reader :containers
6
+
7
+ def initialize(containers:, contents:)
8
+ @available_containers = containers.sort { |container| container[:net_limit] }.reverse
9
+ @largest_container = @available_containers.last
10
+ @contents = contents.sort_by { |content| content[:weight] }.reverse
11
+ @containers = []
12
+ end
13
+
14
+ def call
15
+ raise Error, "Largest container net_limit is #{@largest_container[:net_limit]}, but maximum weight is #{content_max_weight}" if invalid_options?
16
+
17
+ until @contents.empty? do
18
+ container = Container.new(matching_container)
19
+ complete_container(container)
20
+ @containers << container
21
+ end
22
+ self
23
+ end
24
+
25
+ def total_gross_weight
26
+ @containers.sum(&:gross_weight).truncate(3)
27
+ end
28
+
29
+ def total_net_weight
30
+ @containers.sum(&:net_weight).truncate(3)
31
+ end
32
+
33
+ def container_count
34
+ @containers.count
35
+ end
36
+
37
+ private
38
+
39
+ def complete_container(container)
40
+ loop do
41
+ fittable_conteiner = @contents.find { |content| container.fittable?(content[:weight]) }
42
+ fittable_conteiner.nil? ? break : container.add_content(@contents.delete_at(@contents.index(fittable_conteiner)))
43
+ end
44
+ end
45
+
46
+ def content_max_weight
47
+ @contents.max { |c| c[:weight] }
48
+ end
49
+
50
+ def invalid_options?
51
+ content_max_weight[:weight] > @largest_container[:net_limit]
52
+ end
53
+
54
+ def matching_container
55
+ @available_containers.find { |container| container[:net_limit] >= @contents.sum { |content| content[:weight] } } || @largest_container
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Boxxer
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxxer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Sivoglaz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-07 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -101,6 +101,7 @@ files:
101
101
  - boxxer.gemspec
102
102
  - lib/boxxer.rb
103
103
  - lib/boxxer/container.rb
104
+ - lib/boxxer/handler.rb
104
105
  - lib/boxxer/version.rb
105
106
  homepage:
106
107
  licenses:
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  - !ruby/object:Gem::Version
122
123
  version: '0'
123
124
  requirements: []
124
- rubygems_version: 3.0.6
125
+ rubygems_version: 3.0.8
125
126
  signing_key:
126
127
  specification_version: 4
127
128
  summary: Picking containers, boxes, cartons, packages based on items weight