Class: Opensteam::ShoppingCart::Cart
- Inherits:
-
Object
- Object
- Opensteam::ShoppingCart::Cart
- Defined in:
- lib/opensteam/shopping_cart.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
cart_items.
Instance Method Summary collapse
-
#add(yamlid) ⇒ Object
add an item to cart or increment its quantity.
-
#del(opts = {}) ⇒ Object
delete item from cart (:cart_item_id or :yamlid ).
-
#get(opts = {}) ⇒ Object
get item from cart get( :cart_itemd_id => 1 ) # get item by internal array id get( :yamlid => 3 ) # get item by Inventory-id.
-
#initialize ⇒ Cart
constructor
intialize items array.
-
#set_quantity(id, q) ⇒ Object
set quantity for item (by internal array-id).
-
#total_price ⇒ Object
calculate total price.
Constructor Details
#initialize ⇒ Cart
intialize items array
195 196 197 |
# File 'lib/opensteam/shopping_cart.rb', line 195 def initialize @items = [] end |
Instance Attribute Details
#items ⇒ Object
cart_items
191 192 193 |
# File 'lib/opensteam/shopping_cart.rb', line 191 def items @items end |
Instance Method Details
#add(yamlid) ⇒ Object
add an item to cart or increment its quantity
223 224 225 226 227 228 229 230 231 |
# File 'lib/opensteam/shopping_cart.rb', line 223 def add( yamlid ) item = get( :yamlid => yamlid ) if item.nil? @items << CartItem.new( yamlid ) return true else return item.incr end end |
#del(opts = {}) ⇒ Object
delete item from cart (:cart_item_id or :yamlid )
253 254 255 256 257 258 259 |
# File 'lib/opensteam/shopping_cart.rb', line 253 def del( opts = {} ) if opts.has_key?( :yamlid ) @items.delete( get( :yamlid => opts[:yamlid] ) ) elsif opts.has_key?( :cart_item_id ) @items.delete_at( opts[:cart_item_id].to_i ) end end |
#get(opts = {}) ⇒ Object
get item from cart
get( :cart_itemd_id => 1 ) # get item by internal array id
get( :yamlid => 3 ) # get item by Inventory-id
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/opensteam/shopping_cart.rb', line 205 def get( opts = {} ) if opts.has_key? :yamlid @items.each do |v| return v if v.yamlid == opts[:yamlid] end elsif opts.has_key? :cart_item_id return @items[ opts[:cart_item_id].to_i ] end return nil end |
#set_quantity(id, q) ⇒ Object
set quantity for item (by internal array-id)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/opensteam/shopping_cart.rb', line 236 def set_quantity( id, q ) item = @items[ id.to_i ] return nil if item.nil? q = q.to_i if q == 0 @items.delete_at( id.to_i ) elsif q > 0 item.quantity = ( item.inventory.storage < q ) ? item.inventory.storage : q end item end |
#total_price ⇒ Object
calculate total price
264 265 266 |
# File 'lib/opensteam/shopping_cart.rb', line 264 def total_price @items.collect { |x| x.price * x.quantity }.inject(&:+) end |