Module: PryCommandSetRegistry

Extended by:
Forwardable
Defined in:
lib/pry_command_set_registry.rb,
lib/pry_command_set_registry/version.rb,
lib/pry_command_set_registry/commands.rb,
lib/pry_command_set_registry/registry.rb,
lib/pry_command_set_registry/command_set.rb

Overview

The namespace and primary access point for addressing the PryCommandSetRegistry plugin. Home to the Registry singleton, the primary store of registered command sets.

Defined Under Namespace

Classes: CommandSet, Registry

Constant Summary collapse

VERSION =

The version of the PryCommandSetRegistry gem.

"0.1.2".freeze
Commands =

Default commands for interacting with PryCommandSetRegistry imported into Pry.

CommandSet.new("PryCommandSetRegistry", desc, :group => "Command Set Registry") do
  command("import-set", "Import a Pry command set") do |command_set_name|
    raise Pry::CommandError, "Provide a command set name" if command_set_name.nil?

    begin
      set = target.eval(command_set_name)
      unless set.respond_to?(:commands) && set.commands.is_a?(Hash)
        registered_set = PryCommandSetRegistry.command_set(command_set_name)
        set = registered_set if registered_set
      end
    rescue NameError
      set = PryCommandSetRegistry.command_set(command_set_name)
      ::Kernel.raise if set.nil?
    end
    _pry_.commands.import(set)
  end

  command("list-sets", "List registered command sets") do
    _pry_.output.puts "Registered Command Sets:"
    _pry_.output.puts format_command_set_listing(PryCommandSetRegistry.command_sets)
  end

  helpers do
    def format_command_set_listing(command_sets)
      return "" if command_sets.none?
      max_len = command_sets.keys.max_by(&:length).length
      sets = command_sets.map do |set_name, set|
        "  #{set_name.ljust(max_len)}  -  #{set.description}"
      end
      sets.join("\n")
    end
  end
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryPryCommandSetRegistry::Registry

The registry singleton that stores all defined command sets.



15
16
17
# File 'lib/pry_command_set_registry.rb', line 15

def registry
  @registry
end

Class Method Details

.command_set(name) ⇒ PryCommandSetRegistry::CommandSet?

Attempts to look up a registered command set with the given name. If the name starts with a colon, the colon is removed prior to lookup.

Parameters:

  • name (String, Symbol)

    The name of a registered command set to attempt to retrieve.

Returns:

See Also:



59
# File 'lib/pry_command_set_registry.rb', line 59

def_delegators :registry, :command_set, :command_sets, :define_command_set

.command_setsHash{String => PryCommandSetRegistry::CommandSet}

The Hash mapping of all registered command sets.



59
# File 'lib/pry_command_set_registry.rb', line 59

def_delegators :registry, :command_set, :command_sets, :define_command_set

.define_command_set(name, description, options = {}) { ... } ⇒ PryCommandSetRegistry::CommandSet

Helper method for defining a command set and registering it immediately. All arguments are passed directly to CommandSet#initialize to instantiate a new command set.

Examples:

Create a new CommandSet with a hello-world command

PryCommandSetRegistry.define_command_set("Examples", "Example Commands", :group => "Examples") do
  command("hello-world", "Greets the world") do
    _pry_.outputter.puts("Hello world!")
  end
end

Parameters:

  • name (String, Symbol)

    The name that should be given to the command set. The provided name will be displayed by the list-sets command and is used to identify the command set when calling the import-set command.

  • description (String)

    A description of the command set. The provided description will be displayed by the list-sets command.

  • options (Hash{Symbol=>String}) (defaults to: {})

    Optional arguments.

Options Hash (options):

  • group (String)

    A group name to apply to all commands in the command set. This group will be displayed in commands like help, however depending on the version of pry it may be sentence cased when displayed. When no group is provided, Pry will use ‘(other)` by default.

Yields:

  • The provided block is evaluated by the command set instance and is used to define commands and configure the command set in other ways.

Returns:

Raises:

  • (ArgumentError)

    if no block is given.

See Also:



59
# File 'lib/pry_command_set_registry.rb', line 59

def_delegators :registry, :command_set, :command_sets, :define_command_set