Class: Dugway::Cart

Inherits:
Object
  • Object
show all
Defined in:
lib/dugway/cart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCart

Returns a new instance of Cart.



5
6
7
# File 'lib/dugway/cart.rb', line 5

def initialize
  reset
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/dugway/cart.rb', line 3

def items
  @items
end

Instance Method Details

#as_json(options = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# 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

#countryObject



29
30
31
# File 'lib/dugway/cart.rb', line 29

def country
  nil
end

#discountObject



49
50
51
52
53
54
55
# File 'lib/dugway/cart.rb', line 49

def discount
  { 
    'enabled' => false,
    'pending' => false,
    'amount' => 0.0
  }
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/dugway/cart.rb', line 13

def empty?
  items.empty?
end

#item_countObject



17
18
19
# File 'lib/dugway/cart.rb', line 17

def item_count
  items.map { |item| item.quantity }.inject(:+) || 0
end

#resetObject



9
10
11
# File 'lib/dugway/cart.rb', line 9

def reset
  self.items = []
end

#shippingObject



33
34
35
36
37
38
39
40
# File 'lib/dugway/cart.rb', line 33

def shipping
  { 
    'enabled' => false,
    'amount' => 0.0,
    'strict' => false,
    'pending' => false
  }
end

#subtotalObject



21
22
23
# File 'lib/dugway/cart.rb', line 21

def subtotal
  items.map { |item| item.price }.inject(:+) || 0.0
end

#taxObject



42
43
44
45
46
47
# File 'lib/dugway/cart.rb', line 42

def tax
  { 
    'enabled' => false,
    'amount' => 0.0
  }
end

#totalObject



25
26
27
# File 'lib/dugway/cart.rb', line 25

def total
  subtotal + shipping['amount'] + tax['amount'] - discount['amount']
end

#update(params) ⇒ Object



57
58
59
60
61
62
# 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