class Birdwatcher::Command

Constants

ARGUMENT_SEPARATOR

Attributes

arguments[R]

Public Class Methods

auto_completion() click to toggle source
# File lib/birdwatcher/command.rb, line 41
def self.auto_completion
  []
end
auto_completion_strings() click to toggle source
# File lib/birdwatcher/command.rb, line 37
def self.auto_completion_strings
  (meta[:names] + auto_completion).uniq
end
descendants() click to toggle source
# File lib/birdwatcher/command.rb, line 29
def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end
detailed_usage() click to toggle source
# File lib/birdwatcher/command.rb, line 27
def self.detailed_usage; end
has_name?(name) click to toggle source
# File lib/birdwatcher/command.rb, line 33
def self.has_name?(name)
  meta[:names].include?(name)
end
meta() click to toggle source
# File lib/birdwatcher/command.rb, line 18
def self.meta
  @meta || fail(MetadataNotSetError, "Metadata has not been set")
end
meta=(meta) click to toggle source
# File lib/birdwatcher/command.rb, line 22
def self.meta=(meta)
  validate_metadata(meta)
  @meta = meta
end

Protected Class Methods

validate_metadata(meta) click to toggle source
# File lib/birdwatcher/command.rb, line 67
def self.validate_metadata(meta)
  fail InvalidMetadataError, "Metadata is not a hash" unless meta.is_a?(Hash)
  fail InvalidMetadataError, "Metadata is empty" if meta.empty?
  fail InvalidMetadataError, "Metadata is missing key: description" unless meta.key?(:description)
  fail InvalidMetadataError, "Metadata is missing key: names" unless meta.key?(:names)
  fail InvalidMetadataError, "Metadata is missing key: usage" unless meta.key?(:usage)
  fail InvalidMetadataError, "Metadata names is not an array" unless meta[:names].is_a?(Array)
  fail InvalidMetadataError, "Metadata names must contain at least one string" if meta[:names].empty?
  fail InvalidMetadataError, "Metadata usage is not string" unless meta[:usage].is_a?(String)
end

Public Instance Methods

execute(argument_line) click to toggle source
# File lib/birdwatcher/command.rb, line 45
def execute(argument_line)
  @arguments = argument_line.to_s.split(ARGUMENT_SEPARATOR).map { |a| a.to_s.strip }
  run
rescue => e
  error("#{e.class}".bold + ": #{e.message}")
  puts e.backtrace.join("\n")
end

Protected Instance Methods

arguments?() click to toggle source
# File lib/birdwatcher/command.rb, line 59
def arguments?
  !arguments.empty?
end
commands() click to toggle source
# File lib/birdwatcher/command.rb, line 63
def commands
  Birdwatcher::Command.descendants
end
run() click to toggle source
# File lib/birdwatcher/command.rb, line 55
def run
  fail NotImplementedError, "Commands must implement #run method"
end