shopping-list 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1 @@
1
+ Use this gem to mimic the behavior of a shopping list.
@@ -0,0 +1 @@
1
+ require 'shopping_list/shopping_list'
@@ -0,0 +1,12 @@
1
+ class Item
2
+
3
+ attr_accessor :name, :category
4
+
5
+ #When creating items, they must be given a name and a category.
6
+ def initialize(name, category)
7
+ self.name = name
8
+ self.category = category
9
+ end
10
+
11
+
12
+ end
@@ -0,0 +1,82 @@
1
+ require 'shopping_list/shopping_list_item'
2
+
3
+ class ShoppingList
4
+
5
+ attr_reader :item_list
6
+
7
+ #An empty shopping list will be an empty
8
+ #array that will hold shopping list items.
9
+ def initialize
10
+ @item_list = []
11
+ end
12
+
13
+ #Adding a shopping list item to the list
14
+ #will increment the quantity of that item
15
+ #if it is already on the list.
16
+ def add_to_list(item, note = "")
17
+ current_item = contains?(item)
18
+ if current_item
19
+ current_item.increment_quantity
20
+ else
21
+ @item_list << ShoppingListItem.new(item, note)
22
+ end
23
+ end
24
+
25
+ #Return the item if it is already in the shopping
26
+ #list.
27
+ def contains?(item)
28
+ @item_list.find { |item_on_list| item_on_list.item == item}
29
+ end
30
+
31
+ #Edit the item's note only if it is in the
32
+ #shopping list. Otherwise, return nil.
33
+ def edit_item_note(item, note)
34
+ current_item = contains?(item)
35
+ if current_item
36
+ current_item.edit_note(note)
37
+ else
38
+ nil
39
+ end
40
+ end
41
+
42
+ def clear_item_notes(item)
43
+ current_item = contains?(item)
44
+ if current_item
45
+ current_item.clear_notes()
46
+ end
47
+ end
48
+
49
+ #Change the item's quantity to the
50
+ #desired amount (qty).
51
+ #--
52
+ #TODO: Should test what happens for negative quantities.
53
+ #++
54
+ #If the desired amount is zero, then
55
+ #the item should be removed from
56
+ #the list.
57
+ def edit_item_quantity(item, qty)
58
+ current_item = contains?(item)
59
+ if current_item
60
+ current_item.edit_quantity(qty)
61
+ if qty == 0
62
+ remove_from_list(item)
63
+ end
64
+ else
65
+ nil
66
+ end
67
+ end
68
+
69
+ def remove_from_list(item)
70
+ current_item = contains?(item)
71
+ if current_item
72
+ @item_list.delete(current_item)
73
+ else
74
+ nil
75
+ end
76
+ end
77
+
78
+ def shopping_list_size
79
+ @item_list.size
80
+ end
81
+
82
+ end
@@ -0,0 +1,44 @@
1
+ class ShoppingListItem
2
+
3
+ attr_reader :item, :quantity, :note
4
+
5
+ #Create a shopping list item by
6
+ #passing in an item.
7
+ #Notes on an item do not have
8
+ #to be set, as the default is
9
+ #an empty note.
10
+ def initialize(item, note = "")
11
+ @item = item
12
+ @quantity = 1
13
+ @note = note
14
+ end
15
+
16
+ def increment_quantity
17
+ @quantity += 1
18
+ end
19
+
20
+ def edit_quantity(qty)
21
+ @quantity = qty unless qty < 0
22
+ end
23
+
24
+ #Notes on a shopping list item
25
+ #will be separated by a newline.
26
+ #If using in a web app, the newline
27
+ #will not display. If this is the
28
+ #case, the newline might be replaced
29
+ #with a line break ( <br /> ).
30
+ def edit_note(note)
31
+ if !note.empty?
32
+ if @note.empty?
33
+ @note = note
34
+ else
35
+ @note << "\n" << note
36
+ end
37
+ end
38
+ end
39
+
40
+ def clear_notes()
41
+ @note = ""
42
+ end
43
+
44
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'item'
4
+
5
+ class TestItem < Test::Unit::TestCase
6
+ def setup
7
+ @item = Item.new('Apple', 'Produce')
8
+ end
9
+
10
+ def test_item_name
11
+ assert_equal('Apple', @item.name)
12
+ end
13
+
14
+ def test_item_category
15
+ assert_equal('Produce', @item.category)
16
+ end
17
+
18
+ end
@@ -0,0 +1,36 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'item'
4
+ require 'shopping_list'
5
+
6
+ class TestShoppingList < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @shopping_list = ShoppingList.new()
10
+ @apple = Item.new('Apple', 'Produce')
11
+ @shopping_list.add_to_list(@apple, 'Granny Smith type.')
12
+ end
13
+
14
+ def test_apple_should_be_on_shopping_list
15
+ assert(@shopping_list.contains?(@apple), "Apple is not in the list!")
16
+ end
17
+
18
+ def test_shopping_list_should_be_empty
19
+ @shopping_list.remove_from_list(@apple)
20
+ assert_equal(0, @shopping_list.shopping_list_size)
21
+ end
22
+
23
+ def test_shopping_list_should_be_empty_when_quantity_is_changed_to_zero
24
+ @shopping_list.edit_item_quantity(@apple, 0)
25
+ assert_equal(0, @shopping_list.shopping_list_size)
26
+ end
27
+
28
+ def test_should_be_able_to_edit_an_item_note
29
+ assert_not_nil(@shopping_list.edit_item_note(@apple, "Test Note."))
30
+ end
31
+
32
+ def test_empty_note_will_do_nothing
33
+ assert_nil(@shopping_list.edit_item_note(@apple, ""))
34
+ end
35
+
36
+ end
@@ -0,0 +1,53 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib/shopping_list")
2
+ require 'shopping_list_item'
3
+ require 'item'
4
+ require 'test/unit'
5
+
6
+ class TestShoppingListItem < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @shopping_list_item = ShoppingListItem.new(Item.new('Apple', 'Produce'))
10
+ end
11
+
12
+ def test_quantity_equals_one
13
+ assert_equal(1, @shopping_list_item.quantity)
14
+ end
15
+
16
+ def test_quantity_did_not_change
17
+ #negative quantities are not valid, so quantity will be unaffected
18
+ old_qty = @shopping_list_item.quantity
19
+ @shopping_list_item.edit_quantity(-1)
20
+ new_qty = @shopping_list_item.quantity
21
+
22
+ assert_equal(old_qty, new_qty)
23
+ end
24
+
25
+ def test_quantity_equals_zero
26
+ assert_equal(0, @shopping_list_item.edit_quantity(0))
27
+ end
28
+
29
+ def test_quantity_equals_thirty
30
+ assert_equal(30, @shopping_list_item.edit_quantity(30))
31
+ end
32
+
33
+ def test_increment_quantity
34
+ assert_equal(2, @shopping_list_item.increment_quantity)
35
+ end
36
+
37
+ def test_adding_a_note_when_note_is_empty
38
+ @shopping_list_item.edit_note("Granny Smith Type.")
39
+ assert_equal('Granny Smith Type.', @shopping_list_item.note)
40
+ end
41
+
42
+ def test_adding_a_note_when_note_is_not_empty
43
+ @shopping_list_item.edit_note("Granny Smith Type.")
44
+ @shopping_list_item.edit_note("")
45
+ @shopping_list_item.edit_note('Produce Section.')
46
+ assert_equal("Granny Smith Type.\nProduce Section.", @shopping_list_item.note)
47
+ end
48
+
49
+ def test_notes_should_be_empty_when_cleared
50
+ assert_equal("", @shopping_list_item.clear_notes)
51
+ end
52
+
53
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopping-list
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Miguel Patino
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-02 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: chiomig@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ files:
31
+ - lib/shopping_list/item.rb
32
+ - lib/shopping_list/shopping_list.rb
33
+ - lib/shopping_list/shopping_list_item.rb
34
+ - lib/shopping_list.rb
35
+ - README
36
+ - test/test_shopping_list_item.rb
37
+ - test/test_shopping_list.rb
38
+ - test/test_item.rb
39
+ has_rdoc: true
40
+ homepage:
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.5.0
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A gem to mimic a shopping list
73
+ test_files:
74
+ - test/test_shopping_list_item.rb
75
+ - test/test_shopping_list.rb
76
+ - test/test_item.rb