Class: Dracula::Namespace
- Inherits:
-
Object
- Object
- Dracula::Namespace
- Defined in:
- lib/dracula/namespace.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#subcommands ⇒ Object
Returns the value of attribute subcommands.
Instance Method Summary collapse
- #add_command(command) ⇒ Object
- #add_subcommand(subcommand) ⇒ Object
- #dispatch(route, params, action = :run) ⇒ Object
- #help ⇒ Object
-
#initialize(klass) ⇒ Namespace
constructor
A new instance of Namespace.
- #prefix ⇒ Object
- #run(params) ⇒ Object
- #top_level? ⇒ Boolean
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
#commands ⇒ Object
Returns the value of attribute commands.
6 7 8 |
# File 'lib/dracula/namespace.rb', line 6 def commands @commands end |
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/dracula/namespace.rb', line 5 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/dracula/namespace.rb', line 4 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
8 9 10 |
# File 'lib/dracula/namespace.rb', line 8 def parent @parent end |
#subcommands ⇒ Object
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 |
#help ⇒ Object
58 59 60 |
# File 'lib/dracula/namespace.rb', line 58 def help NamespaceHelp.new(self).show end |
#prefix ⇒ Object
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
54 55 56 |
# File 'lib/dracula/namespace.rb', line 54 def top_level? prefix == "" end |