Class: MerchantSidekick::ShoppingCart::Cart
- Inherits:
-
Object
- Object
- MerchantSidekick::ShoppingCart::Cart
- Defined in:
- lib/merchant_sidekick/shopping_cart/cart.rb
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#line_items ⇒ Object
readonly
Returns the value of attribute line_items.
-
#options ⇒ Object
cart options getter.
Instance Method Summary collapse
-
#add(stuff, quantity = 1, options = {}) ⇒ Object
Adds a single or array of sellable products to the cart and returns the cart line items.
-
#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.
-
#cart_line_items(products) ⇒ Object
Return a list of cart line items from an array of products, e.g.
-
#empty! ⇒ Object
Remove all line items from cart.
-
#empty? ⇒ Boolean
Check to see if cart is empty?.
-
#find(what, stuff, options = {}) ⇒ Object
Finds an instance of line item, by product or line_item.
-
#initialize(currency_code = 'USD', options = {}) ⇒ Cart
constructor
A new instance of Cart.
-
#items_count ⇒ Object
counts number of entities line_items * quantities.
-
#line_items_count ⇒ Object
counts number of line items.
-
#remove(stuff, options = {}) ⇒ Object
(also: #delete)
Removes an item from the cart.
-
#total_amount ⇒ Object
(also: #total)
Evaluates the total amount of a sum as all item prices * quantity.
-
#update(stuff, quantity, options = {}) ⇒ Object
Updates an existing line_item with quantity by product or line_item instance.
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', = {}) @currency = currency_code @options = {:currency_code => currency_code}.merge() empty! end |
Instance Attribute Details
#currency ⇒ Object (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_items ⇒ Object (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 |
#options ⇒ Object
cart options getter
140 141 142 |
# File 'lib/merchant_sidekick/shopping_cart/cart.rb', line 140 def @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, = {}) if stuff.is_a?(Array) stuff.inject([]) {|result, element| result << add(element, quantity, )} elsif stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem) self.add_cart_line_item(stuff, ) else # assuming it is a "product" (e.g. sellable) instance self.add_product(stuff, quantity, ) 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, = {}) 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. = self..merge() 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?
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, ={}) if stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem) self.find_line_items(what, stuff, ) else self.find_line_items_by_product(what, stuff, ) end end |
#items_count ⇒ Object
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_count ⇒ Object
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, ={}) if stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem) self.remove_cart_line_item(stuff, ) else self.remove_product(stuff, ) end end |
#total_amount ⇒ Object 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, ={}) if stuff.is_a?(MerchantSidekick::ShoppingCart::LineItem) self.update_cart_line_item(stuff, quantity, ) else self.update_product(stuff, quantity, ) end end |