Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored entry retrieval. It's a little more verbose, but more

flexible. We use EntityNotFound to raise errors from the db.
  • Loading branch information...
commit a49abc0efdfe498be42a943bea9766c3eaf0eee7 1 parent b6e5379
cloudhead authored
Showing with 12 additions and 11 deletions.
  1. +1 −1  bin/koi
  2. +11 −10 lib/koi.rb
View
2  bin/koi
@@ -26,5 +26,5 @@ OptionParser.new do |o|
end
end.parse!(args)
-Koi::Command.new(args[0] || :status, args[1] || nil, args[2] || [], options).run
+Koi::Command.new(args[0] || :status, args[1..-1], options).run
View
21 lib/koi.rb
@@ -28,7 +28,7 @@ def self.init? dir = root
end
def self.run *args
- cmd = Command.new(*args)
+ cmd = Command.new(args.first, args[1..-1])
cmd[:silent] = true
cmd.run
end
@@ -53,16 +53,15 @@ class Command
:init, :add, :list, :tag,
:done, :did, :log, :status,
:remove, :float, :sink,
- :ls, :rm, :rise, :x
+ :ls, :rm, :rise, :x, :show
]
Initializers = [:init, :add]
Special = {"!" => :done, "?" => :status, "+" => :float}
def initialize *all
- cmd, param, args, options = all
+ cmd, args, options = all
@command = Special[cmd] || cmd.to_sym
- @args = [args || []].flatten
- @param = param =~ /^\d+$/ ? param.to_i : param
+ @args = (args || []).map {|a| a =~ /^\d+$/ ? a.to_i : a }
@options = options || {}
@db = Koi.init?? Database.new(File.join(Koi.root, Path[:db])) : Database.new
end
@@ -70,15 +69,12 @@ def initialize *all
def run
if Commands.include? @command
if Koi.init? or Initializers.include? @command
- if !@param or @command == :add or @param = @db.find(@param)
- @param ||= @db.last if [:float, :sink, :rm, :tag, :done, :did].include? @command
- if send(@command, *[@param, *@args].compact.flatten)
+ send(@command, *(@args.length == 1 ? @args.first : @args)).tap do |result|
+ if result
save
else
err "error running #@command"
end
- else
- err "task wasn't found"
end
else
err "'koi' is not initialized here, please run `koi init`"
@@ -86,6 +82,8 @@ def run
else
err "#{@command} is not a valid command."
end
+ rescue Database::EntityNotFound
+ err "the koi wasn't found"
end
def []= key, val
@@ -220,6 +218,7 @@ def remove entry
class Database
include Enumerable
+ EntityNotFound = Class.new(RuntimeError)
def initialize path = nil
if @path = path
@@ -237,6 +236,8 @@ def find key
(entities.select(&:sticky?) + entities.reject(&:sticky?))[key]
else
raise ArgumentError, "key must be a String or Fixnum, but is #{key.class}"
+ end.tap do |result|
+ raise EntityNotFound if result.nil?
end
end
alias :[] find

0 comments on commit a49abc0

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