Skip to content

Instantly share code, notes, and snippets.

@fuadsaud
Last active November 24, 2015 01:08
Show Gist options
  • Save fuadsaud/088bf58454c8be3c1a80 to your computer and use it in GitHub Desktop.
Save fuadsaud/088bf58454c8be3c1a80 to your computer and use it in GitHub Desktop.
class CheckOut
def initialize(rules)
@rules = rules
@goods = []
end
def scan(good)
goods << good unless good.nil?
end
def total
rules.map { |r| r.call(goods) }.reduce(0, &:+)
end
private
attr_reader :rules, :goods
end
class ModPrice
def initialize(sku:, regular_price:, quantity: 1, special_price: regular_price)
@sku = sku
@regular_price = regular_price
@quantity = quantity
@special_price = special_price
end
def call(goods)
count = goods.count { |g| g == sku }
count
.divmod(quantity)
.zip([special_price, regular_price])
.map { |(q, p)| q * p }
.reduce(&:+)
end
private
attr_reader :sku, :quantity, :regular_price, :special_price
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment