pry-command-set-registry 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05d4205804afc01b492f5cb8eb27d6dd04cfdc92
4
- data.tar.gz: 2ca8db0ba026143442b230cfb204021ed8023763
3
+ metadata.gz: f0c78344f62614780aa6a856af8e29daadb08666
4
+ data.tar.gz: a42f4b777cfc4af3589ac913f9a983487d735370
5
5
  SHA512:
6
- metadata.gz: d3402499952d253cf878d1c3d6dbc51bab67264b3f6858e4c5e224a9d34b4e3861407a5c082429b3402be15fa98a66c002f90588d33566055d68f80051111153
7
- data.tar.gz: d3cffdcf66b28b058032b68dbe3120bc0a145036e711c751ec73e9de4225356123ae9e85566960c0f66f447bc6e284b3cdc909f233d9c5dc923c3bc4b7843be5
6
+ metadata.gz: 492c4cc1dc6f77b749bd2d3ee30735bb24e5a76da6043209b34fd4c6aa6bcf6a398dbec721d5593e9a317b32024be51b2f9d4f5c0545fd89035310673bd61330
7
+ data.tar.gz: d76e29736b3c448081dff3e908d8493990f0ef2c7167a2b0b914c88fff1e947aec119bf32c07e0ff59f3ae361c4e86d7572fe6358ccc2a99ab0e814d945b6234
data/Gemfile CHANGED
@@ -2,6 +2,10 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
+ group :doc do
6
+ gem "yard"
7
+ end
8
+
5
9
  group :test do
6
10
  gem "guard"
7
11
  gem "guard-minitest"
@@ -4,10 +4,58 @@ require "pry_command_set_registry/registry"
4
4
  require "pry_command_set_registry/command_set"
5
5
  require "pry_command_set_registry/commands"
6
6
 
7
+ # The namespace and primary access point for addressing the
8
+ # PryCommandSetRegistry plugin. Home to the Registry singleton, the primary
9
+ # store of registered command sets.
7
10
  module PryCommandSetRegistry
8
11
  class << self
9
12
  extend Forwardable
13
+ # The registry singleton that stores all defined command sets.
14
+ # @return [PryCommandSetRegistry::Registry]
10
15
  attr_accessor :registry
16
+
17
+ # @!method command_set(name)
18
+ # Attempts to look up a registered command set with the given name. If the
19
+ # name starts with a colon, the colon is removed prior to lookup.
20
+ # @param [String,Symbol] name The name of a registered command set to
21
+ # attempt to retrieve.
22
+ # @return [PryCommandSetRegistry::CommandSet] if a command set with the
23
+ # given name was found.
24
+ # @return [nil] if no command set with the given name was found.
25
+ # @see PryCommandSetRegistry::Registry#command_set
26
+ # @!method command_sets
27
+ # The Hash mapping of all registered command sets.
28
+ # @return [Hash{String => PryCommandSetRegistry::CommandSet}]
29
+ # @see PryCommandSetRegistry::Registry#command_sets
30
+ # @!method define_command_set(name, description, options = {}, &block)
31
+ # Helper method for defining a command set and registering it immediately.
32
+ # All arguments are passed directly to
33
+ # {PryCommandSetRegistry::CommandSet#initialize CommandSet#initialize} to
34
+ # instantiate a new command set.
35
+ #
36
+ # @param [String,Symbol] name The name that should be given to the command
37
+ # set. The provided name will be displayed by the `list-sets` command and
38
+ # is used to identify the command set when calling the `import-set`
39
+ # command.
40
+ # @param [String] description A description of the command set. The provided
41
+ # description will be displayed by the `list-sets` command.
42
+ # @param [Hash{Symbol=>String}] options Optional arguments.
43
+ # @option options [String] group A group name to apply to all commands in
44
+ # the command set. This group will be displayed in commands like `help`,
45
+ # however depending on the version of pry it may be sentence cased when
46
+ # displayed. When no group is provided, Pry will use `(other)` by default.
47
+ # @yield The provided block is evaluated by the command set instance and is
48
+ # used to define commands and configure the command set in other ways.
49
+ # @example Create a new CommandSet with a `hello-world` command
50
+ # PryCommandSetRegistry.define_command_set("Examples", "Example Commands", :group => "Examples") do
51
+ # command("hello-world", "Greets the world") do
52
+ # _pry_.outputter.puts("Hello world!")
53
+ # end
54
+ # end
55
+ # @raise [ArgumentError] if no block is given.
56
+ # @return [PryCommandSetRegistry::CommandSet] The command set instance created.
57
+ # @see PryCommandSetRegistry::Registry#define_command_set
58
+ # @see PryCommandSetRegistry::CommandSet#initialize
11
59
  def_delegators :registry, :command_set, :command_sets, :define_command_set
