simple_command_dispatcher 1.1.1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +102 -1
- data/lib/core_extensions/string.rb +3 -0
- data/lib/simple_command_dispatcher.rb +51 -17
- data/lib/simple_command_dispatcher/configuration.rb +45 -0
- data/lib/simple_command_dispatcher/configure.rb +18 -0
- data/lib/simple_command_dispatcher/klass_transform.rb +1 -1
- data/lib/simple_command_dispatcher/version.rb +1 -1
- metadata +5 -3
- data/CHANGELOG.mb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86113bb49021916a8a1352875961117ee7323c31
|
4
|
+
data.tar.gz: b3341892eaeba98ec82a42795ac9b30e2dd2cb5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a147390f419be6ae76819fc5b781d05ff330fe60ab8ae6104251de6e4abb5c05c8603e3a5bbc6fbbb5aa092c15abe42deb147d20a7c3c2ad575c46d9c111c08
|
7
|
+
data.tar.gz: cf4ca4296568d38f35959730967c6030dd511b9099f14dc8682d356bf9cf7e26df138d7ed7d3b38b58d810d9b3b05822ba1dfaaaf4e90aedc9ef33fd6d3c5690
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
### Version 1.2.1
|
2
|
+
* Configuration class
|
3
|
+
* Added the new Configuration class that exposes the #allow_custom_classes property which takes a Boolean allowing/disallowing the use of custom commands to be used. See the documentation for details and usage.
|
4
|
+
* Custom commands
|
5
|
+
* Allow ability for users to use custom commands (i.e. classes that do not prepend the SimpleCommand module) as long as the command class respond_to? the ::call public class method. Note: Configuration#allow_custom_commands must be set to true to use custom commands.
|
6
|
+
* Documentation updates
|
7
|
+
* Add documentation for new Configuration class and miscellaneous other code additions/changes.
|
8
|
+
|
9
|
+
### Version 1.1.1
|
10
|
+
* Documentation updates
|
11
|
+
* Add example code in README.md to include clarification on command namespacing, and how to autoload command classes to avoid NameError exceptions when SimpleCommand::Dispatcher.call(...) is call due to uninitialized command constants.
|
12
|
+
|
13
|
+
## Version 1.1.0 - 2016-11-01 [YANKED]
|
14
|
+
## Version 1.0.0 - 2016-11-01 [YANKED]
|
15
|
+
## Version 0.2.0 - 2016-11-01 [YANKED]
|
16
|
+
## Version 0.1.3 - 2016-11-01 [YANKED]
|
17
|
+
## Version 0.1.2 - 2016-10-29 [YANKED]
|
18
|
+
## Version 0.1.1 - 2016-10-29 [YANKED]
|
19
|
+
## Version 0.1.0 - 2016-10-29 [YANKED]
|
data/README.md
CHANGED
@@ -1,11 +1,22 @@
|
|
1
1
|
[](https://badge.fury.io/gh/gangelo%2Fsimple_command_dispatcher)
|
2
2
|
[](https://badge.fury.io/rb/simple_command_dispatcher)
|
3
3
|
|
4
|
+

|
5
|
+
[](http://www.rubydoc.info/gems/simple_command_dispatcher/)
|
6
|
+
|
7
|
+
[](https://github.com/gangelo/simple_command_dispatcher/issues)
|
8
|
+
|
9
|
+
[](#license)
|
10
|
+
|
4
11
|
# Q. simple_command_dispatcher - what is it?
|
5
12
|
# A. It's a Ruby gem!
|
6
13
|
|
7
14
|
## Overview
|
8
|
-
__simple_command_dispatcher__ (SCD) allows you to execute __simple_command__ commands in a more dynamic way. If you are not familiar with the _simple_command_ gem, check it out [here][simple-command]. SCD was written specifically with the [rails-api][rails-api] in mind; however, you can use SDC wherever you use simple_command commands.
|
15
|
+
__simple_command_dispatcher__ (SCD) allows you to execute __simple_command__ commands (and now _custom commands_ as of version 1.2.1) in a more dynamic way. If you are not familiar with the _simple_command_ gem, check it out [here][simple-command]. SCD was written specifically with the [rails-api][rails-api] in mind; however, you can use SDC wherever you would use simple_command commands.
|
16
|
+
|
17
|
+
## Update as of Version 1.2.1
|
18
|
+
### Custom Commands
|
19
|
+
SCD now allows you to execute _custom commands_ (i.e. classes that do not prepend the _SimpleCommand_ module) by setting `Configuration#allow_custom_commands = true` (see the __Custom Commands__ section below for details).
|
9
20
|
|
10
21
|
## Example
|
11
22
|
The below example is from a `rails-api` API that uses token-based authentication and services two mobile applications, identified as *__my_app1__* and *__my_app2__*, in this example.
|
@@ -124,6 +135,12 @@ Rails.application.config.to_prepare do
|
|
124
135
|
ActionDispatch::Reloader.to_prepare { reloader.execute_if_updated }
|
125
136
|
reloader.execute
|
126
137
|
end
|
138
|
+
|
139
|
+
# Optionally set our configuration setting to allow
|
140
|
+
# for custom command execution.
|
141
|
+
SimpleCommand::Dispatcher.configure do |config|
|
142
|
+
config.allow_custom_commands = true
|
143
|
+
end
|
127
144
|
```
|
128
145
|
|
129
146
|
```ruby
|
@@ -169,6 +186,90 @@ class ApplicationController < ActionController::API
|
|
169
186
|
end
|
170
187
|
```
|
171
188
|
|
189
|
+
## Custom Commands
|
190
|
+
|
191
|
+
As of __Version 1.2.1__ simple_command_dispatcher (SCD) allows you to execute _custom commands_ (i.e. classes that do not prepend the _SimpleCommand_ module) by setting `Configuration#allow_custom_commands = true`.
|
192
|
+
|
193
|
+
In order to execute _custom commands_, there are three (3) requirements:
|
194
|
+
1. Create a _custom command_. Your _custom command_ class must expose a public `::call` class method.
|
195
|
+
2. Set the `Configuration#allow_custom_commands` property to `true`.
|
196
|
+
3. Execute your _custom command_ by calling the `::call` class method.
|
197
|
+
|
198
|
+
### Custom Command Example
|
199
|
+
|
200
|
+
#### 1. Create a Custom Command
|
201
|
+
```ruby
|
202
|
+
# /api/my_app/v1/custom_command.rb
|
203
|
+
|
204
|
+
module Api
|
205
|
+
module MyApp
|
206
|
+
module V1
|
207
|
+
|
208
|
+
# This is a custom command that does not prepend SimpleCommand.
|
209
|
+
class CustomCommand
|
210
|
+
|
211
|
+
def self.call(*args)
|
212
|
+
command = self.new(*args)
|
213
|
+
if command
|
214
|
+
command.send(:execute)
|
215
|
+
else
|
216
|
+
false
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
private
|
221
|
+
|
222
|
+
def initialize(params = {})
|
223
|
+
@param1 = params[:param1]
|
224
|
+
end
|
225
|
+
|
226
|
+
private
|
227
|
+
|
228
|
+
attr_accessor :param1
|
229
|
+
|
230
|
+
def execute
|
231
|
+
if (param1 == :param1)
|
232
|
+
return true
|
233
|
+
end
|
234
|
+
|
235
|
+
return false
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
```
|
243
|
+
#### 2. Set the `Configuration#allow_custom_commands` property to `true`
|
244
|
+
```ruby
|
245
|
+
# In your rails, rails-api app, etc...
|
246
|
+
# /config/initializers/simple_command_dispatcher.rb
|
247
|
+
|
248
|
+
SimpleCommand::Dispatcher.configure do |config|
|
249
|
+
config.allow_custom_commands = true
|
250
|
+
end
|
251
|
+
```
|
252
|
+
|
253
|
+
#### 3. Execute your _Custom Command_
|
254
|
+
Executing your _custom command_ is no different than executing a __SimpleCommand__ command with the exception that you must properly handle the return object that results from calling your _custom command_; being a _custom command_, there is no guarantee that the return object will be the command object as is the case when calling a SimpleCommand command.
|
255
|
+
```ruby
|
256
|
+
# /app/controllers/some_controller.rb
|
257
|
+
|
258
|
+
require 'simple_command_dispatcher'
|
259
|
+
|
260
|
+
class SomeController < ApplicationController::API
|
261
|
+
public
|
262
|
+
|
263
|
+
def some_api
|
264
|
+
success = SimpleCommand::Dispatcher.call(:CustomCommand, get_command_path, { camelize: true}, request.headers)
|
265
|
+
if success
|
266
|
+
# Do something...
|
267
|
+
else
|
268
|
+
# Do something else...
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
```
|
172
273
|
|
173
274
|
## Installation
|
174
275
|
|
@@ -13,9 +13,9 @@ end
|
|
13
13
|
|
14
14
|
module SimpleCommand
|
15
15
|
|
16
|
-
# Provides a way to call
|
16
|
+
# Provides a way to call SimpleCommands or your own custom commands in a more dymanic manner.
|
17
17
|
#
|
18
|
-
# For information about the simple_command gem, visit https://rubygems.org/gems/simple_command
|
18
|
+
# For information about the simple_command gem, visit {https://rubygems.org/gems/simple_command}
|
19
19
|
#
|
20
20
|
module Dispatcher
|
21
21
|
|
@@ -24,9 +24,9 @@ module SimpleCommand
|
|
24
24
|
|
25
25
|
public
|
26
26
|
|
27
|
-
# Calls a *SimpleCommand* given the command name, the modules the command belongs to and the parameters to pass to the command.
|
27
|
+
# Calls a *SimpleCommand* or *Command* given the command name, the modules the command belongs to and the parameters to pass to the command.
|
28
28
|
#
|
29
|
-
# @param command [Symbol, String] the name of the SimpleCommand to call.
|
29
|
+
# @param command [Symbol, String] the name of the SimpleCommand or Command to call.
|
30
30
|
#
|
31
31
|
# @param command_modules [Hash, Array] the ruby modules that qualify the SimpleCommand to call. When passing a Hash, the Hash
|
32
32
|
# keys serve as documentation only. For example, ['Api', 'AppName', 'V1'] and { :api :Api, app_name: :AppName, api_version: :V1 }
|
@@ -41,10 +41,10 @@ module SimpleCommand
|
|
41
41
|
# @option options [Boolean] :module_titleize (false) determines whether or not module names should be titleized.
|
42
42
|
# @option options [Boolean] :module_camelized (false) determines whether or not module names should be camelized.
|
43
43
|
#
|
44
|
-
# @param command_parameters [
|
45
|
-
# passed through to the call method of the SimpleCommand.
|
44
|
+
# @param command_parameters [Array<Symbol>] the parameters to pass to the call method of the SimpleCommand . This parameter is simply
|
45
|
+
# passed through to the call method of the SimpleCommand/Command.
|
46
46
|
#
|
47
|
-
# @return [SimpleCommand] the SimpleCommand returned as a result
|
47
|
+
# @return [SimpleCommand, Object] the SimpleCommand or Object returned as a result of calling the SimpleCommand#call method or the Command#call method respectfully.
|
48
48
|
#
|
49
49
|
# @example
|
50
50
|
#
|
@@ -61,33 +61,67 @@ module SimpleCommand
|
|
61
61
|
def call(command = "", command_modules = {}, options = {}, *command_parameters)
|
62
62
|
|
63
63
|
# Create a constantized class from our command and command_modules...
|
64
|
-
|
64
|
+
command_class_constant = to_constantized_class(command, command_modules, options)
|
65
65
|
|
66
|
-
#
|
67
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
|
66
|
+
# If we're NOT allowing custom commands, make sure we're dealing with a a command class
|
67
|
+
# that prepends the SimpleCommand module.
|
68
|
+
if !SimpleCommand::Dispatcher.configuration.allow_custom_commands
|
69
|
+
# Calling is_simple_command? returns true if the class pointed to by
|
70
|
+
# command_class_constant is a valid SimpleCommand class; that is,
|
71
|
+
# if it prepends module SimpleCommand::ClassMethods.
|
72
|
+
if !is_simple_command?(command_class_constant)
|
73
|
+
raise ArgumentError.new("Class \"#{command_class_constant}\" must prepend module SimpleCommand if Configuration#allow_custom_commands is true.")
|
74
|
+
end
|
71
75
|
end
|
72
76
|
|
73
|
-
|
74
|
-
|
75
|
-
|
77
|
+
if is_valid_command(command_class_constant)
|
78
|
+
# We know we have a valid SimpleCommand; all we need to do is call #call,
|
79
|
+
# pass the command_parameter variable arguments to the call, and return the results.
|
80
|
+
run_command(command_class_constant, command_parameters)
|
81
|
+
else
|
82
|
+
raise NameError.new("Class \"#{command_class_constant}\" does not respond_to? method ::call.")
|
83
|
+
end
|
76
84
|
end
|
77
85
|
|
78
86
|
private
|
79
87
|
|
80
|
-
#
|
88
|
+
# Returns true or false depending on whether or not the class constant has a public
|
89
|
+
# class method named ::call defined. Commands that do not have a public class method
|
90
|
+
# named ::call, are considered invalid.
|
91
|
+
#
|
92
|
+
# @param klass_constant [String] a class constant that will be validated to see whether or not the class is a valid command.
|
93
|
+
#
|
94
|
+
# @return [Boolean] true if klass_constant has a public class method named ::call defined, false otherwise.
|
81
95
|
#
|
96
|
+
# @!visibility public
|
97
|
+
def is_valid_command(klass_constant)
|
98
|
+
klass_constant.eigenclass.public_method_defined?(:call)
|
99
|
+
end
|
100
|
+
|
82
101
|
# Returns true or false depending on whether or not the class constant prepends module SimpleCommand::ClassMethods.
|
83
102
|
#
|
84
103
|
# @param klass_constant [String] a class constant that will be validated to see whether or not the class prepends module SimpleCommand::ClassMethods.
|
85
104
|
#
|
86
105
|
# @return [Boolean] true if klass_constant prepends Module SimpleCommand::ClassMethods, false otherwise.
|
87
106
|
#
|
107
|
+
# @!visibility public
|
88
108
|
def is_simple_command?(klass_constant)
|
89
109
|
klass_constant.eigenclass.included_modules.include? SimpleCommand::ClassMethods
|
90
110
|
end
|
111
|
+
|
112
|
+
# Runs the command given the parameters and returns the result.
|
113
|
+
#
|
114
|
+
# @param klass_constant [String] a class constant that will be called.
|
115
|
+
# @param parameters [Array] an array of parameters to pass to the command that will be called.
|
116
|
+
#
|
117
|
+
# @return [Object] returns the object (if any) that results from calling the command.
|
118
|
+
#
|
119
|
+
# @!visibility public
|
120
|
+
def run_command(klass_constant, parameters)
|
121
|
+
klass_constant.call(*parameters)
|
122
|
+
#rescue NameError
|
123
|
+
# raise NameError.new("Class \"#{klass_constant}\" does not respond_to? method ::call.")
|
124
|
+
end
|
91
125
|
end
|
92
126
|
end
|
93
127
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module SimpleCommand
|
2
|
+
module Dispatcher
|
3
|
+
|
4
|
+
# Gem configuration settings class. Use this class to configure this gem.
|
5
|
+
#
|
6
|
+
# To configure this gem in your application, simply add the following code in your application and set the
|
7
|
+
# appropriate configuration settings.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# SimpleCommand::Dispatcher.configure do |config|
|
12
|
+
# config.allow_custom_commands = true
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
class Configuration
|
16
|
+
# Gets/sets the *allow_custom_commands* configuration setting (defaults to false).
|
17
|
+
# If this setting is set to *false*, only command classes that prepend the *SimpleCommand* module
|
18
|
+
# will be considered acceptable to run, all other command classes will fail to run. If this
|
19
|
+
# setting is set to *true*, any command class will be considered acceptable to run, regardless of
|
20
|
+
# whether or not the class prepends the *SimpleCommand* module.
|
21
|
+
#
|
22
|
+
# For information about the simple_command gem, visit {https://rubygems.org/gems/simple_command}
|
23
|
+
#
|
24
|
+
# @return [Boolean] the value.
|
25
|
+
#
|
26
|
+
attr_accessor :allow_custom_commands
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
# The default is to use any command that exposes a ::call class method.
|
30
|
+
reset
|
31
|
+
end
|
32
|
+
|
33
|
+
public
|
34
|
+
|
35
|
+
# Resets the configuration to use the default values.
|
36
|
+
#
|
37
|
+
# @return [nil] returns nil.
|
38
|
+
#
|
39
|
+
def reset
|
40
|
+
@allow_custom_commands = false
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SimpleCommand
|
2
|
+
module Dispatcher
|
3
|
+
class << self
|
4
|
+
attr_writer :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
# Returns the application configuration object.
|
8
|
+
#
|
9
|
+
# @return [Configuration] the application Configuration object.
|
10
|
+
def self.configuration
|
11
|
+
@configuration ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
yield(configuration)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -199,7 +199,7 @@ module SimpleCommand
|
|
199
199
|
#
|
200
200
|
def validate_klass(klass, options)
|
201
201
|
if !(klass.is_a?(Symbol) || klass.is_a?(String))
|
202
|
-
raise ArgumentError.new('Class is not a String or Symbol. Class must equal the name of the SimpleCommand to call in the form of a String or Symbol.')
|
202
|
+
raise ArgumentError.new('Class is not a String or Symbol. Class must equal the class name of the SimpleCommand or Command to call in the form of a String or Symbol.')
|
203
203
|
end
|
204
204
|
|
205
205
|
klass = klass.to_s.strip
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_command_dispatcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gene M. Angelo, Jr.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -144,7 +144,7 @@ files:
|
|
144
144
|
- ".rspec"
|
145
145
|
- ".ruby-version"
|
146
146
|
- ".travis.yml"
|
147
|
-
- CHANGELOG.
|
147
|
+
- CHANGELOG.md
|
148
148
|
- CODE_OF_CONDUCT.md
|
149
149
|
- Gemfile
|
150
150
|
- LICENSE.txt
|
@@ -154,6 +154,8 @@ files:
|
|
154
154
|
- bin/setup
|
155
155
|
- lib/core_extensions/string.rb
|
156
156
|
- lib/simple_command_dispatcher.rb
|
157
|
+
- lib/simple_command_dispatcher/configuration.rb
|
158
|
+
- lib/simple_command_dispatcher/configure.rb
|
157
159
|
- lib/simple_command_dispatcher/klass_transform.rb
|
158
160
|
- lib/simple_command_dispatcher/version.rb
|
159
161
|
- lib/tasks/simple_command_dispatcher_sandbox.rake
|
data/CHANGELOG.mb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
### Unreleased
|
2
|
-
* Upcoming features
|
3
|
-
* whitelist for allowable namespaces
|
4
|
-
|
5
|
-
### 1.1.1
|
6
|
-
* Documentation updates
|
7
|
-
* Add example code in README.md to include clarification on command namespacing, and how to autoload command classes to avoid NameError exceptions when SimpleCommand::Dispatcher.call(...) is call due to uninitialized command constants.
|
8
|
-
|
9
|
-
### 1.1.0
|
10
|
-
* Bug fix lib/core_extensions/string.rb not included in SimpleCommand::Dispatcher, causing exception.
|
11
|
-
|
12
|
-
### 1.0.0
|
13
|
-
* Limit support to ruby >= 2.2.2
|
14
|
-
|
15
|
-
### 0.2.0
|
16
|
-
* Yanked
|
17
|
-
|
18
|
-
### 0.1.3
|
19
|
-
* Documentation updates
|
20
|
-
* Add `SimpleCommand::KlassTransform` rake task sandbox to test the below listed `SimpleCommand::KlassTransform` members; run the rake tasks for usage; these rake tasks are simply a playground to see how simple_command_dispatcher transforms parameters into output when calling SimpleCommand::Dispatcher.call(...):
|
21
|
-
* `SimpleCommand::KlassTransform#to_class_string`
|
22
|
-
* $ rake to_class_string
|
23
|
-
* `SimpleCommand::KlassTransform#to_modules_string`
|
24
|
-
* $ rake to_modules_string
|
25
|
-
* `SimpleCommand::KlassTransform#to_constantized_class_string`
|
26
|
-
* $ rake to_constantized_class_string
|
27
|
-
|
28
|
-
### 0.1.2
|
29
|
-
* Documentation updates
|
30
|
-
|
31
|
-
### 0.1.1
|
32
|
-
* Documentation updates
|