boxxer 0.1.4 → 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: c3e8f33230d128b7c587948a392f750872681530e3780b549ed0fc6c048bde9f
4
- data.tar.gz: 344d927c6b5c36317792de56f4d84b4d52e2a7929ee89a1030500218ee0bb942
3
+ metadata.gz: d7737f168e9e937115ec24749076825f5fc4f887d114622e39587da103c6c9b0
4
+ data.tar.gz: ba7ee022f7852be525ef5c43ca25adfd8ec80d9a47db25da038f36fe0894ef2e
5
5
  SHA512:
6
- metadata.gz: 10644a7dfaeebaac09b8f5c77eeb6942dae9092e1ed68cb7aec8229b597ebde03396ae9f104a484e4f799b8c0b0021f1f4a11229aa29989b89e43bce88b65f8d
7
- data.tar.gz: b6b58d4846a5ea0f833601ecbbe20fc054e2467e1a3bb1e7fd95ac4eb540526a1fc9db12c5da32667f630a287d6063230a58850c0a6e63b316e7d32b85351ca7
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.4)
4
+ boxxer (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -10,15 +10,13 @@ irb
10
10
  ```
11
11
 
12
12
  ```
13
+ require 'securerandom'
13
14
  require 'boxxer'
14
- weights = []
15
- 20.times { weights << rand(0.5..1).round(3) }
15
+ contents = 20.times.with_object([]) { |_, obj| obj.push(item: SecureRandom.alphanumeric(10), weight: rand(0.5..1).round(3)) }
16
16
  containers = [{ length: 47, width: 38, height: 10, tare_weight: 0.019, net_limit: 0.481 },
17
17
  { length: 40, width: 28, height: 13, tare_weight: 0.34, net_limit: 0.66 },
18
18
  { length: 40, width: 28, height: 80, tare_weight: 0.1, net_limit: 0.7 },
19
19
  { length: 40, width: 28, height: 18, tare_weight: 0.5, net_limit: 1.5 },
20
20
  { length: 40, width: 28, height: 20, tare_weight: 0.52, net_limit: 2.48 }]
21
- arranger = Boxxer::Arranger.new(containers: containers, weights: weights)
22
- arranger.call
23
- arranger.containers
21
+ boxxer = Boxxer.call(containers: containers, contents: contents)
24
22
  ```
@@ -1,7 +1,7 @@
1
1
  module Boxxer
2
2
  class Container
3
- attr_reader :tare_weight, :net_weight, :gross_weight, :width, :height, :length
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,11 +9,11 @@ module Boxxer
9
9
  @length = length
10
10
  @net_limit = net_limit
11
11
  @tare_weight = tare_weight
12
- @weights = []
12
+ @contents = []
13
13
  end
14
14
 
15
15
  def net_weight
16
- (@weights.sum).truncate(3)
16
+ @contents.sum { |content| content[:weight] }.truncate(3)
17
17
  end
18
18
 
19
19
  def gross_weight
@@ -24,8 +24,8 @@ module Boxxer
24
24
  @net_limit >= net_weight + weight
25
25
  end
26
26
 
27
- def add_weight(weight)
28
- @weights.push(weight)
27
+ def add_content(content)
28
+ @contents.push(content)
29
29
  end
30
30
  end
31
31
  end
@@ -4,16 +4,17 @@ module Boxxer
4
4
  class Handler
5
5
  attr_reader :containers
6
6
 
7
- def initialize(containers:, weights:)
7
+ def initialize(containers:, contents:)
8
8
  @available_containers = containers.sort { |container| container[:net_limit] }.reverse
9
9
  @largest_container = @available_containers.last
10
- @weights = weights.sort.reverse
10
+ @contents = contents.sort_by { |content| content[:weight] }.reverse
11
11
  @containers = []
12
12
  end
13
13
 
14
14
  def call
15
- raise Error, "Largest container net_limit is #{@largest_container[:net_limit]}, but maximum weight is #{@weights.max}" if invalid_options?
16
- until @weights.empty? do
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
17
18
  container = Container.new(matching_container)
18
19
  complete_container(container)
19
20
  @containers << container
@@ -37,17 +38,21 @@ module Boxxer
37
38
 
38
39
  def complete_container(container)
39
40
  loop do
40
- fittable_weight = @weights.find { |weight| container.fittable?(weight) }
41
- fittable_weight.nil? ? break : container.add_weight(@weights.delete_at(@weights.index(fittable_weight)))
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)))
42
43
  end
43
44
  end
44
45
 
46
+ def content_max_weight
47
+ @contents.max { |c| c[:weight] }
48
+ end
49
+
45
50
  def invalid_options?
46
- @weights.max > @largest_container[:net_limit]
51
+ content_max_weight[:weight] > @largest_container[:net_limit]
47
52
  end
48
53
 
49
54
  def matching_container
50
- @available_containers.find { |container| container[:net_limit] >= @weights.sum } || @largest_container
55
+ @available_containers.find { |container| container[:net_limit] >= @contents.sum { |content| content[:weight] } } || @largest_container
51
56
  end
52
57
  end
53
58
  end
@@ -1,3 +1,3 @@
1
1
  module Boxxer
2
- VERSION = '0.1.4'
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.4
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-17 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