-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,5 @@ | ||
module PryCommandSetRegistry | ||
desc = "Commands for interacting with the Pry command set registry" | ||
# Namespace for PryCommandSetRegistry built-in commands. | ||
module PryCommandSetRegistry::Commands; end | ||
|
||
# Default commands for interacting with PryCommandSetRegistry imported into | ||
# Pry. | ||
Commands = 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 | ||
end | ||
require "pry_command_set_registry/commands/import_set_command" | ||
require "pry_command_set_registry/commands/list_sets_command" |
50 changes: 50 additions & 0 deletions
50
lib/pry_command_set_registry/commands/import_set_command.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module PryCommandSetRegistry::Commands | ||
# Pry command providing a means to import a command set into the current Pry | ||
# session. | ||
class ImportSetCommand < Pry::ClassCommand | ||
match "import-set" | ||
description "Import a Pry command set." | ||
banner <<-BANNER | ||
Usage: import-set <command_set_name> | ||
Import a Pry command set. | ||
BANNER | ||
|
||
# Attempts to lookup and import the specified command set into the current | ||
# Pry session. This command is designed to give priority to command sets | ||
# that resolve against the current binding if any exist. Efforts to resolve | ||
# the command set name proceed as follows: | ||
# | ||
# 1. Evaluate provided command set name against current binding. | ||
# a. If evaluation succeeds and the result of evaluation looks like a | ||
# command set, the result object will be imported. | ||
# b. If the evaluation succeeds and the result of the evaluation does | ||
# not look like a command set, the result is discarded. | ||
# 2. If the evaluation fails resulting in a NameError, the command will | ||
# attempt to use the given command set name to retrieve a command set | ||
# from the command set registry. | ||
# a. If a command set is found in the registry with a name matching the | ||
# given command set name, that command set is imported into the current | ||
# Pry session. | ||
# b. If no command set is found in the registry with the given command | ||
# set name, the NameError is raised. | ||
# | ||
# @raise [Pry::CommandError] if no command set name is given. | ||
# @return [void] | ||
def process(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) | ||
raise if set.nil? | ||
end | ||
_pry_.commands.import(set) | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
lib/pry_command_set_registry/commands/list_sets_command.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module PryCommandSetRegistry::Commands | ||
# Pry command providing a list of all registered command sets. | ||
class ListSetsCommand < Pry::ClassCommand | ||
match "list-sets" | ||
description "List registered command sets." | ||
banner <<-BANNER | ||
Usage: list-sets | ||
List registered command sets. | ||
BANNER | ||
|
||
# Displays a list of all registered command sets. | ||
# | ||
# @return [void] | ||
def process | ||
_pry_.output.puts "Registered Command Sets:" | ||
_pry_.output.puts format_command_set_listing(PryCommandSetRegistry.command_sets) | ||
end | ||
|
||
private | ||
|
||
# Formats the provided list of command sets in a human-friendly format. | ||
# | ||
# @param [Array<PryCommandSetRegistry::CommandSet>] A collection of command | ||
# sets. | ||
# @return [String] The human-friendly format list of the command sets. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module PryCommandSetRegistry | ||
desc = "Commands for interacting with the Pry command set registry" | ||
|
||
# Default commands for interacting with PryCommandSetRegistry imported into | ||
# Pry. | ||
CommandsCommandSet = CommandSet.new("PryCommandSetRegistry", desc, :group => "Command Set Registry") do | ||
def self.auto_import_commands | ||
[ | ||
Commands::ImportSetCommand, | ||
Commands::ListSetsCommand, | ||
] | ||
end | ||
|
||
auto_import_commands.each { |command| add_command(command) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "test_helper" | ||
|
||
class ListSetsCommandTest < PryCommandSetRegistry::TestCase | ||
include PryTestCase::CommandHelpers | ||
|
||
context "list-sets" do | ||
setup do | ||
# Reset registry | ||
clean_registry = PryCommandSetRegistry::Registry.new | ||
PryCommandSetRegistry.send(:registry=, clean_registry) | ||
end | ||
|
||
should "List no registered sets when none are registered" do | ||
output = StringIO.new | ||
command_exec_cli("list-sets", :output => output) | ||
assert_equal "Registered Command Sets:\n\n", output.string | ||
end | ||
|
||
should "List registered sets" do | ||
output = StringIO.new | ||
name = "Foo" | ||
desc = "Bar" | ||
PryCommandSetRegistry.define_command_set(name, desc) {} | ||
command_exec_cli("list-sets", :output => output) | ||
expected_output = "Registered Command Sets:\n Foo - Bar\n" | ||
assert_equal expected_output, output.string | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "test_helper" | ||
|
||
class CommandsCommandSetTest < PryCommandSetRegistry::TestCase | ||
Subject = PryCommandSetRegistry::CommandsCommandSet | ||
|
||
subject { Subject } | ||
|
||
context "commands" do | ||
Subject.auto_import_commands.each do |command| | ||
should "include #{command.name}" do | ||
assert_equal command, subject[command.match] | ||
end | ||
end | ||
end | ||
|
||
context "auto-import" do | ||
should "be imported into Pry.commands automatically" do | ||
subject.each do |match, command| | ||
assert_equal Pry.commands[match], command | ||
end | ||
end | ||
end | ||
end |