Class: CLI::Kit::CommandRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/kit/command_registry.rb

Defined Under Namespace

Modules: NullContextualResolver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CommandRegistry.



22
23
24
25
26
27
# File 'lib/cli/kit/command_registry.rb', line 22

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

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



6
7
8
# File 'lib/cli/kit/command_registry.rb', line 6

def aliases
  @aliases
end

#commandsObject (readonly)

Returns the value of attribute commands.



6
7
8
# File 'lib/cli/kit/command_registry.rb', line 6

def commands
  @commands
end

Instance Method Details

#add(const, name) ⇒ Object



35
36
37
# File 'lib/cli/kit/command_registry.rb', line 35

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

#add_alias(from, to) ⇒ Object



44
45
46
# File 'lib/cli/kit/command_registry.rb', line 44

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

#command_namesObject



48
49
50
# File 'lib/cli/kit/command_registry.rb', line 48

def command_names
  @contextual_resolver.command_names + commands.keys
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cli/kit/command_registry.rb', line 52

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

#lookup_command(name) ⇒ Object



39
40
41
42
# File 'lib/cli/kit/command_registry.rb', line 39

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

#resolved_commandsObject



29
30
31
32
33
# File 'lib/cli/kit/command_registry.rb', line 29

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