module TheCart::Shopper::InstanceMethods
Public Instance Methods
add_item_to_cart(item)
click to toggle source
# File lib/the_cart/shopper.rb, line 27 def add_item_to_cart(item) if item_in_cart?(item) TheCart.redis.zincrby self.cart_id_key, 1, item.id else TheCart.redis.zadd self.cart_id_key, 1, item.id end TheCart.redis.expire self.cart_id_key, self.class.cart_configuration[:expire_cart_in].to_i end
cart()
click to toggle source
# File lib/the_cart/shopper.rb, line 13 def cart TheCart.redis.zrange(self.cart_id_key, 0, -1, {withscores: true}).map { |item| item_id, quantity = item hash = TheCart.redis.hgetall "the_cart:#{item_id}:item_details" hash["quantity"] = quantity.to_i hash } end
cart_count()
click to toggle source
# File lib/the_cart/shopper.rb, line 41 def cart_count total = 0 TheCart.redis.zrange(self.cart_id_key, 0, -1, {withscores: true}).map { |item| item_id, quantity = item total += quantity.to_i } total end
cart_total()
click to toggle source
# File lib/the_cart/shopper.rb, line 50 def cart_total total = 0 self.cart.map {|item| total += item["price"].to_f * item["quantity"].to_i } total end
clear_cart!()
click to toggle source
# File lib/the_cart/shopper.rb, line 56 def clear_cart! TheCart.redis.del self.cart_id_key end
item_in_cart?(item)
click to toggle source
# File lib/the_cart/shopper.rb, line 23 def item_in_cart?(item) TheCart.redis.zrank(self.cart_id_key, item.id) != nil end
remove_item_from_cart(item)
click to toggle source
# File lib/the_cart/shopper.rb, line 36 def remove_item_from_cart(item) TheCart.redis.zrem self.cart_id_key, item.id TheCart.redis.expire self.cart_id_key, self.class.cart_configuration[:expire_cart_in].to_i end
Protected Instance Methods
cart_id_key()
click to toggle source
# File lib/the_cart/shopper.rb, line 62 def cart_id_key "the_cart:#{self.id}:cart" end