cartman 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ *.un~
20
+ *.rdb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cartman.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Will Cosgrove
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Cartman
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cartman'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cartman
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cartman/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "cartman"
8
+ gem.version = Cartman::VERSION
9
+ gem.authors = ["Will Cosgrove"]
10
+ gem.email = ["will@willcosgrove.com"]
11
+ gem.description = %q{Cartman is a frameworke agnostic, redis-backed, shopping cart system}
12
+ gem.summary = %q{Doing shopping carts like a boss since 2012}
13
+ gem.homepage = "http://github.com/willcosgrove/cartman"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency("redis")
21
+
22
+ gem.add_development_dependency("rspec")
23
+ end
@@ -0,0 +1,15 @@
1
+ require "cartman/version"
2
+ require "cartman/configuration"
3
+ require "cartman/cart"
4
+ require 'redis'
5
+
6
+ module Cartman
7
+ module_function
8
+ def config(&block)
9
+ if block_given?
10
+ @config = Configuration.new(&block)
11
+ else
12
+ @config
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module Cartman
2
+ class Cart
3
+ CART_LINE_ITEM_ID_KEY = "cartman:line_item:id"
4
+
5
+ def initialize(uid)
6
+ @uid = uid
7
+ @@redis = Cartman::Configuration.redis
8
+ end
9
+
10
+ def add_item(options)
11
+ line_item_id = @@redis.incr CART_LINE_ITEM_ID_KEY
12
+ @@redis.mapped_hmset("cartman:line_item:#{line_item_id}", options)
13
+ @@redis.sadd key, line_item_id
14
+ end
15
+
16
+ def items
17
+ line_item_ids = @@redis.smembers key
18
+ line_item_ids.collect { |id|
19
+ @@redis.hgetall "cartman:line_item:#{id}"
20
+ }
21
+ end
22
+
23
+ private
24
+
25
+ def key
26
+ "cartman:cart:#{@uid}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ module Cartman
2
+ class Configuration
3
+ @@configuration = {}
4
+ def initialize(&block)
5
+ instance_eval &block
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ if !args.empty?
10
+ @@configuration.store(method, *args)
11
+ else
12
+ @@configuration.fetch(method)
13
+ end
14
+ end
15
+
16
+ class << self
17
+ def method_missing(method, *args, &block)
18
+ if !args.empty?
19
+ @@configuration.store(method, args[0])
20
+ else
21
+ @@configuration.fetch(method)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Cartman
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cartman do
4
+ describe Cartman::Cart do
5
+ let(:cart) { Cartman::Cart.new(1) }
6
+
7
+ before(:all) do
8
+ Cartman.config do
9
+ redis Redis.new
10
+ end
11
+ end
12
+
13
+ before(:each) do
14
+ Cartman.config.redis.flushdb
15
+ end
16
+
17
+ describe "#key" do
18
+ it "should return a proper key string" do
19
+ cart.send(:key).should eq("cartman:cart:1")
20
+ end
21
+ end
22
+
23
+ describe "#add_item" do
24
+ before(:each) do
25
+ cart.add_item(id: 17, class: "Bottle", name: "Bordeux", unit_cost: 92.12, cost: 184.24, quantity: 2)
26
+ end
27
+
28
+ it "creates a line item key" do
29
+ Cartman.config.redis.exists("cartman:line_item:1").should be_true
30
+ end
31
+ it "adds that line item key's id to the cart set" do
32
+ Cartman.config.redis.sismember(cart.send(:key), 1).should be_true
33
+ end
34
+ end
35
+
36
+ describe "#items" do
37
+ before(:each) do
38
+ cart.add_item(id: 17, class: "Bottle", name: "Bordeux", unit_cost: 92.12, cost: 184.24, quantity: 2)
39
+ cart.add_item(id: 34, class: "Bottle", name: "Cabernet", unit_cost: 92.12, cost: 184.24, quantity: 2)
40
+ end
41
+
42
+ it "should return an array of hashes" do
43
+ cart.items.class.should eq(Array)
44
+ cart.items.first.class.should eq(Hash)
45
+ cart.items.size.should eq(2)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ unless Kernel.respond_to?(:require_relative)
2
+ module Kernel
3
+ def require_relative(path)
4
+ require File.join(File.dirname(caller[0]), path.to_str)
5
+ end
6
+ end
7
+ end
8
+
9
+ require_relative "../lib/cartman"
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cartman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Will Cosgrove
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Cartman is a frameworke agnostic, redis-backed, shopping cart system
47
+ email:
48
+ - will@willcosgrove.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - cartman.gemspec
59
+ - lib/cartman.rb
60
+ - lib/cartman/cart.rb
61
+ - lib/cartman/configuration.rb
62
+ - lib/cartman/version.rb
63
+ - spec/cart_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: http://github.com/willcosgrove/cartman
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -2349574205203334648
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: -2349574205203334648
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.23
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Doing shopping carts like a boss since 2012
95
+ test_files:
96
+ - spec/cart_spec.rb
97
+ - spec/spec_helper.rb