Permalink
Browse files

refactor

  • Loading branch information...
1 parent 0779059 commit 5a5b6108d3d80a473a476834ced44116883911a8 @markburns committed Mar 18, 2012
Showing with 32 additions and 25 deletions.
  1. +27 −21 lib/kaboom/command.rb
  2. +5 −4 test/test_command.rb
View
@@ -64,13 +64,15 @@ def overview
storage.lists.each do |list|
output " #{list.name} (#{list.items.size})"
end
- s = "You don't have anything yet! To start out, create a new list:"
- s << "\n $ boom <list-name>"
- s << "\nAnd then add something to your list!"
- s << "\n $ boom <list-name> <item-name> <item-value>"
- s << "\nYou can then grab your new item:"
- s << "\n $ boom <item-name>"
- output s if storage.lists.size == 0
+ s = <<-eof
+ You don't have anything yet! To start out, create a new list:"
+ $ boom <list-name>"
+ And then add something to your list!"
+ $ boom <list-name> <item-name> <item-value>"
+ You can then grab your new item:"
+ $ boom <item-name>"
+ eof
+ output s.gsub(/ {10}/,"") if storage.lists.size == 0
end
# Public: prints the detailed view of all your Lists and all their
@@ -155,25 +157,29 @@ def detail_list(name)
end
end
- # Public: opens the Item.
- #
- # Returns nothing.
def open(key, value)
if storage.list_exists?(key)
list = List.find(key)
if value
item = storage.items.detect { |item| item.name == value }
- output "#{cyan("Boom!")} We just opened #{yellow(Platform.open(item))} for you."
else
list.items.each { |item| Platform.open(item) }
- output "#{cyan("Boom!")} We just opened all of #{yellow(key)} for you."
+ return output "#{cyan("Boom!")} We just opened all of #{yellow(key)} for you."
end
else
item = storage.items.detect { |item| item.name == key }
- output "#{cyan("Boom!")} We just opened #{yellow(Platform.open(item))} for you."
end
+
+ return not_found key, value unless item
+ output "#{cyan("Boom!")} We just opened #{y(Platform.open(item))} for you."
+ end
+
+ def not_found major, minor=nil
+ return output "#{yellow(major)} #{red("not found")}" if minor.nil?
+ return output "#{yellow(minor)} #{red("not found in")} #{yellow(major)}"
end
+
# Public: Opens a random item
#
# Returns nothing.
@@ -197,16 +203,13 @@ def random(major)
#
# Returns nothing
def echo(major, minor)
- unless minor
- item = storage.items.detect do |item|
- item.name == major
- end
- return output "#{yellow(major)} #{red("not found")}" unless item
- else
+ if minor
list = List.find(major)
item = list.find_item(minor)
- return output "#{yellow(minor)} #{red("not found in")} #{yellow(major)}" unless item
+ else
+ item = storage.items.detect { |i| i.name == major }
end
+ return not_found major, minor unless item
output item.value
end
@@ -365,6 +368,9 @@ def r t
#
# Returns nothing.
def help
+ #this currently looks horrible in code, but nice in output.
+ #would be good to make it both
+
text = %{
#{r "BOOM snippets"} ___________________________________________________
@@ -406,7 +412,7 @@ def help
https://github.com/markburns/kaboom
}.gsub(/^ {8}/, '') # strip the first eight spaces of every line
- output text
+ output text
end
end
View
@@ -137,19 +137,19 @@ def test_nonexistent_item_access_scoped_by_list
end
def test_echo_item
- assert_match /https:\/\/github\.com/, command('echo github')
+ assert_match %r{https://github\.com}, command('echo github')
end
def test_echo_item_shorthand
- assert_match /https:\/\/github\.com/, command('e github')
+ assert_match %r{https://github\.com}, command('e github')
end
def test_echo_item_does_not_exist
- assert_match /wrong not found/, command('echo wrong')
+ assert_match /wrong.*not found/, command('echo wrong')
end
def test_echo_list_item
- assert_match /https:\/\/github\.com/, command('echo urls github')
+ assert_match %r{https://github\.com}, command('echo urls github')
end
def test_echo_list_item_does_not_exist
@@ -178,6 +178,7 @@ def test_version_switch
def test_version_long
assert_match /#{Boom::VERSION}/, command('--version')
end
+
def test_stdin_pipes
stub = Object.new
stub.stubs(:stat).returns([1,2])

0 comments on commit 5a5b610

Please sign in to comment.