Module: Amp::Command
- Defined in:
- lib/amp-front/dispatch/commands/base.rb,
lib/amp-front/dispatch/commands/validations.rb
Defined Under Namespace
Modules: Validations Classes: Base
Class Method Summary collapse
-
.creatable(command) ⇒ Object
return the argument if ‘new’ would succeed.
-
.create(name) {|new_class| ... } ⇒ Object
Creates a new command class and sets its name appropriately.
-
.for_name(name) ⇒ Object
Looks up the command with the given name.
-
.lookup_const(modules) ⇒ Object
See if the specified module list matches a defined constant.
-
.namespace(namespace) ⇒ Object
Runs the provided block with a base module of the given name.
Class Method Details
.creatable(command) ⇒ Object
return the argument if ‘new’ would succeed
64 65 66 |
# File 'lib/amp-front/dispatch/commands/base.rb', line 64 def self.creatable(command) command.is_a?(Class) ? command : nil end |
.create(name) {|new_class| ... } ⇒ Object
Creates a new command class and sets its name appropriately. Yields it, so it can be customized by the caller, adding options, an on_call block, and so on.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/amp-front/dispatch/commands/base.rb', line 21 def self.create(name) @current_base_module ||= Amp::Command name = name.capitalize new_class = Class.new(Base) new_class.name = name yield new_class @current_base_module.const_set(name, new_class) Amp::Help::CommandHelpEntry.new(name.downcase, new_class) new_class end |
.for_name(name) ⇒ Object
Looks up the command with the given name.
69 70 71 72 |
# File 'lib/amp-front/dispatch/commands/base.rb', line 69 def self.for_name(name) modules = name.split.map {|name| name.capitalize} creatable(lookup_const(modules)) end |
.lookup_const(modules) ⇒ Object
See if the specified module list matches a defined constant
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/amp-front/dispatch/commands/base.rb', line 51 def self.lookup_const(modules) current = Amp::Command modules.each do |module_name| if module_name =~ /^[A-Za-z0-9_]+$/ && current.const_defined?(module_name) current = current.const_get(module_name) else return current end end return current end |
.namespace(namespace) ⇒ Object
Runs the provided block with a base module of the given name. So instead of creating Amp::Command::NewCommand, this allows you to namespace created code as Amp::Command::MyModule::NewCommand, isolating it from other plugins.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/amp-front/dispatch/commands/base.rb', line 36 def self.namespace(namespace) old_namespace = @current_base_module ||= Amp::Command namespace = namespace.capitalize unless old_namespace.const_defined?(namespace) new_namespace = Module.new old_namespace.const_set(namespace, new_namespace) end @current_base_module = const_get(namespace) yield ensure @current_base_module = old_namespace end |