lib/signore/executable.rb in signore-0.1.2 vs lib/signore/executable.rb in signore-0.2.0
- old
+ new
@@ -1,47 +1,69 @@
-# encoding: UTF-8
-
module Signore class Executable
-
- def initialize args = ARGV, db_class = Database
- opts = Trollop.options args do
- opt :database, 'Location of the signature database', default: ENV.fetch('XDG_DATA_HOME') { File.expand_path '~/.local/share' } + '/signore/signatures.yml'
+ def initialize args = ARGV, db_factory: Database
+ @settings = settings_from args
+ @db = db_factory.new settings.db_path
+ unless %w[prego pronto].include? settings.action
+ abort 'usage: signore prego|pronto [tag, …]'
end
- Trollop.die 'usage: signore prego|pronto [label, …]' unless ['prego', 'pronto'].include? args.first
-
- @db = db_class.new opts[:database]
-
- @action = args.shift
-
- @no_tags, @tags = args.partition { |tag| tag.start_with? '~' }
- @no_tags.map! { |tag| tag[1..-1] }
end
- def run input = $stdin
- case @action
- when 'prego'
- sig = @db.find tags: @tags, no_tags: @no_tags
- when 'pronto'
- params = get_params input
- sig = Signature.new params[:text], params[:author], params[:source], params[:subject], @tags
- @db << sig
- end
-
+ def run(input: $stdin)
+ sig = case settings.action
+ when 'prego' then handle_prego settings
+ when 'pronto' then handle_pronto input
+ end
puts sig.to_s
end
+ attr_reader :db, :settings
+ private :db, :settings
+
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|
+ def handle_prego settings
+ db.find required_tags: settings.required_tags,
+ forbidden_tags: settings.forbidden_tags
+ end
+
+ def handle_pronto input
+ params = params_from input
+ sig = Signature.new params.text, params.author, params.source,
+ params.subject, settings.required_tags
+ db << sig
+ sig
+ end
+
+ def settings_from args
+ OpenStruct.new.tap do |settings|
+ db_dir = ENV.fetch('XDG_DATA_HOME') { File.expand_path '~/.local/share' }
+ settings.db_path = "#{db_dir}/signore/signatures.yml"
+ parse_settings args, settings
+ settings.action = args.shift
+ settings.forbidden_tags, settings.required_tags = args.partition do |tag|
+ tag.start_with? '~'
+ end
+ settings.forbidden_tags.map! { |tag| tag[1..-1] }
+ end
+ end
+
+ def params_from input
+ OpenStruct.new Hash[[:text, :author, :subject, :source].map do |param|
[param, get_param(param, input)]
end].reject { |_, value| value.empty? }
end
+ def parse_settings args, settings
+ OptionParser.new do |opts|
+ opts.on '-d', '--database PATH', 'Database location' do |path|
+ settings.db_path = path
+ end
+ end.parse! args
+ end
end end