Class: CLI::Kit::CommandRegistry

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cli/kit/command_registry.rb

Defined Under Namespace

Modules: ContextualResolver, NullContextualResolver

Constant Summary collapse

CommandOrProc =
T.type_alias do
  T.any(T.class_of(CLI::Kit::BaseCommand), T.proc.returns(T.class_of(CLI::Kit::BaseCommand)))
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initialize(default:, contextual_resolver: NullContextualResolver) ⇒ CommandRegistry

Returns a new instance of CommandRegistry.



60
61
62
63
64
65
# File 'lib/cli/kit/command_registry.rb', line 60

def initialize(default:, contextual_resolver: NullContextualResolver)
  @commands = {}
  @aliases  = {}
  @default = default
  @contextual_resolver = contextual_resolver
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



18
19
20
# File 'lib/cli/kit/command_registry.rb', line 18

def aliases
  @aliases
end

#commandsObject (readonly)

Returns the value of attribute commands.



15
16
17
# File 'lib/cli/kit/command_registry.rb', line 15

def commands
  @commands
end

Instance Method Details

#add(const, name) ⇒ Object



75
76
77
# File 'lib/cli/kit/command_registry.rb', line 75

def add(const, name)
  commands[name] = const
end

#add_alias(from, to) ⇒ Object



86
87
88
# File 'lib/cli/kit/command_registry.rb', line 86

def add_alias(from, to)
  aliases[from] = to unless aliases[from]
end

#command_namesObject



91
92
93
# File 'lib/cli/kit/command_registry.rb', line 91

def command_names
  @contextual_resolver.command_names + commands.keys
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/cli/kit/command_registry.rb', line 96

def exist?(name)
  !resolve_command(name).first.nil?
end

#lookup_command(name) ⇒ Object



80
81
82
83
# File 'lib/cli/kit/command_registry.rb', line 80

def lookup_command(name)
  name = @default if name.to_s.empty?
  resolve_command(T.must(name))
end

#resolved_commandsObject



68
69
70
71
72
# File 'lib/cli/kit/command_registry.rb', line 68

def resolved_commands
  @commands.each_with_object({}) do |(k, v), a|
    a[k] = resolve_class(v)
  end
end