Class Opensteam::ShoppingCart::Cart
In: lib/opensteam/shopping_cart.rb
Parent: Object

Methods

add   del   get   new   set_quantity   total_price  

Attributes

items  [RW]  cart_items

Public Class methods

intialize items array

[Source]

     # File lib/opensteam/shopping_cart.rb, line 213
213:       def initialize
214:         @items = []
215:       end

Public Instance methods

add an item to cart or increment its quantity

[Source]

     # File lib/opensteam/shopping_cart.rb, line 241
241:       def add( yamlid )
242:         item = get( :yamlid => yamlid )
243:         if item.nil?
244:           @items << CartItem.new( yamlid )
245:           return true
246:         else
247:           return item.incr
248:         end
249:       end

delete item from cart (:cart_item_id or :yamlid )

[Source]

     # File lib/opensteam/shopping_cart.rb, line 271
271:       def del( opts = {} )
272:         if opts.has_key?( :yamlid )
273:           @items.delete( get( :yamlid => opts[:yamlid] ) )
274:         elsif opts.has_key?( :cart_item_id )
275:           @items.delete_at( opts[:cart_item_id].to_i )
276:         end
277:       end

get item from cart

  get( :cart_itemd_id => 1 ) # get item by internal array id
  get( :yamlid => 3 ) # get item by Inventory-id

[Source]

     # File lib/opensteam/shopping_cart.rb, line 223
223:       def get( opts = {} )
224: 
225:         if opts.has_key? :yamlid
226:           @items.each do |v|
227:             return v if v.yamlid == opts[:yamlid]
228:           end
229:                                         
230:         elsif opts.has_key? :cart_item_id
231:           return @items[ opts[:cart_item_id].to_i ]
232:         end
233:                                 
234:         return nil
235:                 
236:       end

set quantity for item (by internal array-id)

[Source]

     # File lib/opensteam/shopping_cart.rb, line 254
254:       def set_quantity( id, q )
255:         item = @items[ id.to_i ]
256:                                 
257:         return nil if item.nil?
258:                                 
259:         q = q.to_i
260:         if q == 0
261:           @items.delete_at( id.to_i )
262:         elsif q > 0
263:           item.quantity = ( item.inventory.storage < q ) ? item.inventory.storage : q 
264:         end
265:                                 
266:         item
267:       end

calculate total price

[Source]

     # File lib/opensteam/shopping_cart.rb, line 282
282:       def total_price
283:         @items.collect { |x| x.price * x.quantity }.inject(&:+)
284:       end

[Validate]