Class: Command

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

Constant Summary

Constants included from Styles

Styles::CONSOLE_WIDTH, Styles::REPL_OUT, Styles::STYLES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Styles

#create_style, #end_style, #styled_text

Constructor Details

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

Returns a new instance of Command.



44
45
46
47
48
# File 'lib/replicant/commands/command.rb', line 44

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.



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

def args
  @args
end

Class Method Details

.allObject



12
13
14
15
16
# File 'lib/replicant/commands/command.rb', line 12

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

.inherited(subclass) ⇒ Object



7
8
9
10
# File 'lib/replicant/commands/command.rb', line 7

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

.load(repl, command_line) ⇒ Object



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

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



55
56
57
# File 'lib/replicant/commands/command.rb', line 55

def description
  "TODO: description missing"
end

#executeObject



63
64
65
66
67
68
69
# File 'lib/replicant/commands/command.rb', line 63

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

#nameObject



50
51
52
# File 'lib/replicant/commands/command.rb', line 50

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

#usageObject

subclasses override this to provide a usage example



60
61
# File 'lib/replicant/commands/command.rb', line 60

def usage
end