Class: Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/commands-lite/base.rb

Overview

Commands/Commander registry

Class Method Summary collapse

Class Method Details

.commandsObject

todo/check: use Commander/CommandIndex,Registry,… or such - why? why not?



90
91
92
# File 'lib/commands-lite/base.rb', line 90

def self.commands  ## todo/check: change to registry or such - why? why not?
  @@register ||= {}
end

.register(klass) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
# File 'lib/commands-lite/base.rb', line 94

def self.register( klass )
  raise ArgumentError, "class MUST be a Command"   unless klass.ancestors.include?( Command )

  h = commands
  h[ klass.command_name] = klass
  h
end

.run(args = []) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/commands-lite/base.rb', line 103

def self.run( args=[] )
  command_name = args.shift

  ## 1) downcase  e.g. GithubStats
  ## 2) remove - to _   ## treat them the same e.g. github-stats => github_stats
  command_name = command_name
                   .gsub( /[_-]/, '' )
                   .downcase

  command = commands[ command_name ]
  if command.nil?
    puts "!! ERROR: no command definition found for >#{command_name}<; known commands include:"
    pp commands
    exit 1
  end

  command.run( args )
end