Class: Google4R::Checkout::ShoppingCart
- Inherits:
-
Object
- Object
- Google4R::Checkout::ShoppingCart
- Defined in:
- lib/google4r/checkout/shared.rb
Overview
ShoppingCart instances are containers for Item instances. You can add Items to the class using #create_item (see the documentation of this method for an example).
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
You can set the
time with this property. -
#items ⇒ Object
readonly
The items in the cart.
-
#owner ⇒ Object
readonly
The owner of this cart.
-
#private_data ⇒ Object
You can set almost arbitrary data into the cart using this method.
Class Method Summary collapse
-
.create_from_element(element, owner) ⇒ Object
Creates a new ShoppingCart object from a REXML::Element object.
Instance Method Summary collapse
-
#create_item {|item| ... } ⇒ Object
Use this method to add a new item to the cart.
-
#initialize(owner) ⇒ ShoppingCart
constructor
Initialize a new ShoppingCart with an empty Array for the items.
Constructor Details
#initialize(owner) ⇒ ShoppingCart
Initialize a new ShoppingCart with an empty Array for the items.
115 116 117 118 |
# File 'lib/google4r/checkout/shared.rb', line 115 def initialize(owner) @owner = owner @items = Array.new end |
Instance Attribute Details
#expires_at ⇒ Object
You can set the
73 74 75 |
# File 'lib/google4r/checkout/shared.rb', line 73 def expires_at @expires_at end |
#items ⇒ Object (readonly)
The items in the cart. Do not modify this array directly but use #create_item to add items.
68 69 70 |
# File 'lib/google4r/checkout/shared.rb', line 68 def items @items end |
#owner ⇒ Object (readonly)
The owner of this cart. At the moment, this always is the CheckoutCartCommand.
64 65 66 |
# File 'lib/google4r/checkout/shared.rb', line 64 def owner @owner end |
#private_data ⇒ Object
You can set almost arbitrary data into the cart using this method.
The data will be converted to XML in the following way: The keys are converted to tag names (whitespace becomes "-", all chars not matching /[a-zA-Z0-9-_])/ will be removed.
If a value is an array then the key for this value will be used as the tag name for each of the arrays's entries.
Arrays will be flattened before it is processed.
Example
cart.private_data = { 'foo' => { 'bar' => 'baz' } })
# will produce the following XML
cart.private_data = { 'foo' => [ { 'bar' => 'baz' }, "d'oh", 2 ] }
# will produce the following XML
106 107 108 |
# File 'lib/google4r/checkout/shared.rb', line 106 def private_data @private_data end |
Class Method Details
.create_from_element(element, owner) ⇒ Object
Creates a new ShoppingCart object from a REXML::Element object.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/google4r/checkout/shared.rb', line 157 def self.create_from_element(element, owner) result = ShoppingCart.new(owner) text = element.elements['cart-expiration/good-until-date'].text rescue nil result.expires_at = Time.parse(text) unless text.nil? data_element = element.elements['merchant-private-data'] value = PrivateDataParser.element_to_value(data_element) unless data_element.nil? result.private_data = value unless value.nil? element.elements.each('items/item') do |item_element| result.items << Item.create_from_element(item_element, result) end return result end |
Instance Method Details
#create_item {|item| ... } ⇒ Object
Use this method to add a new item to the cart. If you use a block with this method then the block will be given the new item. The new item will be returned in any case.
Passing a block is the preferred way of using this method.
Example
# Using a block (preferred).
cart = ShoppingCart.new
cart.create_item do |item|
item.name = "Dry Food Pack"
item.description = "A pack of highly nutritious..."
item.unit_price = Money.new(3500, "USD") # $35.00
item.quantity = 1
end
# Not using a block.
cart = ShoppingCart.new
item = cart.create_item
item.name = "Dry Food Pack"
item.description = "A pack of highly nutritious..."
item.unit_price = Money.new(3500, "USD") # $35.00
item.quantity = 1
146 147 148 149 150 151 152 153 154 |
# File 'lib/google4r/checkout/shared.rb', line 146 def create_item(&block) item = Item.new(self) @items << item # Pass the newly generated item to the given block to set its attributes. yield(item) if block_given? return item end |