Skip to content

Instantly share code, notes, and snippets.

@ericchen
Created March 23, 2011 06:37
Show Gist options
  • Save ericchen/882707 to your computer and use it in GitHub Desktop.
Save ericchen/882707 to your computer and use it in GitHub Desktop.
Cart model
class Cart
def initialize
@products = []
@rules = []
end
def regular_total
@products.map { |p| p.price }.inject(&:+)
end
def benefits
@rules.map { |rule| rule.apply_for(@products) }.inject(&:+)
end
def total
regular_total - benefits
end
def <<(product)
@products << product
end
def when_buying(product)
rule = Rule.new(product)
@rules << rule
rule
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment