wunder 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: 3e8f6b22d9b6d0c9044da576131e9d7f3bd3313f695873fe451789458ca0564c
4
+ data.tar.gz: be323501c72c54d920bbd96132eb2da25cb3945f708826b029164647d1a3b858
5
+ SHA512:
6
+ metadata.gz: a0aa9d6fc612de24191aff79c8315fdfa2dcdf0c0e0212de0aee34bafcb3e63630116a794ad90584ffb4f7ee1b1717a2de8b9736d42d92ecc3cbd19cce6f18f0
7
+ data.tar.gz: 59ab31d94eb31ed66352e5ad23f4d4b44ec4111b7a9a80e569a3985b02ba0f01bbd8128cef556f93636d0ae6ebaf96f9df08369c21838c0c93c4c16081d8f94d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ inherit_gem:
2
+ sub-inspector:
3
+ - default.yml
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -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 akhileshsharma.aki@gmail.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,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in checkout.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wunder (0.1.0)
5
+ money (~> 6.11)
6
+ terminal-table (~> 1.8)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ bundler-audit (0.6.0)
13
+ bundler (~> 1.2)
14
+ thor (~> 0.18)
15
+ coderay (1.1.2)
16
+ concurrent-ruby (1.0.5)
17
+ diff-lcs (1.3)
18
+ faker (1.8.7)
19
+ i18n (>= 0.7)
20
+ i18n (1.0.1)
21
+ concurrent-ruby (~> 1.0)
22
+ jaro_winkler (1.5.1)
23
+ method_source (0.9.0)
24
+ money (6.11.3)
25
+ i18n (>= 0.6.4, < 1.1)
26
+ parallel (1.12.1)
27
+ parser (2.5.1.0)
28
+ ast (~> 2.4.0)
29
+ powerpack (0.1.2)
30
+ pry (0.11.3)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
33
+ rainbow (3.0.0)
34
+ rake (10.5.0)
35
+ rspec (3.7.0)
36
+ rspec-core (~> 3.7.0)
37
+ rspec-expectations (~> 3.7.0)
38
+ rspec-mocks (~> 3.7.0)
39
+ rspec-core (3.7.1)
40
+ rspec-support (~> 3.7.0)
41
+ rspec-expectations (3.7.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.7.0)
44
+ rspec-mocks (3.7.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.7.0)
47
+ rspec-support (3.7.1)
48
+ rubocop (0.57.2)
49
+ jaro_winkler (~> 1.5.1)
50
+ parallel (~> 1.10)
51
+ parser (>= 2.5)
52
+ powerpack (~> 0.1)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (~> 1.0, >= 1.0.1)
56
+ rubocop-rspec (1.25.1)
57
+ rubocop (>= 0.53.0)
58
+ ruby-progressbar (1.9.0)
59
+ sub-inspector (0.1.5)
60
+ rubocop (~> 0.55)
61
+ rubocop-rspec (~> 1.25.1)
62
+ terminal-table (1.8.0)
63
+ unicode-display_width (~> 1.1, >= 1.1.1)
64
+ thor (0.20.0)
65
+ unicode-display_width (1.4.0)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ bundler (~> 1.16)
72
+ bundler-audit (~> 0.6)
73
+ faker (~> 1.8)
74
+ pry (~> 0.11)
75
+ rake (~> 10.0)
76
+ rspec (~> 3.0)
77
+ rubocop (~> 0.57)
78
+ sub-inspector (= 0.1.5)
79
+ wunder!
80
+
81
+ BUNDLED WITH
82
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Akhilesh
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,46 @@
1
+ [![Build Status](https://travis-ci.org/basicsaki/wunder.svg?branch=master)](https://travis-ci.org/basicsaki/wunder)
2
+
3
+
4
+ # Checkout
5
+
6
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/checkout`. To experiment with that code, run `bin/console` for an interactive prompt.
7
+
8
+ TODO: Delete this and the text above, and describe your gem
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'checkout'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install checkout
25
+
26
+ ## Usage
27
+
28
+ TODO: Write usage instructions here
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ 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).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/checkout. 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.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the Checkout project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/checkout/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'checkout'
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
data/lib/interface.rb ADDED
@@ -0,0 +1,41 @@
1
+ require_relative "./wunder.rb"
2
+ # initiate parsing csv file
3
+
4
+ # Parser
5
+ # new expects csv file path,validate {true or false},use mock file true or false
6
+ parser = Parser.new("", true, true)
7
+ parser.process_file
8
+
9
+ # Product Collection
10
+ products = parser.products
11
+
12
+ # promotion
13
+ promotion = Promotion.new("new_year", "1234")
14
+
15
+ # rules
16
+ quantity_rule_1 = Promotional::Rule::QuantityPrice.new(1, 10)
17
+ quantity_rule_2 = Promotional::Rule::QuantityPrice.new(1, 10)
18
+
19
+ # promotional rules
20
+ label_1 = "Flat_rate discount on prices"
21
+ label_2 = "percentage discount on prices"
22
+
23
+ promotion_rule_1 = PromotionalRule.new(label_2, "percentage", true, quantity_rule_2)
24
+ promotion_rule_2 = PromotionalRule.new(label_1, "flat_rate", true, quantity_rule_1)
25
+
26
+
27
+ promotion.add_rules_in_bulk([promotion_rule_2,promotion_rule_1])
28
+
29
+ # promotional Rules
30
+ # Checkout interface
31
+ # scan(item) #remove_scan(item)
32
+
33
+ co = Checkout.new(promotion.promotion_rules)
34
+
35
+ co.scan(products[0])
36
+ co.scan(products[1])
37
+ co.scan(products[2])
38
+
39
+ table = Print.new([co]).table
40
+
41
+ puts table
@@ -0,0 +1,64 @@
1
+ class Adjustment
2
+ attr_reader :basket, :promotional_rules, :basket_without_discounts
3
+
4
+ def initialize(basket, promotional_rules)
5
+ @basket = basket
6
+ @promotional_rules = promotional_rules
7
+ @applied_promotional_rules = []
8
+ end
9
+
10
+ def calculate_total
11
+ adjusted_total = apply_item_discounts
12
+ apply_basket_discounts(adjusted_total)
13
+ end
14
+
15
+ def eligible_promotional_rules
16
+ pr = (@basket_promotional_rules | @product_promotional_rules).map(&:label)
17
+ pr.join(",")
18
+ end
19
+
20
+ private
21
+
22
+ def apply_item_discounts
23
+ adjusted_total = BigDecimal(0)
24
+
25
+ basket.items.each do |basket_item|
26
+ @product_promotional_rules = eligible_product_promo_discounts(basket_item)
27
+ compute_item_discounted_price(basket_item)
28
+ adjusted_total += basket_item.product.price * basket_item.quantity
29
+ end
30
+ adjusted_total
31
+ end
32
+
33
+ def apply_basket_discounts(items_total)
34
+ @basket_promotional_rules = eligible_basket_promotional_discounts
35
+
36
+ unless @basket_promotional_rules.empty?
37
+ basket_promotional_rules.each do |promotional_rule|
38
+ items_total = promotional_rule.calculate_total(items_total)
39
+ end
40
+ end
41
+
42
+ items_total
43
+ end
44
+
45
+ def compute_item_discounted_price(basket_item)
46
+ return if @product_promotional_rules.empty?
47
+ @product_promotional_rules.each do |promotional_rule|
48
+ price = promotional_rule.calculate_total(basket_item)
49
+ basket_item.product.price = price unless price.nil?
50
+ end
51
+ end
52
+
53
+ def eligible_product_promo_discounts(basket_item)
54
+ promotional_rules.select do |promo_rule|
55
+ promo_rule.on_item == true && promo_rule.rule.eligible?(basket_item)
56
+ end
57
+ end
58
+
59
+ def eligible_basket_promotional_discounts
60
+ promotional_rules.select do |promo_rule|
61
+ promo_rule.on_item == false
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "./basket_item.rb"
2
+ require_relative "./collections/item_collection.rb"
3
+
4
+ class Basket
5
+ attr_accessor :items
6
+
7
+ def initialize
8
+ @items = ItemCollection.new
9
+ end
10
+
11
+ def add_item(product)
12
+ item = items.find_product(product.product_code)
13
+ if item.nil?
14
+ items.push(BasketItem.new(product))
15
+ else
16
+ item.increment_item_quantity
17
+ end
18
+ end
19
+
20
+ def remove_item(product)
21
+ item = items.find_product(product.product_code)
22
+ if item.quantity == 1
23
+ items.delete(item)
24
+ else
25
+ item.decrement_item_quantity
26
+ end
27
+ end
28
+
29
+ def items_in_basket
30
+ products = []
31
+ items.each do |item|
32
+ products << "#{item.product.product_code} - #{item.product.name}"
33
+ end
34
+
35
+ products.join(" , ")
36
+ end
37
+ end
@@ -0,0 +1,16 @@
1
+ class BasketItem
2
+ attr_accessor :product, :quantity
3
+
4
+ def initialize(product)
5
+ @product = product
6
+ @quantity = 1
7
+ end
8
+
9
+ def increment_item_quantity
10
+ @quantity += 1
11
+ end
12
+
13
+ def decrement_item_quantity
14
+ @quantity -= 1
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "./basket.rb"
2
+ require_relative "./adjustment.rb"
3
+
4
+ class Checkout
5
+ attr_accessor :basket, :promotional_rules, :adjustment
6
+
7
+ def initialize(promotional_rules)
8
+ @basket = Basket.new
9
+ @promotional_rules = promotional_rules
10
+ end
11
+
12
+ def scan(product)
13
+ basket.add_item(product)
14
+ end
15
+
16
+ def remove_scan(product)
17
+ basket.remove_item(product)
18
+ end
19
+
20
+ def total
21
+ @adjustment = Adjustment.new(basket, promotional_rules)
22
+ @adjustment.calculate_total
23
+ end
24
+
25
+ def applied_promotional_rules
26
+ adjustment.eligible_promotional_rules if adjustment
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ class ItemCollection < Array
2
+ def find_product(product_code)
3
+ each do |item|
4
+ @item = item if item.product.product_code == product_code
5
+ end
6
+
7
+ @item
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ class ProductCollection < Array
2
+ def find_by_product_code(code)
3
+ each do |product|
4
+ @fetch_product = product if product.product_code == code
5
+ end
6
+
7
+ @fetch_product
8
+ end
9
+
10
+ def validate_product_code_is_uniq(code)
11
+ find_by_product_code(code).nil? ? true : (raise "DuplicateProductCodeError")
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class PromotionalRulesCollection < Array
2
+ def find_promotional_rule(rule)
3
+ each do |promotional_rule|
4
+ @promotional_rule = promotional_rule if promotional_rule == rule
5
+ end
6
+
7
+ @promotional_rule
8
+ end
9
+
10
+ def validate_promotion_rule_is_uniq(promotional_rule)
11
+ err_msg = "DuplicatePromotionalRuleError"
12
+ find_promotional_rule(promotional_rule).nil? ? true : (raise err_msg.to_s)
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module RuleValidations
2
+ DISCOUNT_TYPES = %w[percentage flat_rate].freeze
3
+
4
+ def should_be_present(name, attribute)
5
+ raise "#{name} Should be present" if attribute == "" || attribute.nil?
6
+ end
7
+
8
+ def check_discount_type(discount_type)
9
+ discount_type_error = "Allowed values 'percentage' or 'flat_rate'"
10
+ raise ArgumentError, discount_type_error unless
11
+ DISCOUNT_TYPES.include?(discount_type)
12
+ end
13
+
14
+ def either_should_be_present(name, attr1, attr2)
15
+ raise "atleast fill out #{name}" if attr1.empty? && attr2.empty?
16
+ end
17
+
18
+ def atleast_one_should_be_present(name, attr1, attr2)
19
+ raise "either fill out #{name}" if
20
+ attr1.empty? == false && attr2.empty? == false
21
+ end
22
+
23
+ def should_be_a_number(name, attribute)
24
+ raise "#{name} should be a number" if attribute.is_a?(Numeric) == false
25
+ end
26
+
27
+ def should_be_less_than(name, attribute, number)
28
+ raise "#{name} should be >= #{number}" if attribute < number
29
+ end
30
+
31
+ def should_be_a_boolean(name, attribute)
32
+ raise "#{name} should be a boolean" if boolean?(attribute) == false
33
+ end
34
+
35
+ def boolean?(attribute)
36
+ ["true", "false", true, false].include? attribute
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ Product Code, Name, Price
2
+ 001,book,20
3
+ 002,music cd,30
4
+ 003,chocolate bar,50
@@ -0,0 +1,37 @@
1
+ require "csv"
2
+
3
+ require_relative "./product.rb"
4
+ require_relative "./collections/product_collection.rb"
5
+
6
+ class Parser
7
+ attr_accessor :path, :products
8
+
9
+ # initialize a new conference settings
10
+ def initialize(path = "", no_validate = false, use_mock_file = false)
11
+ @path = (use_mock_file == true ? mock_file_path : path)
12
+ validate if no_validate == false
13
+ end
14
+
15
+ def validate
16
+ err_msg = "No such file found"
17
+ raise err_msg.to_s if path == "" || path.nil? || File.exist?(path) == false
18
+ end
19
+
20
+ def process_file
21
+ @products = ProductCollection.new
22
+
23
+ CSV.foreach(path, col_sep: ",", row_sep: :auto, headers: true) do |row|
24
+ product = Product.new(row[0], row[1], row[2])
25
+ next unless @products.validate_product_code_is_uniq(product.product_code)
26
+ @products << product
27
+ end
28
+
29
+ @products
30
+ end
31
+
32
+ # incase file is not provided
33
+ def mock_file_path
34
+ source = __dir__
35
+ File.join(source, "input/input.csv")
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ require "terminal-table"
2
+ require "money"
3
+
4
+ class Print
5
+ attr_reader :checkouts
6
+
7
+ def initialize(checkouts)
8
+ @checkouts = checkouts
9
+ end
10
+
11
+ def table
12
+ rows = []
13
+ headings = ["", "Items In basket", "Applied Promo Rules", "Total Price"]
14
+ checkouts.each_with_index do |checkout, index|
15
+ price = Money.new(checkout.total * 100).format
16
+ items = checkout.basket.items_in_basket
17
+ applied_promotional_rules = checkout.applied_promotional_rules
18
+
19
+ rows << ["Test_#{index}", items, applied_promotional_rules, price]
20
+ end
21
+ Terminal::Table.new headings: headings, rows: rows
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ class Product
2
+ attr_accessor :price, :product_code, :name
3
+
4
+ def initialize(product_code, name, price, no_validate = false)
5
+ @product_code = product_code
6
+ @name = name
7
+ @price = BigDecimal(price)
8
+
9
+ validate if no_validate == false
10
+ end
11
+
12
+ def validate
13
+ [product_code, name, price].each do |parameter|
14
+ raise ArgumentError, "ProductParameterMissing" if parameter.nil?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "./collections/promotional_rules_collection.rb"
2
+ require_relative "./promotional_rule.rb"
3
+
4
+ class Promotion
5
+ attr_reader :name, :promotion_code
6
+
7
+ attr_accessor :promotion_rules
8
+
9
+ def initialize(name, promotion_code)
10
+ @name = name
11
+ @promotion_code = promotion_code
12
+ @promotion_rules = PromotionalRulesCollection.new
13
+ end
14
+
15
+ def add_rule(promotion_rule)
16
+ rule = promotion_rules.find_promotional_rule(promotion_rule)
17
+ promotion_rules.push(promotion_rule) if rule.nil?
18
+ end
19
+
20
+ def remove_rule(promotion_rule)
21
+ rule = promotion_rules.find_promotional_rule(promotion_rule)
22
+ promotion_rules.delete(rule) unless rule.nil?
23
+ end
24
+
25
+ def add_rules_in_bulk(promotion_rules)
26
+ promotion_rules.each do |promotion_rule|
27
+ add_rule(promotion_rule)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ module Promotional
2
+ module Rule
3
+ class QuantityPrice < PromotionalRule
4
+ attr_reader :minimum_quantity, :value
5
+
6
+ def initialize(minimum_quantity, value, no_validate = false)
7
+ @minimum_quantity = minimum_quantity
8
+ @value = value
9
+
10
+ validate if no_validate == false
11
+ end
12
+
13
+ def eligible?(item)
14
+ item.quantity >= minimum_quantity
15
+ end
16
+
17
+ def calculate_discounted_price(basket_item, discount_type)
18
+ if discount_type == "percentage" && eligible?(basket_item)
19
+ discount = compute_discount(basket_item)
20
+ basket_item.product.price = discount
21
+ elsif discount_type == "flat_rate" && eligible?(basket_item)
22
+ basket_item.product.price = value
23
+ end
24
+ end
25
+
26
+ def calulate_total_discounted_price(total); end
27
+
28
+ def validate
29
+ should_be_present("QuantityPrice::Value", value)
30
+ should_be_a_number("QuantiyPrice::MinimumQuantity", minimum_quantity)
31
+ should_be_less_than("QuantityPrice::MinQuantity", minimum_quantity, 1)
32
+ end
33
+
34
+ private
35
+
36
+ def compute_discount(basket_item)
37
+ discount = (basket_item.product.price * value) / 100
38
+ basket_item.product.price - discount
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ require_relative "./concerns/rule_validations.rb"
2
+
3
+ class PromotionalRule
4
+ include RuleValidations
5
+
6
+ attr_reader :label, :discount_type, :on_item, :rule
7
+
8
+ def initialize(label, discount_type, on_item, rule, no_validate = false)
9
+ @label = label
10
+ @discount_type = discount_type
11
+ @on_item = on_item
12
+ @rule = rule
13
+
14
+ validate if no_validate == false
15
+ end
16
+
17
+ def eligible?
18
+ raise "Should be defined by the respective rule"
19
+ end
20
+
21
+ def validate
22
+ should_be_present("label", label)
23
+ check_discount_type(discount_type)
24
+ should_be_a_boolean("PromotionaRule::OnItem", on_item)
25
+ should_be_present("PromotionalRule::Rule", rule)
26
+ end
27
+
28
+ def calculate_discounted_price
29
+ raise "Should be defined by the respective rule"
30
+ end
31
+
32
+ def calculate_total_discounted_price
33
+ raise "Should be defined by the respective rule"
34
+ end
35
+
36
+ def calculate_total(arg)
37
+ if on_item == true
38
+ rule.calculate_discounted_price(arg, discount_type)
39
+ else
40
+ rule.calculate_total_discounted_price(arg, discount_type)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Wunder
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/wunder.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "pry"
2
+
3
+ files = ["./wunder/version.rb", "./wunder/parser.rb",
4
+ "./wunder/promotion.rb", "./wunder/checkout.rb", "./wunder/print.rb"]
5
+ rule_files = Dir[File.join(__dir__, "wunder", "promotional", "rule", "*.rb")]
6
+
7
+ (files | rule_files).each do |file|
8
+ require_relative file
9
+ end
10
+
11
+ module Wunder
12
+ Money.default_currency = Money::Currency.new("EUR")
13
+ Money.use_i18n = false
14
+ end
data/wunder.gemspec ADDED
@@ -0,0 +1,48 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "wunder/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wunder"
8
+ spec.version = Wunder::VERSION
9
+ spec.authors = ["Akhilesh"]
10
+ spec.email = ["akhileshsharma.aki@gmail.com"]
11
+
12
+ spec.summary = %q{Code Test.}
13
+ spec.description = %q{Checkout system with flexible rules.}
14
+ spec.homepage = "https://github.com/basicsaki/wunder"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "money","~> 6.11"
36
+ spec.add_dependency "terminal-table","~> 1.8"
37
+
38
+ spec.add_development_dependency "pry", "~> 0.11"
39
+ spec.add_development_dependency "faker", "~> 1.8"
40
+ spec.add_development_dependency "rubocop", "~> 0.57"
41
+ spec.add_development_dependency "sub-inspector", "0.1.5"
42
+
43
+ spec.add_development_dependency "bundler", "~> 1.16"
44
+ spec.add_development_dependency "bundler-audit","~> 0.6"
45
+
46
+ spec.add_development_dependency "rake", "~> 10.0"
47
+ spec.add_development_dependency "rspec", "~> 3.0"
48
+ end
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wunder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akhilesh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: money
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faker
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.57'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.57'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sub-inspector
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.5
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.16'
111
+ - !ruby/object:Gem::Dependency
112
+ name: bundler-audit
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.6'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.6'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '10.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '10.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ description: Checkout system with flexible rules.
154
+ email:
155
+ - akhileshsharma.aki@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".rubocop.yml"
163
+ - ".travis.yml"
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - lib/interface.rb
173
+ - lib/wunder.rb
174
+ - lib/wunder/adjustment.rb
175
+ - lib/wunder/basket.rb
176
+ - lib/wunder/basket_item.rb
177
+ - lib/wunder/checkout.rb
178
+ - lib/wunder/collections/item_collection.rb
179
+ - lib/wunder/collections/product_collection.rb
180
+ - lib/wunder/collections/promotional_rules_collection.rb
181
+ - lib/wunder/concerns/rule_validations.rb
182
+ - lib/wunder/input/input.csv
183
+ - lib/wunder/parser.rb
184
+ - lib/wunder/print.rb
185
+ - lib/wunder/product.rb
186
+ - lib/wunder/promotion.rb
187
+ - lib/wunder/promotional/rule/quantity_price.rb
188
+ - lib/wunder/promotional_rule.rb
189
+ - lib/wunder/version.rb
190
+ - wunder.gemspec
191
+ homepage: https://github.com/basicsaki/wunder
192
+ licenses:
193
+ - MIT
194
+ metadata:
195
+ allowed_push_host: https://rubygems.org
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 2.7.7
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: Code Test.
216
+ test_files: []