cartman 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,5 @@ language: ruby
2
2
  rvm:
3
3
  - "1.9.2"
4
4
  - "1.9.3"
5
+ - "2.0.0"
5
6
  script: bundle exec rspec spec
data/README.md CHANGED
@@ -61,7 +61,7 @@ The `Cart` object also has some handy methods that you should be aware of:
61
61
  - `remove_item(item)` - which, you guessed it, removes an item. This method takes an Item object, not a hash.
62
62
  - `contains?(Product)` - This is a biggie. It will tell you if a certain item is in the cart. And the way it works is you pass it an object, like an instance of a Product model, and it will examine the class, and the id, and look to see if it's in the cart already. This method only works if the `:id` and `:type` keys are set in the item's data hash.
63
63
  - `find(Product)` - This will return the `Item` object that represents the object passed in. It works like `contains?` and uses class, and id. It only works if the `:id` and `:type` keys are set in the item's data hash.
64
- - `items` - this returns a magic array of all the items. I call it magic because you can call on it:
64
+ - `items` - this returns a magic array of all the items. You can also pass in a type string, which will return a magic array of all the items with a matching type. I call it magic because you can call on it:
65
65
  - `each_with_object` - which will act like a regular `each` call, but the block will yield the `Item` and the object it represents by using the `:type` and `:id` keys.
66
66
  - `total` - will sum all of the `cost` return values of all of the items in the cart. For this to work, the `:unit_cost` and `:quantity` fields need to be set for all items.
67
67
  - `count` - which will give you the total number of items in the cart. Faster than `cart.items.size` because it doesn't load all of the item data from redis.
@@ -28,8 +28,13 @@ module Cartman
28
28
  touch
29
29
  end
30
30
 
31
- def items
32
- ItemCollection.new(line_item_ids.collect{ |item_id| get_item(item_id)})
31
+ def items(type=nil)
32
+ items = ItemCollection.new(line_item_ids.collect{ |item_id| get_item(item_id)})
33
+ if type
34
+ return items.select{ |item| item.type == type }
35
+ else
36
+ return items
37
+ end
33
38
  end
34
39
 
35
40
  def contains?(object)
@@ -1,3 +1,3 @@
1
1
  module Cartman
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -74,6 +74,7 @@ describe Cartman do
74
74
  before(:each) do
75
75
  cart.add_item(id: 17, type: "Bottle", name: "Bordeux", unit_cost: 92.12, quantity: 2)
76
76
  cart.add_item(id: 34, type: "Bottle", name: "Cabernet", unit_cost: 92.12, quantity: 2)
77
+ cart.add_item(id: 35, type: "GiftCard", name: "Gift Card", unit_cost: 100.00, quantity: 1)
77
78
  end
78
79
 
79
80
  it "should return an ItemCollection of Items" do
@@ -82,6 +83,16 @@ describe Cartman do
82
83
  cart.items.first.id.should eq("17")
83
84
  cart.items.first.name.should eq("Bordeux")
84
85
  end
86
+
87
+ it "should return all items in cart if no filter is given" do
88
+ cart.items.size.should eq(3)
89
+ end
90
+
91
+ it "should return a subset of the items if a filter is given" do
92
+ cart.items("Bottle").size.should eq(2)
93
+ cart.items("GiftCard").size.should eq(1)
94
+ cart.items("SomethingElse").size.should eq(0)
95
+ end
85
96
  end
86
97
 
87
98
  describe "#contains?(item)" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cartman
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-19 00:00:00.000000000 Z
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis