Project

the_cart

0.0
No commit activity in last 3 years
No release in over 3 years
Online cart implementation in redis
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
>= 0

Runtime

>= 0
 Project Readme

TheCart

TheCart is an implementation of a cart that utilizes Redis's awesomeness.

Installation

Add this line to your application's Gemfile:

gem 'the_cart'

And then execute:

$ bundle

Or install it yourself as:

$ gem install the_cart

Configuration

create an initializer in config/initializers/the_cart.rb:

TheCart.configure do |c|
  #...
end

The following options exist:

  • redis: an existing Redis connection, defaults to a Redis.new instance.

Usage

TheCart consists of two main actors, Shopper and Item.

Shopper

To add shopping abilities to a class, add the following:

  include TheCart::Shopper

and the cartify directive:

  cartify cart_expires_in: 12

cart_expires_in lets TheCart know in how many hours the cart key in redis should be expired, defaults to 24 hours.

Item

To allow the shopper to add items to the cart, all classes that can be added should include the following:

  include TheCart::Item

and the following configuration:

  cartify_item track: [field1, field2,...], price_field: :price

Those track fields/attributes/methods return values will be cached in Redis after everytime you save the item instance, note that your ORM needs to implement after_save since TheCart utilizes those callbacks to update the cached data from Redis.

The id attribute will automatically be cached.

price_field is the name of the attribute / method that returns the item price, defaults to :price

Examples

Listing items in a cart

@user.cart # => returns a hash of the tracked item fields currently cached, with quantity

Adding an item

@user.add_item_to_cart(@product)

Removing an item

@user.remove_item_from_cart(@product)

Count items in cart

@user.cart_count #=> will return the actual item count, quantity considered.

Total Cost of items in cart

@user.cart_total #= 3000.0

Contributing

  1. Fork it ( http://github.com//the_cart/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request