Class: Refinery::Cart
- Inherits:
-
Object
- Object
- Refinery::Cart
- Defined in:
- lib/refinery/cart.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#store_name ⇒ Object
readonly
Returns the value of attribute store_name.
Instance Method Summary collapse
- #add_product(product) ⇒ Object
-
#initialize(store = "Your") ⇒ Cart
constructor
A new instance of Cart.
- #total_items ⇒ Object
- #total_price ⇒ Object
Constructor Details
#initialize(store = "Your") ⇒ Cart
Returns a new instance of Cart.
9 10 11 12 |
# File 'lib/refinery/cart.rb', line 9 def initialize( store="Your" ) @items = [] @store_name = store end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/refinery/cart.rb', line 7 def items @items end |
#store_name ⇒ Object (readonly)
Returns the value of attribute store_name.
7 8 9 |
# File 'lib/refinery/cart.rb', line 7 def store_name @store_name end |
Instance Method Details
#add_product(product) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/refinery/cart.rb', line 14 def add_product(product) current_item = @items.find {|item| item.product == product} if current_item current_item.increment_quantity else current_item = ::Refinery::CartItem.new(product) @items << current_item end current_item end |
#total_items ⇒ Object
26 27 28 |
# File 'lib/refinery/cart.rb', line 26 def total_items @items.sum { |item| item.quantity } end |
#total_price ⇒ Object
31 32 33 |
# File 'lib/refinery/cart.rb', line 31 def total_price @items.sum { |item| item.price } end |