Skip to content

Instantly share code, notes, and snippets.

@gavinballard
Created May 12, 2016 10:40
Show Gist options
  • Save gavinballard/bc5420ff7b4c79a9c3389537c35bd08d to your computer and use it in GitHub Desktop.
Save gavinballard/bc5420ff7b4c79a9c3389537c35bd08d to your computer and use it in GitHub Desktop.
Buy Set Get X% Off Campaign
# The campaign class.
class BuySetGetXOffCampaign
def initialize(selector, discount, partitioner)
@selector = selector
@discount = discount
@partitioner = partitioner
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment