class Dracula::Namespace

Attributes

commands[RW]
description[RW]
name[RW]
parent[RW]
subcommands[RW]

Public Class Methods

new(klass) click to toggle source
# File lib/dracula/namespace.rb, line 10
def initialize(klass)
  @klass = klass
  @commands = []
  @subcommands = []
  @parent = []
end

Public Instance Methods

add_command(command) click to toggle source
# File lib/dracula/namespace.rb, line 62
def add_command(command)
  @commands << command
end
add_subcommand(subcommand) click to toggle source
# File lib/dracula/namespace.rb, line 66
def add_subcommand(subcommand)
  @subcommands << subcommand
end
dispatch(route, params, action = :run) click to toggle source
# File lib/dracula/namespace.rb, line 17
def dispatch(route, params, action = :run)
  case route.size
  when 0 then
    action == :run ? run(params) : help
  when 1 then
    handler = commands.find { |c| c.name == route[0] } || subcommands.find { |c| c.name == route[0] }

    if handler
      action == :run ? handler.run(params) : handler.help
    else
      puts Dracula::UI.error("Command not found")
      puts ""
      help
      exit(1)
    end
  else
    handler = subcommands.find { |c| c.name == route[0] }

    if handler
      handler.dispatch(route[1..-1], params, action)
    else
      puts Dracula::UI.error("Command not found #{prefix}#{Dracula::UI.danger(route.join(":"))}")
      puts ""
      help
      exit(1)
    end
  end
end
help() click to toggle source
# File lib/dracula/namespace.rb, line 58
def help
  NamespaceHelp.new(self).show
end
prefix() click to toggle source
# File lib/dracula/namespace.rb, line 50
def prefix
  name ? "#{parent.prefix}#{name}:" : ""
end
run(params) click to toggle source
# File lib/dracula/namespace.rb, line 46
def run(params)
  help
end
top_level?() click to toggle source
# File lib/dracula/namespace.rb, line 54
def top_level?
  prefix == ""
end