Class: Dracula::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/dracula/namespace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Namespace

Returns a new instance of Namespace.



10
11
12
13
14
15
# File 'lib/dracula/namespace.rb', line 10

def initialize(klass)
  @klass = klass
  @commands = []
  @subcommands = []
  @parent = []
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



6
7
8
# File 'lib/dracula/namespace.rb', line 6

def commands
  @commands
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/dracula/namespace.rb', line 5

def description
  @description
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/dracula/namespace.rb', line 4

def name
  @name
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/dracula/namespace.rb', line 8

def parent
  @parent
end

#subcommandsObject

Returns the value of attribute subcommands.



7
8
9
# File 'lib/dracula/namespace.rb', line 7

def subcommands
  @subcommands
end

Instance Method Details

#add_command(command) ⇒ Object



62
63
64
# File 'lib/dracula/namespace.rb', line 62

def add_command(command)
  @commands << command
end

#add_subcommand(subcommand) ⇒ Object



66
67
68
# File 'lib/dracula/namespace.rb', line 66

def add_subcommand(subcommand)
  @subcommands << subcommand
end

#dispatch(route, params, action = :run) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

#helpObject



58
59
60
# File 'lib/dracula/namespace.rb', line 58

def help
  NamespaceHelp.new(self).show
end

#prefixObject



50
51
52
# File 'lib/dracula/namespace.rb', line 50

def prefix
  name ? "#{parent.prefix}#{name}:" : ""
end

#run(params) ⇒ Object



46
47
48
# File 'lib/dracula/namespace.rb', line 46

def run(params)
  help
end

#top_level?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/dracula/namespace.rb', line 54

def top_level?
  prefix == ""
end