class Dugway::Cart
Attributes
items[RW]
Public Class Methods
new()
click to toggle source
# File lib/dugway/cart.rb, line 5 def initialize reset end
Public Instance Methods
as_json(options=nil)
click to toggle source
# File lib/dugway/cart.rb, line 64 def as_json(options=nil) { :item_count => item_count, :subtotal => subtotal, :price => subtotal, # deprecated but need this for backwards JS compatibility :total => total, :items => items, :country => country, :shipping => shipping, :discount => discount } end
country()
click to toggle source
# File lib/dugway/cart.rb, line 29 def country nil end
discount()
click to toggle source
# File lib/dugway/cart.rb, line 49 def discount { 'enabled' => false, 'pending' => false, 'amount' => 0.0 } end
empty?()
click to toggle source
# File lib/dugway/cart.rb, line 13 def empty? items.empty? end
item_count()
click to toggle source
# File lib/dugway/cart.rb, line 17 def item_count items.map { |item| item.quantity }.inject(:+) || 0 end
reset()
click to toggle source
# File lib/dugway/cart.rb, line 9 def reset self.items = [] end
shipping()
click to toggle source
# File lib/dugway/cart.rb, line 33 def shipping { 'enabled' => false, 'amount' => 0.0, 'strict' => false, 'pending' => false } end
subtotal()
click to toggle source
# File lib/dugway/cart.rb, line 21 def subtotal items.map { |item| item.price }.inject(:+) || 0.0 end
tax()
click to toggle source
# File lib/dugway/cart.rb, line 42 def tax { 'enabled' => false, 'amount' => 0.0 } end
total()
click to toggle source
# File lib/dugway/cart.rb, line 25 def total subtotal + shipping['amount'] + tax['amount'] - discount['amount'] end
update(params)
click to toggle source
# File lib/dugway/cart.rb, line 57 def update(params) add, adds, updates = params.delete(:add), params.delete(:adds), params.delete(:update) add_item(add) if add add_items(adds) if adds update_quantities(updates) if updates end
Private Instance Methods
add_item(add)
click to toggle source
# File lib/dugway/cart.rb, line 79 def add_item(add) id = add[:id].to_i unless item = items.find { |i| i.option['id'] == id.to_i } product, option = Dugway.store.product_and_option(id) if product && option item = Item.new item.id = items.size + 1 item.name = option['name'] == 'Default' ? product['name'] : "#{ product['name'] } - #{ option['name'] }" item.unit_price = option['price'] item.product = product item.option = option item.quantity = 0 items << item end end if item item.quantity += add[:quantity] ? add[:quantity].to_i : 1 end item end
add_items(adds)
click to toggle source
# File lib/dugway/cart.rb, line 105 def add_items(adds) adds.each { |add| add_item(add) } end
update_quantities(updates)
click to toggle source
# File lib/dugway/cart.rb, line 109 def update_quantities(updates) updates.each_pair { |id, qty| if item = items.find { |i| i.id == id.to_i } item.quantity = qty.to_i end } items.reject! { |item| item.quantity == 0 } end