Class: Dugway::Cart
- Inherits:
-
Object
- Object
- Dugway::Cart
- Defined in:
- lib/dugway/cart.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #country ⇒ Object
- #discount ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Cart
constructor
A new instance of Cart.
- #item_count ⇒ Object
- #reset ⇒ Object
- #shipping ⇒ Object
- #subtotal ⇒ Object
- #tax ⇒ Object
- #total ⇒ Object
- #update(params) ⇒ Object
Constructor Details
#initialize ⇒ Cart
Returns a new instance of Cart.
5 6 7 |
# File 'lib/dugway/cart.rb', line 5 def initialize reset end |
Instance Attribute Details
#items ⇒ Object
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(=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 ⇒ Object
29 30 31 |
# File 'lib/dugway/cart.rb', line 29 def country nil end |
#discount ⇒ Object
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
13 14 15 |
# File 'lib/dugway/cart.rb', line 13 def empty? items.empty? end |
#item_count ⇒ Object
17 18 19 |
# File 'lib/dugway/cart.rb', line 17 def item_count items.map { |item| item.quantity }.inject(:+) || 0 end |
#reset ⇒ Object
9 10 11 |
# File 'lib/dugway/cart.rb', line 9 def reset self.items = [] end |
#shipping ⇒ Object
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 |
#subtotal ⇒ Object
21 22 23 |
# File 'lib/dugway/cart.rb', line 21 def subtotal items.map { |item| item.price }.inject(:+) || 0.0 end |
#tax ⇒ Object
42 43 44 45 46 47 |
# File 'lib/dugway/cart.rb', line 42 def tax { 'enabled' => false, 'amount' => 0.0 } end |
#total ⇒ Object
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 |