mercadona 0.1.0
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 +7 -0
- data/.ruby-version +1 -0
- data/LICENSE.txt +21 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/mercadona/amount_formatter.rb +9 -0
- data/lib/mercadona/checkout.rb +44 -0
- data/lib/mercadona/core.rb +18 -0
- data/lib/mercadona/discount/base.rb +39 -0
- data/lib/mercadona/discount/bogo.rb +13 -0
- data/lib/mercadona/discount/bulk.rb +19 -0
- data/lib/mercadona/discount/quantity.rb +19 -0
- data/lib/mercadona/entity/base.rb +7 -0
- data/lib/mercadona/entity/discount_rule.rb +31 -0
- data/lib/mercadona/entity/order_item.rb +13 -0
- data/lib/mercadona/error/argument.rb +10 -0
- data/lib/mercadona/error/runtime.rb +9 -0
- data/lib/mercadona/version.rb +5 -0
- data/lib/mercadona.rb +3 -0
- data/mercadona.gemspec +32 -0
- metadata +121 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 48672addf9ab3b766efbb3becdd57685902710787756a46a3328da7063b146ec
|
|
4
|
+
data.tar.gz: b0dd301ac7042f467e8e6db9c137080f4aec8f66f4cf808ece4c7333c88ccce7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7ed023cacd19cb778fe26fdff556af8297f1a4805c34f53a5d245ecc82afd1e37fb95fc10179a498f3d61064744f25d8e6c7a33f70b3696f53b3b37f18d0e76d
|
|
7
|
+
data.tar.gz: 4fe01102cacdafb1683596daadbc2660a6892cdb1c89dd17067a3e2fd2ea7f4cb6a7fc267f2a8c105182a5cc8d03cbcf894832fa3c2a08a0cd419ff5d450eae6
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-2.7.0
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Vladislav Trotsenko
|
|
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/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'mercadona'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require 'irb'
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
class Checkout
|
|
5
|
+
DEFAULT_CURRENCY = '£'
|
|
6
|
+
INITIAL_AMOUNT = 0
|
|
7
|
+
|
|
8
|
+
def initialize(discount_rules, currency = Mercadona::Checkout::DEFAULT_CURRENCY)
|
|
9
|
+
@discount_rules = discount_rules
|
|
10
|
+
@currency = currency
|
|
11
|
+
@basket = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def scan(order_item)
|
|
15
|
+
basket << order_item
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def total
|
|
19
|
+
Mercadona::AmountFormatter.call(currency, calculate_amount / 100.0)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :discount_rules, :currency, :basket
|
|
25
|
+
|
|
26
|
+
def discount_order_item?(order_item)
|
|
27
|
+
discount_rules.key?(order_item.product_code)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def amount_with_discount(order_item, quantity)
|
|
31
|
+
discount_rules[order_item.product_code].amount_with_discount(order_item, quantity)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def calculate_amount
|
|
35
|
+
# Because the technical brief does not specify the pricing method (whether per item or per specific weight),
|
|
36
|
+
# we will simplify our calculations by using the price per item as the reference point.
|
|
37
|
+
basket.tally.inject(Mercadona::Checkout::INITIAL_AMOUNT) do |amount, (order_item, quantity)|
|
|
38
|
+
next amount + amount_with_discount(order_item, quantity) if discount_order_item?(order_item)
|
|
39
|
+
|
|
40
|
+
amount + (order_item.price_in_fractional_currency * quantity)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
ALLOWED_COMPARISON_OPERATORS = %w[== < > >= <=].freeze
|
|
5
|
+
|
|
6
|
+
require_relative '../mercadona/error/argument'
|
|
7
|
+
require_relative '../mercadona/error/runtime'
|
|
8
|
+
require_relative '../mercadona/entity/base'
|
|
9
|
+
require_relative '../mercadona/entity/discount_rule'
|
|
10
|
+
require_relative '../mercadona/entity/order_item'
|
|
11
|
+
require_relative '../mercadona/amount_formatter'
|
|
12
|
+
require_relative '../mercadona/discount/base'
|
|
13
|
+
require_relative '../mercadona/discount/bogo'
|
|
14
|
+
require_relative '../mercadona/discount/quantity'
|
|
15
|
+
require_relative '../mercadona/discount/bulk'
|
|
16
|
+
require_relative '../mercadona/checkout'
|
|
17
|
+
require_relative '../mercadona/version'
|
|
18
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
module Discount
|
|
5
|
+
class Base
|
|
6
|
+
DISCOUNT_RULES_ATTRS = %i[condition quantity].freeze
|
|
7
|
+
|
|
8
|
+
def self.call(discount_rule, order_item, order_item_quantity)
|
|
9
|
+
new(discount_rule, order_item, order_item_quantity).call
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def initialize(discount_rule, order_item, order_item_quantity)
|
|
13
|
+
@discount_rule = discount_rule
|
|
14
|
+
@order_item_price = order_item.price_in_fractional_currency
|
|
15
|
+
@order_item_quantity = order_item_quantity
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def call
|
|
19
|
+
raise Mercadona::Error::Runtime, Mercadona::Error::Runtime::NOT_IMPLEMENTED
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :discount_rule, :order_item_price, :order_item_quantity
|
|
25
|
+
|
|
26
|
+
Mercadona::Discount::Base::DISCOUNT_RULES_ATTRS.each do |method_name|
|
|
27
|
+
define_method(method_name) { discount_rule.public_send(method_name) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def discount_case?
|
|
31
|
+
order_item_quantity.public_send(condition, quantity)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def amount_without_discount
|
|
35
|
+
@amount_without_discount ||= order_item_price * order_item_quantity
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
module Discount
|
|
5
|
+
class Bulk < Mercadona::Discount::Base
|
|
6
|
+
def call
|
|
7
|
+
return amount_without_discount unless discount_case?
|
|
8
|
+
|
|
9
|
+
amount_without_discount - (amount_without_discount * discount).round
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def discount
|
|
15
|
+
@discount ||= discount_rule.discount.to_f
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
module Discount
|
|
5
|
+
class Quantity < Mercadona::Discount::Base
|
|
6
|
+
def call
|
|
7
|
+
return amount_without_discount unless discount_case?
|
|
8
|
+
|
|
9
|
+
amount_without_discount - (discount * order_item_quantity)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def discount
|
|
15
|
+
@discount ||= (discount_rule.discount * 100).to_i
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
module Entity
|
|
5
|
+
DiscountRule = Mercadona::Entity::Base.new(:type, :condition, :quantity, :discount, keyword_init: true) do
|
|
6
|
+
def initialize(type:, condition:, **args)
|
|
7
|
+
validate_sensitive_args(type, condition)
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def amount_with_discount(order_item, order_item_quantity)
|
|
12
|
+
type.call(self, order_item, order_item_quantity)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def discount_class_valid?(discount_class)
|
|
18
|
+
discount_class.respond_to?(:call)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def comparison_operator_valid?(comparison_operator)
|
|
22
|
+
Mercadona::ALLOWED_COMPARISON_OPERATORS.include?(comparison_operator)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def validate_sensitive_args(discount_class, comparison_operator)
|
|
26
|
+
raise Mercadona::Error::Argument, Mercadona::Error::Argument::DISCOUNT_CLASS unless discount_class_valid?(discount_class)
|
|
27
|
+
raise Mercadona::Error::Argument, Mercadona::Error::Argument::COMPARISON_OPERATOR unless comparison_operator_valid?(comparison_operator)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
module Entity
|
|
5
|
+
OrderItem = Mercadona::Entity::Base.new(:product_code, :name, :price, keyword_init: true) do
|
|
6
|
+
# Understanding Ruby's floating-point precision issues for money calculations,
|
|
7
|
+
# let's use whole numbers instead of floats.
|
|
8
|
+
def price_in_fractional_currency
|
|
9
|
+
(price.to_f * 100).to_i
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mercadona
|
|
4
|
+
module Error
|
|
5
|
+
class Argument < ::ArgumentError
|
|
6
|
+
DISCOUNT_CLASS = 'type: must have a :call method'
|
|
7
|
+
COMPARISON_OPERATOR = "condition: must be one of #{Mercadona::ALLOWED_COMPARISON_OPERATORS.join(', ')}"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
data/lib/mercadona.rb
ADDED
data/mercadona.gemspec
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/mercadona/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'mercadona'
|
|
7
|
+
spec.version = Mercadona::VERSION
|
|
8
|
+
spec.authors = ['Vladislav Trotsenko']
|
|
9
|
+
spec.email = ['admin@bestweb.com.ua']
|
|
10
|
+
|
|
11
|
+
spec.summary = %(Simple retail shop interface)
|
|
12
|
+
spec.description = %(Simple retail shop interface.)
|
|
13
|
+
|
|
14
|
+
spec.homepage = 'https://github.com/bestwebua/mercadona'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
|
|
17
|
+
spec.metadata = {
|
|
18
|
+
'homepage_uri' => 'https://github.com/bestwebua/mercadona',
|
|
19
|
+
'changelog_uri' => 'https://github.com/bestwebua/mercadona/blob/master/CHANGELOG.md',
|
|
20
|
+
'source_code_uri' => 'https://github.com/bestwebua/mercadona',
|
|
21
|
+
'documentation_uri' => 'https://github.com/bestwebua/mercadona/blob/master/README.md',
|
|
22
|
+
'bug_tracker_uri' => 'https://github.com/bestwebua/mercadona/issues'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
spec.required_ruby_version = '>= 2.7.0'
|
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").select { |f| f.match(%r{^(bin|lib)/|.ruby-version|mercadona.gemspec|LICENSE}) }
|
|
27
|
+
spec.require_paths = %w[lib]
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency 'factory_bot', '~> 6.4', '>= 6.4.4'
|
|
30
|
+
spec.add_development_dependency 'rake', '~> 13.2', '>= 13.2.1'
|
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.13'
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mercadona
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Vladislav Trotsenko
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2024-07-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: factory_bot
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6.4'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 6.4.4
|
|
23
|
+
type: :development
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - "~>"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '6.4'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 6.4.4
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: rake
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '13.2'
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 13.2.1
|
|
43
|
+
type: :development
|
|
44
|
+
prerelease: false
|
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - "~>"
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '13.2'
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: 13.2.1
|
|
53
|
+
- !ruby/object:Gem::Dependency
|
|
54
|
+
name: rspec
|
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '3.13'
|
|
60
|
+
type: :development
|
|
61
|
+
prerelease: false
|
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - "~>"
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '3.13'
|
|
67
|
+
description: Simple retail shop interface.
|
|
68
|
+
email:
|
|
69
|
+
- admin@bestweb.com.ua
|
|
70
|
+
executables: []
|
|
71
|
+
extensions: []
|
|
72
|
+
extra_rdoc_files: []
|
|
73
|
+
files:
|
|
74
|
+
- ".ruby-version"
|
|
75
|
+
- LICENSE.txt
|
|
76
|
+
- bin/console
|
|
77
|
+
- bin/setup
|
|
78
|
+
- lib/mercadona.rb
|
|
79
|
+
- lib/mercadona/amount_formatter.rb
|
|
80
|
+
- lib/mercadona/checkout.rb
|
|
81
|
+
- lib/mercadona/core.rb
|
|
82
|
+
- lib/mercadona/discount/base.rb
|
|
83
|
+
- lib/mercadona/discount/bogo.rb
|
|
84
|
+
- lib/mercadona/discount/bulk.rb
|
|
85
|
+
- lib/mercadona/discount/quantity.rb
|
|
86
|
+
- lib/mercadona/entity/base.rb
|
|
87
|
+
- lib/mercadona/entity/discount_rule.rb
|
|
88
|
+
- lib/mercadona/entity/order_item.rb
|
|
89
|
+
- lib/mercadona/error/argument.rb
|
|
90
|
+
- lib/mercadona/error/runtime.rb
|
|
91
|
+
- lib/mercadona/version.rb
|
|
92
|
+
- mercadona.gemspec
|
|
93
|
+
homepage: https://github.com/bestwebua/mercadona
|
|
94
|
+
licenses:
|
|
95
|
+
- MIT
|
|
96
|
+
metadata:
|
|
97
|
+
homepage_uri: https://github.com/bestwebua/mercadona
|
|
98
|
+
changelog_uri: https://github.com/bestwebua/mercadona/blob/master/CHANGELOG.md
|
|
99
|
+
source_code_uri: https://github.com/bestwebua/mercadona
|
|
100
|
+
documentation_uri: https://github.com/bestwebua/mercadona/blob/master/README.md
|
|
101
|
+
bug_tracker_uri: https://github.com/bestwebua/mercadona/issues
|
|
102
|
+
post_install_message:
|
|
103
|
+
rdoc_options: []
|
|
104
|
+
require_paths:
|
|
105
|
+
- lib
|
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: 2.7.0
|
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - ">="
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: '0'
|
|
116
|
+
requirements: []
|
|
117
|
+
rubygems_version: 3.4.10
|
|
118
|
+
signing_key:
|
|
119
|
+
specification_version: 4
|
|
120
|
+
summary: Simple retail shop interface
|
|
121
|
+
test_files: []
|