Class: Command
- Inherits:
-
Object
- Object
- Command
- Includes:
- Styles
- Defined in:
- lib/replicant/commands/command.rb
Direct Known Subclasses
AdbCommand, ClearCommand, DeviceCommand, DevicesCommand, EnvCommand, ListCommand, PackageCommand, ResetCommand, RestartCommand
Constant Summary
Constants included from Styles
Styles::CONSOLE_WIDTH, Styles::REPL_OUT, Styles::STYLES
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
Instance Method Summary collapse
-
#description ⇒ Object
subclasses override this to provide a description of their functionality.
- #execute ⇒ Object
-
#initialize(repl, args = nil, options = {}) ⇒ Command
constructor
A new instance of Command.
- #name ⇒ Object
-
#usage ⇒ Object
subclasses override this to provide a usage example.
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, = {}) @repl = repl @args = args.strip if args @options = end |
Instance Attribute Details
#args ⇒ Object (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
.all ⇒ Object
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
#description ⇒ Object
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 |
#execute ⇒ Object
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 |
#name ⇒ Object
50 51 52 |
# File 'lib/replicant/commands/command.rb', line 50 def name "!#{self.class.name.gsub("Command", "").downcase}" end |
#usage ⇒ Object
subclasses override this to provide a usage example
60 61 |
# File 'lib/replicant/commands/command.rb', line 60 def usage end |