-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbogo_campaign.rb
59 lines (56 loc) · 1.33 KB
/
bogo_campaign.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# BogoCampaign
# ============
#
# Example campaigns
# -----------------
#
# * Buy one, get one free
# * Buy one, get one 50% off
# * Buy two items and get a third for $5 off
#
class BogoCampaign
# Initializes the campaign.
#
# Arguments
# ---------
#
# * selector
# The selector finds eligible items for this campaign.
#
# * discount
# The discount changes the prices of the items returned by the partitioner.
#
# * partitioner
# The partitioner takes all applicable items, and returns only those that
# are to be discounted. In a "Buy two items, get the third for free"
# campaign, the partitioner would skip two items and return the third item.
#
def initialize(selector, discount, partitioner)
@selector = selector
@discount = discount
@partitioner = partitioner
end
# Runs the campaign on the given cart.
#
# Arguments
# ---------
#
# * cart
# The cart to which the campaign is applied.
#
# Example
# -------
# To run the campaign on the input cart:
#
# campaign.run(Input.cart)
#
def run(cart)
applicable_items = cart.line_items.select do |line_item|
@selector.match?(line_item)
end
discounted_items = @partitioner.partition(cart, applicable_items)
discounted_items.each do |line_item|
@discount.apply(line_item)
end
end
end