Class: CLI::Kit::CommandRegistry

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

Defined Under Namespace

Modules: ContextualResolver, NullContextualResolver

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#aliasesObject (readonly)

: Hash[String, String]



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

def aliases
  @aliases
end

#commandsObject (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_namesObject

: -> 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

Returns:

  • (Boolean)


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_commandsObject

: -> 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