Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
/ cart Public archive

Cart is framework agnostic imlementation of shopping cart.

License

Notifications You must be signed in to change notification settings

botanicus/cart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Cart is framework agnostic solution for shopping cart. There are two existing imlementations. First is Cart::Simple which is just basic cart which can store only ID of products without any metadata. The second is Cart::Advanced and it is good when you have not just product, but also some metadata as size or color of product etc.

Initialization

Cart::Simple

require "cart/simple"
Cart.product_model = Product

Cart::Advanced

require "cart/advanced"
Cart.metadata_model = OrderItem

Metadata model must respond to serialize object method and deserialize class method for storing and restoring data:

class OrderItem
  include DataMapper::Resource
  class << self
    def deserialize(data)
      self.new(YAML::load(data))
    end
  end

  def serialize
    {:size => self.size, :product_id => self.product.id}.to_yaml
  end
end

Logger

Cart is just simple API for your favorite framework, so it haven’t any logger itself and it just use the logger of your framework. The only thing you need is setup it:

# whatever what respond to debug method
Cart.logger = Merb.logger

Maybe you would like to hook the logger, for example add information that the message comes from cart or maybe you just use framework which hasn’t any logger at all. In this case you can set logger to lambda or whatever callable object:

# or whatever what respond to to_proc method
Cart.logger = method(:puts)
Cart.logger = lambda { |msg| puts(msg) }

Usage

class Application < Merb::Controller
  # it must be deserialized in each request,
  # even with memory session store
  before :initialize_cart
  
  def initialize_cart
    @cart = cookies[:cart] ? Cart.load(cookies[:cart]) : Cart.new
  end

  def save_cart
    cookies[:cart] = @cart.save
  end

  def reset_cart
    cookies.delete(:cart)
    @cart = Cart.new
  end

  def add_to_cart(order_item)
    self.initialize_cart unless @cart
    @cart.add(order_item)
    self.save_cart
  end

  # Remove all the items with responding product and inverted values from cart
  def remove_from_cart(order_item)
    @cart.remove(order_item)
    self.save_cart
  end
end

About

Cart is framework agnostic imlementation of shopping cart.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages