Class: Cue::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cue/command/base.rb

Direct Known Subclasses

Add, Clear, Delete, List, Show, Toggle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/cue/command/base.rb', line 9

def options
  @options
end

Instance Method Details

#find_item(key_prefix) ⇒ Object



35
36
37
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

#nargsObject



39
40
41
# File 'lib/cue/command/base.rb', line 39

def nargs
  0
end

#parserObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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