Class: ActiveCart::Cart
- Inherits:
-
Object
- Object
- ActiveCart::Cart
- Extended by:
- Forwardable
- Includes:
- Enumerable, Singleton
- Defined in:
- lib/active_cart/cart.rb
Overview
The Cart class is the core class in ActiveCart. It is a singleton (so you can only have one cart per application), that gets setup initially by passing in a storage engine instance. Storage engines abstract away the storage of the cart, and is left as an exercise to the user. See the Storage engine docs for details.
The Cart class also takes order_total objects, which will calculate order totals. it may include thinkgs like shipping, or gift vouchers etc. See the Order Total docs for details.
The Cart object delegates a number of Array methods: :[], :<<, :[]=, :at, :clear, :collect, :map, :delete, :delete_at, :each, :each_index, :empty?, :eql?, :first, :include?, :index, :inject, :last, :length, :pop, :push, :shift, :size, :unshift
Instance Attribute Summary collapse
-
#customer ⇒ Object
Returns the value of attribute customer.
-
#order_total_calculators ⇒ Object
Returns the value of attribute order_total_calculators.
-
#storage_engine ⇒ Object
Returns the value of attribute storage_engine.
Class Method Summary collapse
-
.instance_with_setup_check ⇒ Object
(also: instance)
nodoc.
-
.setup(storage_engine, &block) ⇒ Object
The method MUST be called before you call instance, otherwise you will receive and StandardError You need to supply a storage engine.
Instance Method Summary collapse
-
#add_to_cart(item, quantity = 1) ⇒ Object
Adds an item (or a quantity of that item) to the cart.
-
#invoice_id ⇒ Object
Returns a unique id for the invoice.
-
#quantity ⇒ Object
Returns the number of items in the cart.
-
#remove_from_cart(item, quantity = 1) ⇒ Object
Removes an item (or a quantity of that item) from the cart.
-
#sub_total ⇒ Object
Returns the subtotal of cart, which is effectively the total of all the items multiplied by their quantites.
-
#total ⇒ Object
Returns the total of the cart.
Instance Attribute Details
#customer ⇒ Object
Returns the value of attribute customer.
12 13 14 |
# File 'lib/active_cart/cart.rb', line 12 def customer @customer end |
#order_total_calculators ⇒ Object
Returns the value of attribute order_total_calculators.
12 13 14 |
# File 'lib/active_cart/cart.rb', line 12 def order_total_calculators @order_total_calculators end |
#storage_engine ⇒ Object
Returns the value of attribute storage_engine.
12 13 14 |
# File 'lib/active_cart/cart.rb', line 12 def storage_engine @storage_engine end |
Class Method Details
.instance_with_setup_check ⇒ Object Also known as: instance
nodoc
17 18 19 20 |
# File 'lib/active_cart/cart.rb', line 17 def self.instance_with_setup_check raise StandardError, 'Please call setup first' unless @setup_called instance_without_setup_check end |
.setup(storage_engine, &block) ⇒ Object
The method MUST be called before you call instance, otherwise you will receive and StandardError You need to supply a storage engine. An optional block can be given which allows you to add order total items.
A typical initialization block might look like this
end
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_cart/cart.rb', line 31 def self.setup(storage_engine, &block) @setup_called = true instance = self.instance_without_setup_check instance.storage_engine = storage_engine instance.order_total_calculators = OrderTotalCollection.new(self) if block_given? yield instance.order_total_calculators end instance end |
Instance Method Details
#add_to_cart(item, quantity = 1) ⇒ Object
Adds an item (or a quantity of that item) to the cart. If the item already exists, the internal quantity will be incremented by the quantity paramater
69 70 71 |
# File 'lib/active_cart/cart.rb', line 69 def add_to_cart(item, quantity = 1) @storage_engine.add_to_cart(item, quantity) end |
#invoice_id ⇒ Object
Returns a unique id for the invoice. It’s upto the storage engine to generate and track these numbers
53 54 55 |
# File 'lib/active_cart/cart.rb', line 53 def invoice_id @storage_engine.invoice_id end |
#quantity ⇒ Object
Returns the number of items in the cart. Each different item in the cart may have different quantities, and this method will return the sum of that. For example if the first item has a quantity of 2 and the second has a quantity of 3, this method will return 5
59 60 61 |
# File 'lib/active_cart/cart.rb', line 59 def quantity @storage_engine.quantity end |
#remove_from_cart(item, quantity = 1) ⇒ Object
Removes an item (or a quantity of that item) from the cart. If final total is 0, the item will be removed from the cart
74 75 76 |
# File 'lib/active_cart/cart.rb', line 74 def remove_from_cart(item, quantity = 1) @storage_engine.remove_from_cart(item, quantity) end |
#sub_total ⇒ Object
Returns the subtotal of cart, which is effectively the total of all the items multiplied by their quantites. Does NOT include order_totals
64 65 66 |
# File 'lib/active_cart/cart.rb', line 64 def sub_total @storage_engine.sub_total end |
#total ⇒ Object
Returns the total of the cart. THis includes all the order_total calculations
79 80 81 |
# File 'lib/active_cart/cart.rb', line 79 def total sub_total + order_total_calculators.total end |