Class: Google4R::Checkout::Item
- Inherits:
-
Object
- Object
- Google4R::Checkout::Item
- Defined in:
- lib/google4r/checkout/shared.rb
Overview
An Item object represents a line of goods in the shopping cart/receipt.
You should never initialize them directly but use ShoppingCart#create_item instead.
Note that you have to create/set the tax tables for the owner of the cart in which the item is before you can set the tax_table attribute.
Direct Known Subclasses
Defined Under Namespace
Classes: DigitalContent, Subscription
Instance Attribute Summary collapse
-
#description ⇒ Object
The description of the cart item (string, required).
-
#digital_content ⇒ Object
readonly
DigitalContent information for this item.
-
#id ⇒ Object
Optional string value that is used to store the item's id (defined by the merchant) in the cart.
-
#name ⇒ Object
The name of the cart item (string, required).
-
#private_data ⇒ Object
Optional hash value that is used to store the item's id (defined by the merchant) in the cart.
-
#quantity ⇒ Object
Number of units that this item represents (integer, required).
-
#shopping_cart ⇒ Object
readonly
The cart that this item belongs to.
-
#subscription ⇒ Object
readonly
Subscription information for this item.
-
#tax_table ⇒ Object
The tax table to use for this item.
-
#unit_price ⇒ Object
The price for one unit of the given good (Money instance, required).
-
#weight ⇒ Object
The weigth of the cart item (Weight, required when carrier calculated shipping is used).
Class Method Summary collapse
-
.create_from_element(element, shopping_cart) ⇒ Object
Creates a new Item object from a REXML::Element object.
Instance Method Summary collapse
- #create_digital_content(digital_content = nil, &block) ⇒ Object
- #create_subscription(subscription = nil, &block) ⇒ Object
-
#initialize(shopping_cart) ⇒ Item
constructor
Create a new Item in the given Cart.
Constructor Details
#initialize(shopping_cart) ⇒ Item
Create a new Item in the given Cart. You should not instantize this class directly but use Cart#create_item instead.
291 292 293 |
# File 'lib/google4r/checkout/shared.rb', line 291 def initialize(shopping_cart) @shopping_cart = shopping_cart end |
Instance Attribute Details
#description ⇒ Object
The description of the cart item (string, required).
190 191 192 |
# File 'lib/google4r/checkout/shared.rb', line 190 def description @description end |
#digital_content ⇒ Object (readonly)
DigitalContent information for this item. Optional.
250 251 252 |
# File 'lib/google4r/checkout/shared.rb', line 250 def digital_content @digital_content end |
#id ⇒ Object
Optional string value that is used to store the item's id (defined by the merchant)
in the cart. Serialized to
221 222 223 |
# File 'lib/google4r/checkout/shared.rb', line 221 def id @id end |
#name ⇒ Object
The name of the cart item (string, required).
187 188 189 |
# File 'lib/google4r/checkout/shared.rb', line 187 def name @name end |
#private_data ⇒ Object
Optional hash value that is used to store the item's id (defined by the merchant)
in the cart. Serialized to
Must be a Hash. See ShoppingCart#private_data on how the serialization to XML is done.
229 230 231 |
# File 'lib/google4r/checkout/shared.rb', line 229 def private_data @private_data end |
#quantity ⇒ Object
Number of units that this item represents (integer, required).
217 218 219 |
# File 'lib/google4r/checkout/shared.rb', line 217 def quantity @quantity end |
#shopping_cart ⇒ Object (readonly)
The cart that this item belongs to.
184 185 186 |
# File 'lib/google4r/checkout/shared.rb', line 184 def shopping_cart @shopping_cart end |
#subscription ⇒ Object (readonly)
Subscription information for this item. Optional.
270 271 272 |
# File 'lib/google4r/checkout/shared.rb', line 270 def subscription @subscription end |
#tax_table ⇒ Object
The tax table to use for this item. Optional.
238 239 240 |
# File 'lib/google4r/checkout/shared.rb', line 238 def tax_table @tax_table end |
#unit_price ⇒ Object
The price for one unit of the given good (Money instance, required).
193 194 195 |
# File 'lib/google4r/checkout/shared.rb', line 193 def unit_price @unit_price end |
#weight ⇒ Object
The weigth of the cart item (Weight, required when carrier calculated shipping is used)
207 208 209 |
# File 'lib/google4r/checkout/shared.rb', line 207 def weight @weight end |
Class Method Details
.create_from_element(element, shopping_cart) ⇒ Object
Creates a new Item object from a REXML::Element object.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/google4r/checkout/shared.rb', line 296 def self.create_from_element(element, shopping_cart) result = Item.new(shopping_cart) result.name = element.elements['item-name'].text result.description = element.elements['item-description'].text result.quantity = element.elements['quantity'].text.to_i result.id = element.elements['merchant-item-id'].text rescue nil weight_element = element.elements['item-weight'] if not weight_element.nil? result.weight = Weight.create_from_element(weight_element) end data_element = element.elements['merchant-private-item-data'] if not data_element.nil? then value = PrivateDataParser.element_to_value(data_element) result.private_data = value unless value.nil? end table_selector = element.elements['tax-table-selector'].text rescue nil if not table_selector.nil? then result.tax_table = shopping_cart.owner.tax_tables.find {|table| table.name == table_selector } end unit_price = (element.elements['unit-price'].text.to_f * 100).to_i unit_price_currency = element.elements['unit-price'].attributes['currency'] result.unit_price = Money.new(unit_price, unit_price_currency) digital_content_element = element.elements['digital-content'] if not digital_content_element.nil? result.create_digital_content(DigitalContent.create_from_element(digital_content_element)) end return result end |
Instance Method Details
#create_digital_content(digital_content = nil, &block) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/google4r/checkout/shared.rb', line 252 def create_digital_content(digital_content=nil, &block) if @digital_content.nil? if digital_content.nil? @digital_content = DigitalContent.new else @digital_content = digital_content end end if block_given? yield @digital_content end return @digital_content end |
#create_subscription(subscription = nil, &block) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/google4r/checkout/shared.rb', line 272 def create_subscription(subscription=nil, &block) if @subscription.nil? if subscription.nil? @subscription = Subscription.new else @subscription = subscription end end if block_given? yield @subscription end return @subscription end |