-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathitem_campaign.rb
More file actions
49 lines (46 loc) · 1.04 KB
/
item_campaign.rb
File metadata and controls
49 lines (46 loc) · 1.04 KB
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
# ItemCampaign
# ============
#
# Example campaigns
# -----------------
# * All items are 15% off
# * Items with the "sale" tag are 5$ off
# * Items that cost less than 10$ are 10% off
#
class ItemCampaign
# Initializes the campaign.
#
# Arguments
# ---------
# * selector
# The selector finds eligible items for the campaign.
#
# * discount
# The discount changes the price of the items returned by the partitioner.
#
def initialize(selector, discount)
@selector = selector
@discount = discount
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)
# Iterate through the line items in the cart.
cart.line_items.each do |line_item|
# Skip this line item unless it's associated with the target product.
next unless @selector.match?(line_item)
@discount.apply(line_item)
end
end
end