class Cue::Command::Base
Attributes
options[R]
Public Class Methods
new(args)
click to toggle source
# File lib/cue/command/base.rb, line 11 def initialize(args) @options = OpenStruct.new.tap do |opts| opts.store = Cue::Store::Redis.new end @args = parser.parse(args) configure_store check_num_arguments end
Public Instance Methods
find_item(key_prefix)
click to toggle source
# File lib/cue/command/base.rb, line 35 def find_item(key_prefix) options.store.read(find_key(key_prefix)) end
find_key(key_prefix)
click to toggle source
# File lib/cue/command/base.rb, line 21 def find_key(key_prefix) keys = options.store.keys item_ids = keys.select { |id| id.start_with?(key_prefix) } if item_ids.size > 1 parser.abort "#{key_prefix} is too ambiguous." end item_id = item_ids.first parser.abort("Couldn't find item #{key_prefix}.") unless item_id item_id end
nargs()
click to toggle source
# File lib/cue/command/base.rb, line 39 def nargs 0 end
parser()
click to toggle source
# File lib/cue/command/base.rb, line 43 def parser @parser ||= OptionParser.new do |opts| opts.banner = "Usage: #{File.basename($0)} [options]" opts.on('-s', '--store [STORE]', 'Specify the store adapter') do |store| begin require "cue/store/#{store}" klass = store.split('_').map(&:capitalize).join options.store = Cue.const_get("Store::#{klass}").new rescue LoadError opts.abort "Couldn't find the store adapter \"#{store}\"." end end end end
Private Instance Methods
check_num_arguments()
click to toggle source
# File lib/cue/command/base.rb, line 61 def check_num_arguments case nargs when Range parser.abort('Invalid number of arguments.') unless nargs.cover?(@args.size) else parser.abort('Invalid number of arguments.') if @args.size != nargs end end
config_path()
click to toggle source
# File lib/cue/command/base.rb, line 70 def config_path File.join(ENV['HOME'], '.cue', 'config.json') end
configure_redis()
click to toggle source
# File lib/cue/command/base.rb, line 74 def configure_redis get_config(:redis) do |config| Cue::Store::Redis.configure do |rconfig| opts = config.select { |k,_| [:host, :port, :password].include?(k) } rconfig.redis = Redis.new(opts) end end end
configure_store()
click to toggle source
# File lib/cue/command/base.rb, line 83 def configure_store case options.store when Cue::Store::Redis configure_redis end end
get_config(key=nil) { |json| ... }
click to toggle source
# File lib/cue/command/base.rb, line 90 def get_config(key=nil, &block) return unless File.exists?(config_path) json = JSON.parse(File.read(config_path), symbolize_names: true) key.nil? ? yield(json) : yield(json[key]) end