order_optimizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0845a565bc344866e5e3d4725d28b890c9f3e4d57de36ee2789ee331f000168
4
+ data.tar.gz: b825661a3746a8b7bf574c3938efb2b412ac81163223a4836be013dda983100e
5
+ SHA512:
6
+ metadata.gz: 3103e356f667e9256ff3115d0381f05fa27906083232300c51833fcd5b15118fd4b4154542f07e781882a25a4de8f70b4604c8748c529f92a91d41af315391c4
7
+ data.tar.gz: 965406b44c6006b4ac863cc2dd3d8fc0872fd87a08bb4fadc45954cd81e6a724f7143975985c439a83a4a9a9d0630df54113a2e9e018cd16282ea420eff23315
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # OrderOptimizer Gem Changelog
2
+
3
+
4
+ ## 0.1.0 First release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at op@crispymtn.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in order_optimizer.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ order_optimizer (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.13.0)
10
+ rake (13.0.1)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler
17
+ minitest
18
+ order_optimizer!
19
+ rake
20
+
21
+ BUNDLED WITH
22
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Crispy Mountain GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # OrderOptimizer
2
+
3
+ The `OrderOptimizer` gem helps to optimize orders when the goods are offered in different pack sizes and in different discount levels.
4
+
5
+ Imagine a product can be ordered in different pack sizes and each pack size has a different price per unit. For example like this:
6
+
7
+ | Pack size | Price per Unit |
8
+ |-----------|----------------|
9
+ | 1000 | 3.90 |
10
+ | 100 | 4.00 |
11
+ | 10 | 6.00 |
12
+ | 1 | 9.00 |
13
+
14
+ In this example, it is quite obvious that it is cheaper buying one 10-pack instead of nine 1-packs when you need 9 units (because the 10-pack costs 60.00 but nine 1-packs would cost 81.00).
15
+
16
+ But what would be the cheapest combination when you need 946 units? Or 947?
17
+
18
+ The `OrderOptimizer` answers this kind of question in a performant way.
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'order_optimizer'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install order_optimizer
35
+
36
+ ## Usage
37
+
38
+ ```ruby
39
+ # Initialize the optimizer with a catalog
40
+ order_optimizer = OrderOptimizer.new(
41
+ '1-pack' => { quantity: 1, price_per_unit: 9 },
42
+ '10-pack' => { quantity: 10, price_per_unit: 6 },
43
+ '100-pack' => { quantity: 100, price_per_unit: 4 },
44
+ '1000-pack' => { quantity: 1000, price_per_unit: 3.9 }
45
+ )
46
+
47
+ # Pick the cheapers possible order for the required quantity
48
+ cheapest_order_for_946_units = order_optimizer.cheapest_order(required_qty: 946)
49
+ cheapest_order_for_946_units.quantity # => 946
50
+ cheapest_order_for_946_units.total # => 3894.00
51
+ cheapest_order_for_946_units.skus # => { '100-pack' => 9, '10-pack' => 4, '1-pack' => 6 }
52
+
53
+ cheapest_order_for_947_units = order_optimizer.cheapest_order(required_qty: 947)
54
+ cheapest_order_for_947_units.quantity # => 1_000
55
+ cheapest_order_for_947_units.total # => 3_900.00
56
+ cheapest_order_for_947_units.skus # => { '1000-pack' => 1 }
57
+ ```
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/crispymtn/order_optimizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
72
+
73
+ ## Code of Conduct
74
+
75
+ Everyone interacting in the OrderOptimizer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/crispymtn/order_optimizer/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "order_optimizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ require 'order_optimizer/sku'
2
+
3
+ class OrderOptimizer
4
+ class Catalog
5
+ def initialize(skus)
6
+ @skus = skus.map { |id, values| Sku.new(id, **values) }.sort_by(&:price_per_unit)
7
+ end
8
+
9
+ def skus
10
+ @skus.dup
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ class OrderOptimizer
2
+ class Order
3
+ attr_reader :quantity, :total, :skus
4
+
5
+ def initialize
6
+ @quantity = 0
7
+ @total = 0
8
+ @skus = {}
9
+ end
10
+
11
+ def add(sku, count: 1)
12
+ @quantity += count * sku.quantity
13
+ @total += count * sku.price_per_sku
14
+ @skus = skus.merge(sku.id => count) { |identifier, current, plus| current + plus }
15
+ self
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ class OrderOptimizer
2
+ class Sku
3
+ attr_reader :id, :quantity, :price_per_unit, :price_per_sku
4
+
5
+ def initialize(id, quantity:, price_per_unit:)
6
+ @id = id
7
+ @quantity = quantity
8
+ @price_per_unit = BigDecimal(price_per_unit, 2)
9
+ @price_per_sku = quantity * price_per_unit
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class OrderOptimizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'bigdecimal'
2
+
3
+ require "order_optimizer/catalog"
4
+ require "order_optimizer/order"
5
+ require "order_optimizer/version"
6
+
7
+ class OrderOptimizer
8
+ def initialize(skus)
9
+ @catalog = OrderOptimizer::Catalog.new(skus)
10
+ end
11
+
12
+ def cheapest_order(required_qty:)
13
+ possible_orders(required_qty: required_qty)
14
+ .min_by(&:total)
15
+ end
16
+
17
+ private
18
+
19
+ def possible_orders(required_qty:)
20
+ [].tap do |orders|
21
+ skus = @catalog.skus
22
+ order = OrderOptimizer::Order.new
23
+ while sku = skus.shift
24
+ count, remainder = (required_qty - order.quantity).divmod(sku.quantity)
25
+ order.add(sku, count: count) unless count.zero?
26
+ orders << (remainder.zero? ? order.dup : order.dup.add(sku))
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "order_optimizer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "order_optimizer"
7
+ spec.version = OrderOptimizer::VERSION
8
+
9
+ spec.authors = ["crispymtn", "Martin Spickermann"]
10
+ spec.email = ["op@crispymtn.com", "spickermann@gmail.com"]
11
+ spec.homepage = "https://github.com/crispymtn/order_optimizer"
12
+ spec.license = "MIT"
13
+ spec.summary = "Helps to optimize orders if the goods are offered in different pack sizes and in different discount levels."
14
+
15
+
16
+ spec.metadata["changelog_uri"] = "https://github.com/crispymtn/order_optimizer/blob/master/CHANGELOG.md"
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler"
28
+ spec.add_development_dependency "minitest"
29
+ spec.add_development_dependency "rake"
30
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: order_optimizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - crispymtn
8
+ - Martin Spickermann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description:
57
+ email:
58
+ - op@crispymtn.com
59
+ - spickermann@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - CHANGELOG.md
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/order_optimizer.rb
76
+ - lib/order_optimizer/catalog.rb
77
+ - lib/order_optimizer/order.rb
78
+ - lib/order_optimizer/sku.rb
79
+ - lib/order_optimizer/version.rb
80
+ - order_optimizer.gemspec
81
+ homepage: https://github.com/crispymtn/order_optimizer
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ changelog_uri: https://github.com/crispymtn/order_optimizer/blob/master/CHANGELOG.md
86
+ homepage_uri: https://github.com/crispymtn/order_optimizer
87
+ source_code_uri: https://github.com/crispymtn/order_optimizer
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.0.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Helps to optimize orders if the goods are offered in different pack sizes
107
+ and in different discount levels.
108
+ test_files: []