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 <cart-expiration> time with this property.
-
#items ⇒ Object
readonly
The items in the cart.
-
#owner ⇒ Object
readonly
The onwer 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.
113 114 115 116 |
# File 'lib/google4r/checkout/shared.rb', line 113 def initialize(owner) @owner = owner @items = Array.new end |
Instance Attribute Details
#expires_at ⇒ Object
You can set the <cart-expiration> time with this property. If left unset then the tag will not be generated and the cart will never expire.
71 72 73 |
# File 'lib/google4r/checkout/shared.rb', line 71 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.
66 67 68 |
# File 'lib/google4r/checkout/shared.rb', line 66 def items @items end |
#owner ⇒ Object (readonly)
The onwer of this cart. At the moment, this always is the CheckoutCartCommand.
62 63 64 |
# File 'lib/google4r/checkout/shared.rb', line 62 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
<foo>
<bar>baz</bar>
</foo>
cart.private_data = { 'foo' => [ { 'bar' => 'baz' }, "d'oh", 2 ] }
# will produce the following XML
<foo>
<bar>baz</bar>
</foo>
<foo>d&</foo>
<foo>2</foo>
104 105 106 |
# File 'lib/google4r/checkout/shared.rb', line 104 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.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/google4r/checkout/shared.rb', line 155 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
144 145 146 147 148 149 150 151 152 |
# File 'lib/google4r/checkout/shared.rb', line 144 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 |