Skip to content
Browse files

Added ability to retrieve and remove items based on index

Also added some methods like List#find_item
  • Loading branch information...
1 parent 76f9476 commit b340c71cc8e5210c252d2be4a7bbfafb38d7bb1d = committed Nov 24, 2010
Showing with 51 additions and 13 deletions.
  1. +15 −13 lib/boom/command.rb
  2. +27 −0 lib/boom/list.rb
  3. +9 −0 lib/boom/storage.rb
View
28 lib/boom/command.rb
@@ -81,8 +81,11 @@ def delegate(command, major, minor)
return search_items(command) if storage.item_exists?(command)
- if minor == 'delete' and storage.item_exists?(major)
- return item_delete(major)
+ if minor == 'delete'
+ list = storage.find_list(command)
+ if item = list.find_item(major)
+ return item_delete(command, major)
+ end
end
return list_create(command)
@@ -94,7 +97,7 @@ def delegate(command, major, minor)
#
# Returns nothing.
def list_detail(list_name)
- list = storage.lists.find { |list| list.name == list_name }
+ list = storage.find_list(list_name)
list.items.sort{ |x,y| x.name <=> y.name }.each do |item|
output " #{item.name}: #{item.value}"
end
@@ -149,26 +152,25 @@ def list_delete(name)
#
# Returns the newly created Item.
def add_item(list,name,value)
- list = storage.lists.find{|storage_list| storage_list.name == list}
+ list = storage.find_list(list)
list.add_item(Item.new(name,value))
output "Boom! \"#{name}\" in \"#{list.name}\" is \"#{value}\". Got it."
save!
end
# Public: remove a named Item.
#
- # name - the String name of the Item.
+ # list_name - the String name of the List.
+ # item_name - the String name of the Item.
#
# Example
#
- # Commands.delete_item("an-item-name")
+ # Commands.delete_item("a list name", "an-item-name")
#
# Returns nothing.
- def item_delete(name)
- storage.lists = storage.lists.each do |list|
- list.items.reject! { |item| item.name == name }
- end
- output "Boom! \"#{name}\" is gone forever."
+ def item_delete(list_name, item_name)
+ storage.find_list(list_name).delete_item(item_name)
+ output "Boom! \"#{item_name}\" is gone forever."
save!
end
@@ -194,8 +196,8 @@ def search_items(name)
#
# Returns the matching Item.
def search_list_for_item(list_name, item_name)
- list = storage.lists.find { |list| list.name == list_name }
- item = list.items.find { |item| item.name == item_name }
+ list = storage.find_list(list_name)
+ item = list.find_item(item_name)
output Clipboard.copy(item)
end
View
27 lib/boom/list.rb
@@ -33,6 +33,33 @@ def add_item(item)
@items << item
end
+
+ # Public: Retrieves an item from the list
+ #
+ # item_name - Name or array index of item
+ #
+ # Returns the item
+ def find_item(item_name)
+ if item_name =~ /\[(\d+)\]/
+ self.items[$1.to_i]
+ else
+ self.items.find { |item| item.name == item_name }
+ end
+ end
+
+ # Public: Deletes an item from the list
+ #
+ # item_name - Name or array index of item
+ #
+ def delete_item(item_name)
+ if item_name =~ /\[(\d+)\]/
+ self.items.delete_at($1.to_i)
+ else
+ self.items.reject { |item| item.name == item_name }
+ end
+ end
+
+
# Public: a Hash representation of this List.
#
# Returns a Hash of its own data and its child Items.
View
9 lib/boom/storage.rb
@@ -30,6 +30,15 @@ def initialize
# Returns nothing.
attr_writer :lists
+ # Public: Finds a list by name
+ #
+ # name - list name to find
+ #
+ # Returns the found list
+ def find_list(name)
+ @lists.find { |l| l.name == name }
+ end
+
# Public: the list of Lists in your JSON data, sorted by number of items
# descending.
#

0 comments on commit b340c71

Please sign in to comment.
Something went wrong with that request. Please try again.