Class: Tesco::Groceries::BasketItem

Inherits:
Product
  • Object
show all
Defined in:
lib/tesco.rb

Overview

Assists in the modification of basketed products

TODO: Correct basket auth?

Instance Attribute Summary collapse

Attributes inherited from Product

#base_product, #cheaper_alternative, #healthier_alternative, #image_url, #max_quantity, #name, #offer, #product_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Product

#details

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



332
333
334
# File 'lib/tesco.rb', line 332

def error_message
  @error_message
end

#noteObject

Returns the value of attribute note.



332
333
334
# File 'lib/tesco.rb', line 332

def note
  @note
end

#promo_messageObject

Returns the value of attribute promo_message.



332
333
334
# File 'lib/tesco.rb', line 332

def promo_message
  @promo_message
end

#quantityObject

Returns the value of attribute quantity.



332
333
334
# File 'lib/tesco.rb', line 332

def quantity
  @quantity
end

Class Method Details

.new(basket, more) ⇒ Object

I wouldn’t mess around with this from your code, its essentially internal



334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/tesco.rb', line 334

def self.new(basket,more) # :nodoc:
  @basket = basket
  
  # With a little hackiness because Product initializes with self.new, not initialize
  basket_item = super(Product.new(basket.instance_variable_get(:@api),more['ProductId'],more))
  # Set it's instance variables
  basket_item.instance_variable_set(:@quantity,(more['BasketLineQuantity'].to_i rescue 0))
  basket_item.instance_variable_set(:@error_message,(more['BasketLineErrorMessage'] rescue "")) # TODO: Parse this
  basket_item.instance_variable_set(:@promo_message,(more['BasketLinePromoMessage'] rescue ""))
  basket_item.instance_variable_set(:@note,(more['NoteForPersonalShopper'] rescue ""))
  
  basket_item
end

Instance Method Details

#add(val = 1) ⇒ Object

Add a certain number of items to the basket



356
357
358
359
360
361
# File 'lib/tesco.rb', line 356

def add(val = 1)
  @basket.authtest
  return remove if (@quantity + val) <= 0
  __getobj__.instance_variable_get(:@api).api_request(:changebasket,:productid => self.product_id,:changequantity => val,:noteforshopper => @note)  
  @quantity
end

#drop(val = 1) ⇒ Object

Remove a certain number of items from the basket



364
365
366
367
# File 'lib/tesco.rb', line 364

def drop(val = 1)
  @basket.authtest
  add(val * -1)
end

#inspectObject



384
385
386
387
# File 'lib/tesco.rb', line 384

def inspect
  @basket.authtest
  "#{@quantity} item"<<((@quantity == 1) ? "" : "s")
end

#removeObject

Remove this item from the basket completely



379
380
381
382
# File 'lib/tesco.rb', line 379

def remove
  @basket.authtest
  @basket.remove(__getobj__)
end