minicart 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +57 -0
- data/Rakefile +1 -0
- data/lib/minicart.rb +2 -0
- data/lib/minicart/cart.rb +56 -0
- data/lib/minicart/version.rb +3 -0
- data/minicart.gemspec +24 -0
- data/test/test_minicart.rb +114 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
== Minicart: simple shopping cart w/o dependencies
|
2
|
+
|
3
|
+
Minicart is a simple shopping cart w/o dependecies written in Ruby
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install minicart
|
8
|
+
|
9
|
+
== Example
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'minicart'
|
13
|
+
|
14
|
+
# Create some products (just for presentation)
|
15
|
+
class Product
|
16
|
+
attr_accessor :sku, :name, :price
|
17
|
+
|
18
|
+
def initialize(sku, name, price)
|
19
|
+
@sku, @name, @price = sku, name, price
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
ipad2 = Product.new('ipad2', 'Apple Ipad 2', 500)
|
24
|
+
ipod3 = Product.new('ipod3', 'Apple Ipod 3rd Gen', 400)
|
25
|
+
iphone4 = Product.new('iph4', 'Apple Iphone 4', 800)
|
26
|
+
|
27
|
+
# You can use minicart like this
|
28
|
+
cart = Minicart::Cart.new
|
29
|
+
ipod3_id = cart.add_product ipod3
|
30
|
+
iphone4_id = cart.add_product iphone4
|
31
|
+
ipad2_id = cart.add_product ipad2
|
32
|
+
cart.add_product ipod3
|
33
|
+
|
34
|
+
cart.remove ipad2_id
|
35
|
+
cart.update_quantity(iphone4_id, 5)
|
36
|
+
|
37
|
+
# check result
|
38
|
+
cart.count_items # => 7
|
39
|
+
cart.items.map{ |item| item.product.sku } # => ["ipod3", "iph4"]
|
40
|
+
cart.item(iphone4_id).quantity # => 5
|
41
|
+
cart.item(ipod3_id).product.price # => 400
|
42
|
+
|
43
|
+
# You can store any object in cart
|
44
|
+
# for example: hashes or strings
|
45
|
+
cart = Minicart::Cart.new
|
46
|
+
|
47
|
+
id1 = cart.add_product({'name'=>'Windows', 'price'=>100})
|
48
|
+
cart.item(id1).product['price'] # => 100
|
49
|
+
|
50
|
+
id2 = cart.add_product 'Love Linux'
|
51
|
+
cart.item(id2).product # => "Love Linux"
|
52
|
+
|
53
|
+
cart.count_items # => 2
|
54
|
+
|
55
|
+
== Author
|
56
|
+
|
57
|
+
Alex Boreysha gorizvezda@gmail.com
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/minicart.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Minicart
|
2
|
+
class Cart
|
3
|
+
# - cart is a collection of items
|
4
|
+
# - item is a product, quantity and id of item
|
5
|
+
|
6
|
+
class Item
|
7
|
+
attr_accessor :id, :product, :quantity
|
8
|
+
|
9
|
+
def initialize(product, quantity = 1, id = self.object_id)
|
10
|
+
@product, @quantity, @id = product, quantity, id
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@items = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def items
|
19
|
+
@items
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_product(product)
|
23
|
+
if item = item_from_product(product)
|
24
|
+
update_quantity(item.id, item.quantity + 1)
|
25
|
+
else
|
26
|
+
@items << item = Item.new(product)
|
27
|
+
end
|
28
|
+
item.id
|
29
|
+
end
|
30
|
+
|
31
|
+
def item(item_id)
|
32
|
+
@items.detect { |item| item.id == item_id }
|
33
|
+
end
|
34
|
+
|
35
|
+
def remove(item_id)
|
36
|
+
@items.delete_if { |item| item.id == item_id }
|
37
|
+
end
|
38
|
+
|
39
|
+
def update_quantity(item_id, quantity)
|
40
|
+
@items.map { |item| item.quantity = quantity if item.id == item_id }
|
41
|
+
end
|
42
|
+
|
43
|
+
def count_items
|
44
|
+
items.inject(0) { |sum, item| sum += item.quantity }
|
45
|
+
end
|
46
|
+
|
47
|
+
def clear
|
48
|
+
@items.clear
|
49
|
+
end
|
50
|
+
|
51
|
+
def item_from_product(product)
|
52
|
+
@items.detect { |item| item.product == product }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
data/minicart.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "minicart/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "minicart"
|
7
|
+
s.version = Minicart::VERSION
|
8
|
+
s.authors = ["Alexander Boreysha"]
|
9
|
+
s.email = ["gorizvezda@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Minimal shopping cart}
|
12
|
+
s.description = %q{This is minimal shopping cart w/o dependencies}
|
13
|
+
|
14
|
+
s.rubyforge_project = "minicart"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.extra_rdoc_files = %w[README.rdoc]
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
require 'minicart/cart'
|
4
|
+
|
5
|
+
describe 'Cart' do
|
6
|
+
before do
|
7
|
+
@cart = Minicart::Cart.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def non_empty_cart
|
11
|
+
cart = Minicart::Cart.new
|
12
|
+
cart.add_product 'product1'
|
13
|
+
cart.add_product 'product3'
|
14
|
+
cart.add_product 'product2'
|
15
|
+
cart.add_product 'product3'
|
16
|
+
cart
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'create cart' do
|
20
|
+
assert @cart
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'add item to cart' do
|
24
|
+
assert @cart.add_product('super product')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'count items' do
|
28
|
+
assert_equal 0, @cart.count_items
|
29
|
+
assert_equal 4, non_empty_cart.count_items
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'get item by id' do
|
33
|
+
cart = non_empty_cart
|
34
|
+
item = cart.items[1]
|
35
|
+
assert_equal 'product3', cart.item(item.id).product
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'clear cart content' do
|
39
|
+
assert_equal [], @cart.items
|
40
|
+
@cart.add_product 'some product'
|
41
|
+
assert_equal 1, @cart.count_items
|
42
|
+
refute_equal [], @cart.items
|
43
|
+
@cart.clear
|
44
|
+
assert_equal [], @cart.items
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'remove product from cart' do
|
48
|
+
cart = non_empty_cart
|
49
|
+
item_id = cart.items[2].id
|
50
|
+
assert cart.item(item_id)
|
51
|
+
cart.remove(item_id)
|
52
|
+
refute cart.item(item_id)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'increment quantity for adding same product' do
|
56
|
+
item_id = @cart.add_product 'popular product'
|
57
|
+
@cart.add_product 'popular product'
|
58
|
+
assert_equal 2, @cart.item(item_id).quantity
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'add item to cart return item_id' do
|
62
|
+
assert_equal @cart.add_product('some product'), @cart.items[0].id
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'set quantity of item in shopping cart' do
|
66
|
+
cart = non_empty_cart
|
67
|
+
item = cart.items[0]
|
68
|
+
assert_equal 1, item.quantity
|
69
|
+
cart.update_quantity(item.id, 99)
|
70
|
+
assert_equal 99, item.quantity
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'possible usage scenario' do
|
74
|
+
class Product
|
75
|
+
attr_accessor :sku, :name, :price
|
76
|
+
|
77
|
+
def initialize(sku, name, price)
|
78
|
+
@sku, @name, @price = sku, name, price
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
ipad2 = Product.new('ipad2', 'Apple Ipad 2', 500)
|
83
|
+
ipod3 = Product.new('ipod3', 'Apple Ipod 3rd Gen', 400)
|
84
|
+
iphone4 = Product.new('iph4', 'Apple Iphone 4', 800)
|
85
|
+
|
86
|
+
cart = Minicart::Cart.new
|
87
|
+
ipod3_id = cart.add_product ipod3
|
88
|
+
iphone4_id = cart.add_product iphone4
|
89
|
+
ipad2_id = cart.add_product ipad2
|
90
|
+
cart.add_product ipod3
|
91
|
+
|
92
|
+
|
93
|
+
cart.remove ipad2_id
|
94
|
+
|
95
|
+
cart.update_quantity(iphone4_id, 5)
|
96
|
+
|
97
|
+
# check result
|
98
|
+
assert_equal 7, cart.count_items
|
99
|
+
assert_equal %w[ipod3 iph4], cart.items.map{ |item| item.product.sku }
|
100
|
+
assert_equal 5, cart.item(iphone4_id).quantity
|
101
|
+
assert_equal 400, cart.item(ipod3_id).product.price
|
102
|
+
|
103
|
+
# You can store any object in cart as product
|
104
|
+
# for example: hashes or strings
|
105
|
+
cart = Minicart::Cart.new
|
106
|
+
|
107
|
+
id1 = cart.add_product({'name'=>'Windows', 'price'=>100})
|
108
|
+
assert_equal 100, cart.item(id1).product['price']
|
109
|
+
|
110
|
+
id2 = cart.add_product 'Love Linux'
|
111
|
+
assert_equal 'Love Linux', cart.item(id2).product
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minicart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander Boreysha
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This is minimal shopping cart w/o dependencies
|
15
|
+
email:
|
16
|
+
- gorizvezda@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.rdoc
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- README.rdoc
|
25
|
+
- Rakefile
|
26
|
+
- lib/minicart.rb
|
27
|
+
- lib/minicart/cart.rb
|
28
|
+
- lib/minicart/version.rb
|
29
|
+
- minicart.gemspec
|
30
|
+
- test/test_minicart.rb
|
31
|
+
homepage: ''
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project: minicart
|
51
|
+
rubygems_version: 1.8.12
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Minimal shopping cart
|
55
|
+
test_files:
|
56
|
+
- test/test_minicart.rb
|
57
|
+
has_rdoc:
|