lib/signore/executable.rb in signore-0.0.0 vs lib/signore/executable.rb in signore-0.1.0
- old
+ new
@@ -1,33 +1,47 @@
# encoding: UTF-8
module Signore class Executable
- def initialize args = ARGV, db = Database
+ def initialize args = ARGV, db_class = Database
opts = Trollop.options args do
- opt :database, 'Location of the signature database', default: (ENV['XDG_DATA_HOME'] or File.expand_path '~/.local/share') + '/signore/signatures.yml'
+ opt :database, 'Location of the signature database', default: ENV.fetch('XDG_DATA_HOME') { File.expand_path '~/.local/share' } + '/signore/signatures.yml'
end
Trollop.die 'usage: signore prego|pronto [label, …]' unless ['prego', 'pronto'].include? args.first
- db.load opts[:database]
+
+ @db = db_class.new opts[:database]
+
@action = args.shift
- @no_tags, @tags = args.partition { |tag| tag[0] == '~' }
+
+ @no_tags, @tags = args.partition { |tag| tag.start_with? '~' }
@no_tags.map! { |tag| tag[1..-1] }
end
- def run output = STDOUT, input = STDIN
+ def run input = $stdin
case @action
when 'prego'
- output.puts Database.find(tags: @tags, no_tags: @no_tags).display
+ sig = @db.find tags: @tags, no_tags: @no_tags
when 'pronto'
- params = Hash[[:text, :author, :subject, :source].map do |elem|
- output.puts "#{elem}?"
- value = ''
- value << input.gets until value.lines.to_a.last == "\n"
- [elem, value.rstrip]
- end].delete_if { |elem, value| value.empty? }
+ params = get_params input
sig = Signature.new params[:text], params[:author], params[:source], params[:subject], @tags
- Database.save sig
- output.puts sig.display
+ @db << sig
end
+
+ puts sig.to_s
+ end
+
+ private
+
+ def get_param param, input
+ puts "#{param}?"
+ value = ''
+ value << input.gets until value.lines.to_a.last == "\n"
+ value.strip
+ end
+
+ def get_params input
+ Hash[[:text, :author, :subject, :source].map do |param|
+ [param, get_param(param, input)]
+ end].reject { |_, value| value.empty? }
end
end end