12
60
  end
13
61
  self.registry ||= Registry.new
@@ -1,19 +1,61 @@
1
1
  module PryCommandSetRegistry
2
+ # A set of commands that can me imported into a Pry session.
2
3
  class CommandSet < Pry::CommandSet
3
- attr_reader :commands, :description, :name
4
+ # The default group name that Pry gives to commands without a group.
5
+ DEFAULT_GROUP_NAME = "(other)".freeze
4
6
 
5
- def initialize(name, description, &block)
6
- raise "Block required!" unless block_given?
7
+ # The description of the command set provided at creation.
8
+ attr_reader :description
9
+
10
+ # The group name given to the command set at creation.
11
+ attr_reader :group
12
+
13
+ # The name of the command set provided at creation.
14
+ attr_reader :name
15
+
16
+ # Creates a new command set.
17
+ #
18
+ # @param [String,Symbol] name The name that should be given to the command
19
+ # set. The provided name will be displayed by the `list-sets` command and
20
+ # is used to identify the command set when calling the `import-set`
21
+ # command.
22
+ # @param [String] description A description of the command set. The provided
23
+ # description will be displayed by the `list-sets` command.
24
+ # @param [Hash{Symbol=>String}] options Optional arguments.
25
+ # @option options [String] group A group name to apply to all commands in
26
+ # the command set. This group will be displayed in commands like `help`,
27
+ # however depending on the version of pry it may be sentence cased when
28
+ # displayed. When no group is provided, Pry will use `(other)` by default.
29
+ # @yield The provided block is evaluated by the command set instance and is
30
+ # used to define commands and configure the command set in other ways.
31
+ # @example Create a new CommandSet with a `hello-world` command
32
+ # CommandSet.new("Examples", "Example Commands", :group => "Examples") do
33
+ # command("hello-world", "Greets the world") do
34
+ # _pry_.outputter.puts("Hello world!")
35
+ # end
36
+ # end
37
+ # @raise [ArgumentError] if no block is given.
38
+ # @return [PryCommandSetRegistry::CommandSet] The instance created.
39
+ def initialize(name, description, options = {}, &block)
40
+ raise ArgumentError, "Block required!" unless block_given?
7
41
  super(&block)
8
42
  @description = description
9
43
  @name = name.to_s
44
+ @group = options[:group] || DEFAULT_GROUP_NAME
10
45
  apply_group_name_to_commands
11
46
  end
12
47
 
13
48
  private
14
49
 
50
+ # If a group name was provided, adds that group to all commands in the
51
+ # command set.
52
+ #
53
+ # @return [true] if the group name was added to all commands.
54
+ # @return [false] if group no group was provided at instantiation.
15
55
  def apply_group_name_to_commands
16
- commands.values.each { |command| command.group(name) }
56
+ return false if group == DEFAULT_GROUP_NAME
57
+ each { |command_name, command| command.group(group) }
58
+ true
17
59
  end
18
60
  end
19
61
  end
@@ -1,6 +1,9 @@
1
1
  module PryCommandSetRegistry
2
2
  desc = "Commands for interacting with the Pry command set registry"
3
- Commands = CommandSet.new("PryCommandSetRegistry", desc) do
3
+
4
+ # Default commands for interacting with PryCommandSetRegistry imported into
5
+ # Pry.
6
+ Commands = CommandSet.new("PryCommandSetRegistry", desc, :group => "Command Set Registry") do
4
7
  command("import-set", "Import a Pry command set") do |command_set_name|
5
8
  raise Pry::CommandError, "Provide a command set name" if command_set_name.nil?
6
9
 
@@ -1,19 +1,60 @@
1
1
  module PryCommandSetRegistry
2
+ # A registry of command sets and descriptions of those command sets.
2
3
  class Registry
4
+ # The Hash mapping of all registered command sets.
5
+ # @return [Hash{String => PryCommandSetRegistry::CommandSet}]
3
6
  attr_reader :command_sets
4
7
 
8
+ # Creates a new command set registry.
9
+ # @return [PryCommandSetRegistry::Registry] The registry instance.
5
10
  def initialize
6
11
  @command_sets = {}
7
12
  end
8
13
 
14
+ # Attempts to look up a registered command set with the given name. If the
15
+ # name starts with a colon, the colon is removed prior to lookup.
16
+ #
17
+ # @param [String,Symbol] name The name of a registered command set to
18
+ # attempt to retrieve.
19
+ # @return [PryCommandSetRegistry::CommandSet] if a command set with the
20
+ # given name was found.
21
+ # @return [nil] if no command set with the given name was found.
9
22
  def command_set(name)
