Class: Opensteam::Container::Cart

Inherits:
Base
  • Object
show all
Defined in:
lib/opensteam/container.rb

Overview

Shopping Cart implementation SubClass of Opensteam::Container::Base

Instance Method Summary collapse

Methods inherited from Base

#copy_items_from, #move_items_from, table_name

Instance Method Details

#[](id) ⇒ Object

access items as array



66
# File 'lib/opensteam/container.rb', line 66

def []( id ) ; items[id.to_i] ; end

#find_item(item) ⇒ Object

find item



90
91
92
# File 'lib/opensteam/container.rb', line 90

def find_item( item )
  Array(items).find { |i| i.item == item }
end

#index(i) ⇒ Object

access index of items



84
85
86
# File 'lib/opensteam/container.rb', line 84

def index(i)
  @items.index(i)
end

#push(item) ⇒ Object Also known as: <<, add

push/add item to container if item exists, increment quantity



70
71
72
73
74
75
76
# File 'lib/opensteam/container.rb', line 70

def push( item )
  unless( i = find_item( item ) )
    items.create( :item => item )
  else
    i.incr
  end
end

#set_quantity=(h) ⇒ Object

set quantity for items in h (hash)

container.set_quantity = { "0" => 123, "1" => 345 }

where key is the array index of items



102
103
104
# File 'lib/opensteam/container.rb', line 102

def set_quantity=(h)
  h.each_pair { |k,v| self.items[k.to_i].set_quantity(v) }
end

#total_priceObject



95
96
97
# File 'lib/opensteam/container.rb', line 95

def total_price
  items.collect(&:total_price).sum
end

#update!Object



78
79
80
# File 'lib/opensteam/container.rb', line 78

def update!
  update_tax( :country => "Austria" )
end