Skip to content

Commit

Permalink
Convert commands to ClassCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
tdg5 committed Mar 21, 2015
1 parent 9d6c36a commit 8ddb65c
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 64 deletions.
3 changes: 2 additions & 1 deletion lib/pry_command_set_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "pry_command_set_registry/registry"
require "pry_command_set_registry/command_set"
require "pry_command_set_registry/commands"
require "pry_command_set_registry/commands_command_set"

# The namespace and primary access point for addressing the
# PryCommandSetRegistry plugin. Home to the Registry singleton, the primary
Expand Down Expand Up @@ -62,4 +63,4 @@ class << self
private_class_method :registry=
end

Pry.commands.import(PryCommandSetRegistry::Commands)
Pry.commands.import(PryCommandSetRegistry::CommandsCommandSet)
42 changes: 4 additions & 38 deletions lib/pry_command_set_registry/commands.rb
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 lib/pry_command_set_registry/commands/import_set_command.rb
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 lib/pry_command_set_registry/commands/list_sets_command.rb
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
16 changes: 16 additions & 0 deletions lib/pry_command_set_registry/commands_command_set.rb
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "test_helper"

class CommandsTest < PryCommandSetRegistry::TestCase
class ImportSetCommandTest < PryCommandSetRegistry::TestCase
include PryTestCase::CommandHelpers

context "import-set" do
Expand Down Expand Up @@ -71,28 +71,4 @@ class CommandsTest < PryCommandSetRegistry::TestCase
assert_equal true, registry_command_called
end
end

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
30 changes: 30 additions & 0 deletions test/unit/commands/list_sets_command_test.rb
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

23 changes: 23 additions & 0 deletions test/unit/commands_command_set_test.rb
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

0 comments on commit 8ddb65c

Please sign in to comment.