10
23
  sanitized_name = name.to_s.sub(/^:/, "")
11
24
  command_sets[sanitized_name]
12
25
  end
13
26
 
14
- def define_command_set(name, description, &block)
15
- command_set = CommandSet.new(name, description, &block)
27
+ # Helper method for defining a command set and registering it immediately.
28
+ # All arguments are passed directly to
29
+ # {PryCommandSetRegistry::CommandSet#initialize CommandSet#initialize} to
30
+ # instantiate a new command set.
31
+ #
32
+ # @param [String,Symbol] name The name that should be given to the command
33
+ # set. The provided name will be displayed by the `list-sets` command and
34
+ # is used to identify the command set when calling the `import-set`
35
+ # command.
36
+ # @param [String] description A description of the command set. The provided
37
+ # description will be displayed by the `list-sets` command.
38
+ # @param [Hash{Symbol=>String}] options Optional arguments.
39
+ # @option options [String] group A group name to apply to all commands in
40
+ # the command set. This group will be displayed in commands like `help`,
41
+ # however depending on the version of pry it may be sentence cased when
42
+ # displayed. When no group is provided, Pry will use `(other)` by default.
43
+ # @yield The provided block is evaluated by the command set instance and is
44
+ # used to define commands and configure the command set in other ways.
45
+ # @example Create a new CommandSet with a `hello-world` command
46
+ # Registry.define_command_set("Examples", "Example Commands", :group => "Examples") do
47
+ # command("hello-world", "Greets the world") do
48
+ # _pry_.outputter.puts("Hello world!")
49
+ # end
50
+ # end
51
+ # @raise [ArgumentError] if no block is given.
52
+ # @return [PryCommandSetRegistry::CommandSet] The command set instance created.
53
+ # @see PryCommandSetRegistry::CommandSet#initialize
54
+ def define_command_set(name, description, options = {}, &block)
55
+ command_set = CommandSet.new(name, description, options, &block)
16
56
  command_sets[command_set.name] = command_set
57
+ command_set
17
58
  end
18
59
  end
19
60
  end
@@ -1,3 +1,4 @@
1
1
  module PryCommandSetRegistry
2
- VERSION = "0.0.1".freeze
2
+ # The version of the PryCommandSetRegistry gem.
3
+ VERSION = "0.1.0".freeze
3
4
  end
@@ -11,6 +11,10 @@ class CommandSetTest < PryCommandSetRegistry::TestCase
11
11
  @command_set_proc = default_command_set_proc
12
12
  end
13
13
 
14
+ should "raise ArgumentError if no block is given" do
15
+ assert_raises(ArgumentError) { Subject.new(@name, @description) }
16
+ end
17
+
14
18
  should "create a new instance from the given arguments" do
15
19
  instance = Subject.new(@name, @description, &@command_set_proc)
16
20
 
@@ -25,9 +29,15 @@ class CommandSetTest < PryCommandSetRegistry::TestCase
25
29
  assert_equal true, @command_set_proc_obj[:command_called]
26
30
  end
27
31
 
28
- should "correctly set group names" do
32
+ should "should not set group names if no group given" do
29
33
  instance = Subject.new(@name, @description, &@command_set_proc)
30
- assert_equal @name, instance.commands.values.first.group
34
+ assert_equal "(other)", instance.each.first.last.group
35
+ end
36
+
37
+ should "correctly set group names if group given" do
38
+ group = "Test Group"
39
+ instance = Subject.new(@name, @description, :group => group, &@command_set_proc)
40
+ assert_equal group, instance.each.first.last.group
31
41
  end
32
42
  end
33
43
  end
@@ -16,11 +16,14 @@ class RegistryTest < PryCommandSetRegistry::TestCase
16
16
  should "create a new CommandSet with the given args" do
17
17
  name = "foo"
18
18
  description = "bar"
19
+ group = "Test"
20
+ opts = { :group => group }
19
21
  called = false
20
22
  command_set_proc = proc { called = true }
21
- group = subject.define_command_set(name, description, &command_set_proc)
22
- assert_equal name, group.name
23
- assert_equal description, group.description
23
+ command_set = subject.define_command_set(name, description, opts, &command_set_proc)
24
+ assert_equal name, command_set.name
25
+ assert_equal description, command_set.description
26
+ assert_equal group, command_set.group
24
27
  assert_equal true, called, "CommandSet proc was not called!"
25
28
  end
26
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-command-set-registry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Guinther
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler