Class: Command

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repl, args = nil, options = {}) ⇒ Command

Returns a new instance of Command.



42
43
44
45
46
# File 'lib/replicant/command.rb', line 42

def initialize(repl, args = nil, options = {})
  @repl = repl
  @args = args.strip if args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



40
41
42
# File 'lib/replicant/command.rb', line 40

def args
  @args
end

Class Method Details

.allObject



10
11
12
13
14
# File 'lib/replicant/command.rb', line 10

def self.all
  (@@subclasses - [AdbCommand, ListCommand, EnvCommand]).map do |clazz|
    clazz.new(nil)
  end
end

.inherited(subclass) ⇒ Object



5
6
7
8
# File 'lib/replicant/command.rb', line 5

def self.inherited(subclass)
  @@subclasses ||= []
  @@subclasses << subclass
end

.load(repl, command_line) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/replicant/command.rb', line 16

def self.load(repl, command_line)
  if command_line == '!'
    # load command that lists available commands
    ListCommand.new(repl)
  elsif command_line == '?'
    EnvCommand.new(repl)
  elsif command_line.start_with?('!')
    # load custom command
    command_parts = command_line[1..-1].split
    command_name = command_parts.first
    command_args = command_parts[1..-1].join(' ')
    command_class = "#{command_name.capitalize}Command"
    begin
      clazz = Object.const_get(command_class)
      clazz.new(repl, command_args)
    rescue NameError => e
      nil
    end
  else
    # forward command to ADB
    AdbCommand.new(repl, command_line.strip)
  end
end

Instance Method Details

#descriptionObject

subclasses override this to provide a description of their functionality



53
54
55
# File 'lib/replicant/command.rb', line 53

def description
  "TODO: description missing"
end

#executeObject



61
62
63
64
65
66
67
# File 'lib/replicant/command.rb', line 61

def execute
  if valid_args?
    run
  else
    output "Invalid arguments. Ex.: #{usage}"
  end
end

#nameObject



48
49
50
# File 'lib/replicant/command.rb', line 48

def name
  "!#{self.class.name.gsub("Command", "").downcase}"
end

#usageObject

subclasses override this to provide a usage example



58
59
# File 'lib/replicant/command.rb', line 58

def usage
end