Class: MerchantSidekick::ShoppingCart::Cart

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency_code = 'USD', options = {}) ⇒ Cart

Returns a new instance of Cart.



21
22
23
24
25
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 21

def initialize(currency_code = 'USD', options = {})
  @currency = currency_code
  @options = {:currency_code => currency_code}.merge(options)
  empty!
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



18
19
20
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 18

def currency
  @currency
end

#line_itemsObject (readonly)

Returns the value of attribute line_items.



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

def line_items
  @line_items
end

#optionsObject

cart options getter



140
141
142
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 140

def options
  @options
end

Instance Method Details

#add(stuff, quantity = 1, options = {}) ⇒ Object

Adds a single or array of sellable products to the cart and returns the cart line items.

E.g.

@cart.add @sellable
@cart.add @sellable, 4
@cart.add [@sellable1, @sellable2]


36
37
38
39
40
41
42
43
44
45
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 36

def add(stuff, quantity = 1, options = {})
  if stuff.is_a?(Array)
    stuff.inject([]) {|result, element| result << add(element, quantity, options)}
  elsif stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem)
    self.add_cart_line_item(stuff, options)
  else
    # assuming it is a "product" (e.g. sellable) instance
    self.add_product(stuff, quantity, options)
  end
end

#cart_line_item(product, quantity = 1, line_options = {}) ⇒ Object

Create a product line from a product (sellable) and copies all attributes that could be modified later



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 117

def cart_line_item(product, quantity = 1, line_options = {})
  raise "No price column available for '#{product.class.name}'" unless product.respond_to?(:price)
  # we need to set currency explicitly here for correct money conversion of the cart_line_item
  MerchantSidekick::ShoppingCart::LineItem.new do |line_item|
    line_item.options = self.options.merge(line_options)
    line_item.currency = self.currency
    line_item.quantity = quantity
    line_item.product = product
    line_item
  end
end

#cart_line_items(products) ⇒ Object

Return a list of cart line items from an array of products, e.g. Products



130
131
132
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 130

def cart_line_items(products)
  products.map {|p| self.cart_line_item(p)}
end

#empty!Object

Remove all line items from cart



83
84
85
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 83

def empty!
  @line_items = []
end

#empty?Boolean

Check to see if cart is empty?

Returns:

  • (Boolean)


88
89
90
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 88

def empty?
  @line_items.empty?
end

#find(what, stuff, options = {}) ⇒ Object

Finds an instance of line item, by product or line_item

E.g.

@cart.find(:first, @product) # -> @li
@cart.find(:all, @product)   # -> [@li1, @li2]


74
75
76
77
78
79
80
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 74

def find(what, stuff, options={})
  if stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem)
    self.find_line_items(what, stuff, options)
  else
    self.find_line_items_by_product(what, stuff, options)
  end
end

#items_countObject

counts number of entities line_items * quantities.



107
108
109
110
111
112
113
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 107

def items_count
  counter = 0
  self.line_items.each do |item|
    counter += item.quantity
  end
  counter
end

#line_items_countObject

counts number of line items.



102
103
104
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 102

def line_items_count
  self.line_items.size
end

#remove(stuff, options = {}) ⇒ Object Also known as: delete

Removes an item from the cart



48
49
50
51
52
53
54
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 48

def remove(stuff, options={})
  if stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem)
    self.remove_cart_line_item(stuff, options)
  else
    self.remove_product(stuff, options)
  end
end

#total_amountObject Also known as: total

Evaluates the total amount of a sum as all item prices * quantity



93
94
95
96
97
98
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 93

def total_amount
  sum = ::Money.new(1, self.currency)
  @line_items.each {|li| sum += li.total_amount}
  sum -= ::Money.new(1, self.currency)
  sum
end

#update(stuff, quantity, options = {}) ⇒ Object

Updates an existing line_item with quantity by product or line_item instance. If quanity is <= 0, the item will be removed.



59
60
61
62
63
64
65
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 59

def update(stuff, quantity, options={})
  if stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem)
    self.update_cart_line_item(stuff, quantity, options)
  else
    self.update_product(stuff, quantity, options)
  end
end