Skip to content

Instantly share code, notes, and snippets.

@imustardsoft
Created November 8, 2011 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imustardsoft/1347754 to your computer and use it in GitHub Desktop.
Save imustardsoft/1347754 to your computer and use it in GitHub Desktop.
price_bucket.rb
class Calculator::PriceBucket < Calculator
preference :minimal_amount, :decimal, :default => 0
preference :normal_amount, :decimal, :default => 0
preference :discount_amount, :decimal, :default => 0
def self.description
I18n.t("price_bucket")
end
# as object we always get line items, as calculable we have Coupon, ShippingMethod
def compute(object)
if object.is_a?(Array)
base = object.map{ |o| o.respond_to?(:amount) ? o.amount : o.to_d }.sum
else
base = object.respond_to?(:amount) ? object.amount : object.to_d
end
if base >= self.preferred_minimal_amount
self.preferred_normal_amount
else
self.preferred_discount_amount
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment