Class: CLI::Kit::CommandRegistry
- Inherits:
-
Object
- Object
- CLI::Kit::CommandRegistry
- Defined in:
- lib/cli/kit/command_registry.rb
Defined Under Namespace
Modules: ContextualResolver, NullContextualResolver
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
: Hash[String, String].
-
#commands ⇒ Object
readonly
: Hash[String, command_or_proc].
Instance Method Summary collapse
-
#add(const, name) ⇒ Object
: (command_or_proc const, String name) -> void.
-
#add_alias(from, to) ⇒ Object
: (String from, String to) -> void.
-
#command_names ⇒ Object
: -> Array.
-
#exist?(name) ⇒ Boolean
: (String name) -> bool.
-
#initialize(default:, contextual_resolver: NullContextualResolver) ⇒ CommandRegistry
constructor
: (default: String, ?contextual_resolver: ContextualResolver) -> void.
-
#lookup_command(name) ⇒ Object
: (String? name) -> [singleton(CLI::Kit::BaseCommand)?, String].
-
#resolved_commands ⇒ Object
: -> Hash[String, singleton(CLI::Kit::BaseCommand)].
Constructor Details
#initialize(default:, contextual_resolver: NullContextualResolver) ⇒ CommandRegistry
: (default: String, ?contextual_resolver: ContextualResolver) -> void
62 63 64 65 66 67 |
# File 'lib/cli/kit/command_registry.rb', line 62 def initialize(default:, contextual_resolver: NullContextualResolver) @commands = {} @aliases = {} @default = default @contextual_resolver = contextual_resolver end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
: Hash[String, String]
14 15 16 |
# File 'lib/cli/kit/command_registry.rb', line 14 def aliases @aliases end |
#commands ⇒ Object (readonly)
: Hash[String, command_or_proc]
11 12 13 |
# File 'lib/cli/kit/command_registry.rb', line 11 def commands @commands end |
Instance Method Details
#add(const, name) ⇒ Object
: (command_or_proc const, String name) -> void
77 78 79 |
# File 'lib/cli/kit/command_registry.rb', line 77 def add(const, name) commands[name] = const end |
#add_alias(from, to) ⇒ Object
: (String from, String to) -> void
90 91 92 |
# File 'lib/cli/kit/command_registry.rb', line 90 def add_alias(from, to) aliases[from] = to unless aliases[from] end |
#command_names ⇒ Object
: -> Array
95 96 97 |
# File 'lib/cli/kit/command_registry.rb', line 95 def command_names @contextual_resolver.command_names + commands.keys end |
#exist?(name) ⇒ Boolean
: (String name) -> bool
100 101 102 |
# File 'lib/cli/kit/command_registry.rb', line 100 def exist?(name) !resolve_command(name).first.nil? end |
#lookup_command(name) ⇒ Object
: (String? name) -> [singleton(CLI::Kit::BaseCommand)?, String]
82 83 84 85 86 87 |
# File 'lib/cli/kit/command_registry.rb', line 82 def lookup_command(name) name = @default if name.to_s.empty? resolve_command( name, #: as !nil ) end |
#resolved_commands ⇒ Object
: -> Hash[String, singleton(CLI::Kit::BaseCommand)]
70 71 72 73 74 |
# File 'lib/cli/kit/command_registry.rb', line 70 def resolved_commands @commands.each_with_object({}) do |(k, v), a| a[k] = resolve_class(v) end end |