simple_command_dispatcher 3.0.4 → 4.0.0
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/.github/workflows/ruby.yml +47 -25
- data/.gitignore +3 -0
- data/.rubocop.yml +36 -41
- data/.ruby-version +1 -1
- data/CHANGELOG.md +148 -59
- data/Gemfile +3 -6
- data/Gemfile.lock +76 -69
- data/Jenkinsfile +2 -2
- data/README.md +371 -217
- data/Rakefile +0 -8
- data/lib/core_ext/kernel.rb +22 -0
- data/lib/simple_command_dispatcher/configuration.rb +37 -35
- data/lib/simple_command_dispatcher/errors/invalid_class_constant_error.rb +16 -0
- data/lib/simple_command_dispatcher/errors/required_class_method_missing_error.rb +15 -0
- data/lib/simple_command_dispatcher/errors.rb +4 -0
- data/lib/simple_command_dispatcher/helpers/camelize.rb +46 -0
- data/lib/simple_command_dispatcher/helpers/trim_all.rb +16 -0
- data/lib/simple_command_dispatcher/services/command_namespace_service.rb +60 -0
- data/lib/simple_command_dispatcher/services/command_service.rb +152 -0
- data/lib/simple_command_dispatcher/version.rb +2 -4
- data/lib/simple_command_dispatcher.rb +69 -121
- data/simple_command_dispatcher.gemspec +3 -3
- metadata +12 -31
- data/lib/core_extensions/string.rb +0 -10
- data/lib/simple_command_dispatcher/configure.rb +0 -22
- data/lib/simple_command_dispatcher/klass_transform.rb +0 -251
- data/lib/tasks/simple_command_dispatcher_sandbox.rake +0 -222
@@ -6,7 +6,7 @@ require 'simple_command_dispatcher/version'
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'simple_command_dispatcher'
|
9
|
-
spec.version =
|
9
|
+
spec.version = SimpleCommandDispatcher::VERSION
|
10
10
|
spec.authors = ['Gene M. Angelo, Jr.']
|
11
11
|
spec.email = ['public.gma@gmail.com']
|
12
12
|
|
@@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
|
|
35
35
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
36
|
spec.require_paths = ['lib']
|
37
37
|
|
38
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 3.
|
38
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 3.3', '< 4.0')
|
39
|
+
|
39
40
|
spec.add_runtime_dependency 'activesupport', '>= 7.0.8', '< 8.0'
|
40
|
-
spec.add_runtime_dependency 'simple_command', '~> 1.0', '>= 1.0.1'
|
41
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_command_dispatcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gene M. Angelo, Jr.
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: activesupport
|
@@ -30,26 +29,6 @@ dependencies:
|
|
30
29
|
- - "<"
|
31
30
|
- !ruby/object:Gem::Version
|
32
31
|
version: '8.0'
|
33
|
-
- !ruby/object:Gem::Dependency
|
34
|
-
name: simple_command
|
35
|
-
requirement: !ruby/object:Gem::Requirement
|
36
|
-
requirements:
|
37
|
-
- - "~>"
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: '1.0'
|
40
|
-
- - ">="
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version: 1.0.1
|
43
|
-
type: :runtime
|
44
|
-
prerelease: false
|
45
|
-
version_requirements: !ruby/object:Gem::Requirement
|
46
|
-
requirements:
|
47
|
-
- - "~>"
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: '1.0'
|
50
|
-
- - ">="
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version: 1.0.1
|
53
32
|
description: 'Within a services API (rails-api for instance), you may have a need
|
54
33
|
to execute different simple_commands or your own custom commands (service objects)
|
55
34
|
based on one or more factors: multiple application, API version, user type, user
|
@@ -80,19 +59,22 @@ files:
|
|
80
59
|
- Rakefile
|
81
60
|
- bin/console
|
82
61
|
- bin/setup
|
83
|
-
- lib/
|
62
|
+
- lib/core_ext/kernel.rb
|
84
63
|
- lib/simple_command_dispatcher.rb
|
85
64
|
- lib/simple_command_dispatcher/configuration.rb
|
86
|
-
- lib/simple_command_dispatcher/
|
87
|
-
- lib/simple_command_dispatcher/
|
65
|
+
- lib/simple_command_dispatcher/errors.rb
|
66
|
+
- lib/simple_command_dispatcher/errors/invalid_class_constant_error.rb
|
67
|
+
- lib/simple_command_dispatcher/errors/required_class_method_missing_error.rb
|
68
|
+
- lib/simple_command_dispatcher/helpers/camelize.rb
|
69
|
+
- lib/simple_command_dispatcher/helpers/trim_all.rb
|
70
|
+
- lib/simple_command_dispatcher/services/command_namespace_service.rb
|
71
|
+
- lib/simple_command_dispatcher/services/command_service.rb
|
88
72
|
- lib/simple_command_dispatcher/version.rb
|
89
|
-
- lib/tasks/simple_command_dispatcher_sandbox.rake
|
90
73
|
- simple_command_dispatcher.gemspec
|
91
74
|
homepage: https://github.com/gangelo/simple_command_dispatcher
|
92
75
|
licenses:
|
93
76
|
- MIT
|
94
77
|
metadata: {}
|
95
|
-
post_install_message:
|
96
78
|
rdoc_options: []
|
97
79
|
require_paths:
|
98
80
|
- lib
|
@@ -100,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
82
|
requirements:
|
101
83
|
- - ">="
|
102
84
|
- !ruby/object:Gem::Version
|
103
|
-
version: 3.
|
85
|
+
version: '3.3'
|
104
86
|
- - "<"
|
105
87
|
- !ruby/object:Gem::Version
|
106
88
|
version: '4.0'
|
@@ -110,8 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
92
|
- !ruby/object:Gem::Version
|
111
93
|
version: '0'
|
112
94
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
114
|
-
signing_key:
|
95
|
+
rubygems_version: 3.6.8
|
115
96
|
specification_version: 4
|
116
97
|
summary: Provides a way to dispatch simple_command (ruby gem) commands or your own
|
117
98
|
custom commands (service objects) in a more dynamic manner within your service API.
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'configuration'
|
4
|
-
|
5
|
-
module SimpleCommand
|
6
|
-
module Dispatcher
|
7
|
-
class << self
|
8
|
-
attr_writer :configuration
|
9
|
-
end
|
10
|
-
|
11
|
-
# Returns the application configuration object.
|
12
|
-
#
|
13
|
-
# @return [Configuration] the application Configuration object.
|
14
|
-
def self.configuration
|
15
|
-
@configuration ||= Configuration.new
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.configure
|
19
|
-
yield(configuration)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,251 +0,0 @@
|
|
1
|
-
# rubocop:disable Style/OptionHash
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require_relative '../core_extensions/string'
|
5
|
-
|
6
|
-
module SimpleCommand
|
7
|
-
# Handles class and module transformations.
|
8
|
-
module KlassTransform
|
9
|
-
# Returns a constantized class (as a Class constant), given the klass and klass_modules.
|
10
|
-
#
|
11
|
-
# @param klass [Symbol or String] the class name.
|
12
|
-
# @param klass_modules [Hash, Array or String] the modules klass belongs to.
|
13
|
-
# @param options [Hash] the options that determine how klass_modules is transformed.
|
14
|
-
# @option options [Boolean] :camelize (false) determines whether or not both klass and klass_modules
|
15
|
-
# should be camelized.
|
16
|
-
# @option options [Boolean] :titleize (false) determines whether or not both klass and klass_modules
|
17
|
-
# should be titleized.
|
18
|
-
# @option options [Boolean] :class_titleize (false) determines whether or not klass names should be
|
19
|
-
# titleized.
|
20
|
-
# @option options [Boolean] :class_camelized (false) determines whether or not klass names should be
|
21
|
-
# camelized.
|
22
|
-
# @option options [Boolean] :module_titleize (false) determines whether or not klass_modules names
|
23
|
-
# should be titleized.
|
24
|
-
# @option options [Boolean] :module_camelized (false) determines whether or not klass_modules names
|
25
|
-
# should be camelized.
|
26
|
-
#
|
27
|
-
# @return [Class] the class constant. Can be used to call ClassConstant.constantize.
|
28
|
-
#
|
29
|
-
# @raise [NameError] if the constantized class string cannot be constantized; that is, if it is not
|
30
|
-
# a valid class constant.
|
31
|
-
#
|
32
|
-
# @example
|
33
|
-
#
|
34
|
-
# to_constantized_class("Authenticate", "Api") # => Api::Authenticate
|
35
|
-
# to_constantized_class(:Authenticate, [:Api, :AppName, :V1]) # => Api::AppName::V1::Authenticate
|
36
|
-
# to_constantized_class(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 })
|
37
|
-
# # => Api::AppName::V2::Authenticate
|
38
|
-
# to_constantized_class("authenticate", { :api :api, app_name: :app_name, api_version: :v1 },
|
39
|
-
# { class_titleize: true, module_titleize: true }) # => Api::AppName::V1::Authenticate
|
40
|
-
#
|
41
|
-
def to_constantized_class(klass, klass_modules = [], options = {})
|
42
|
-
constantized_class_string = to_constantized_class_string(klass, klass_modules, options)
|
43
|
-
|
44
|
-
begin
|
45
|
-
constantized_class_string.constantize
|
46
|
-
rescue StandardError
|
47
|
-
raise NameError, "\"#{constantized_class_string}\" is not a valid class constant."
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# Returns a fully-qualified constantized class (as a string), given the klass and klass_modules.
|
52
|
-
#
|
53
|
-
# @param [Symbol or String] klass the class name.
|
54
|
-
# @param [Hash, Array or String] klass_modules the modules klass belongs to.
|
55
|
-
# @param [Hash] options the options that determine how klass_modules is transformed.
|
56
|
-
# @option options [Boolean] :class_titleize (false) Determines whether or not klass should be
|
57
|
-
# titleized.
|
58
|
-
# @option options [Boolean] :module_titleize (false) Determines whether or not klass_modules
|
59
|
-
# should be titleized.
|
60
|
-
#
|
61
|
-
# @return [String] the fully qualified class, which includes module(s) and class name.
|
62
|
-
#
|
63
|
-
# @example
|
64
|
-
#
|
65
|
-
# to_constantized_class_string("Authenticate", "Api") # => "Api::Authenticate"
|
66
|
-
# to_constantized_class_string(:Authenticate, [:Api, :AppName, :V1]) # => "Api::AppName::V1::Authenticate"
|
67
|
-
# to_constantized_class_string(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 })
|
68
|
-
# # => "Api::AppName::V2::Authenticate"
|
69
|
-
# to_constantized_class_string("authenticate", { :api :api, app_name: :app_name, api_version: :v1 },
|
70
|
-
# { class_titleize: true, module_titleize: true }) # => "Api::AppName::V1::Authenticate"
|
71
|
-
#
|
72
|
-
def to_constantized_class_string(klass, klass_modules = [], options = {})
|
73
|
-
options = ensure_options(options)
|
74
|
-
klass_modules = to_modules_string(klass_modules, options)
|
75
|
-
klass_string = to_class_string(klass, options)
|
76
|
-
"#{klass_modules}#{klass_string}"
|
77
|
-
end
|
78
|
-
|
79
|
-
# Returns a string of modules that can be subsequently prepended to a class, to create a constantized class.
|
80
|
-
#
|
81
|
-
# @param [Hash, Array or String] klass_modules the modules a class belongs to.
|
82
|
-
# @param [Hash] options the options that determine how klass_modules is transformed.
|
83
|
-
# @option options [Boolean] :module_titleize (false) Determines whether or not klass_modules should be titleized.
|
84
|
-
#
|
85
|
-
# @return [String] a string of modules that can be subsequently prepended to a class, to create a
|
86
|
-
# constantized class.
|
87
|
-
#
|
88
|
-
# @raise [ArgumentError] if the klass_modules is not of type String, Hash or Array.
|
89
|
-
#
|
90
|
-
# @example
|
91
|
-
#
|
92
|
-
# to_modules_string("Api") # => "Api::"
|
93
|
-
# to_modules_string([:Api, :AppName, :V1]) # => "Api::AppName::V1::"
|
94
|
-
# to_modules_string({ :api :Api, app_name: :AppName, api_version: :V1 }) # => "Api::AppName::V1::"
|
95
|
-
# to_modules_string({ :api :api, app_name: :app_name, api_version: :v1 }, { module_titleize: true })
|
96
|
-
# # => "Api::AppName::V1::"
|
97
|
-
#
|
98
|
-
def to_modules_string(klass_modules = [], options = {}) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
|
99
|
-
klass_modules = validate_klass_modules(klass_modules)
|
100
|
-
|
101
|
-
options = ensure_options(options)
|
102
|
-
|
103
|
-
klass_modules_string = ''
|
104
|
-
unless klass_modules.empty?
|
105
|
-
case klass_modules
|
106
|
-
when String
|
107
|
-
klass_modules_string = klass_modules
|
108
|
-
when Array
|
109
|
-
klass_modules_string = klass_modules.join('::').to_s
|
110
|
-
when Hash
|
111
|
-
klass_modules_string = ''
|
112
|
-
klass_modules.to_a.each_with_index.map do |value, index|
|
113
|
-
klass_modules_string = index.zero? ? value[1].to_s : "#{klass_modules_string}::#{value[1]}"
|
114
|
-
end
|
115
|
-
else
|
116
|
-
raise ArgumentError, 'Class modules is not a String, Hash or Array.'
|
117
|
-
end
|
118
|
-
klass_modules_string = klass_modules_string.split('::').map(&:titleize).join('::') if options[:module_titleize]
|
119
|
-
klass_modules_string = camelize(klass_modules_string) if options[:module_camelize]
|
120
|
-
klass_modules_string = klass_modules_string.trim_all
|
121
|
-
klass_modules_string = "#{klass_modules_string}::" unless klass_modules_string.empty?
|
122
|
-
end
|
123
|
-
|
124
|
-
klass_modules_string
|
125
|
-
end
|
126
|
-
|
127
|
-
# Returns the klass as a string after transformations have been applied.
|
128
|
-
#
|
129
|
-
# @param [Symbol or String] klass the class name to be transformed.
|
130
|
-
# @param [Hash] options the options that determine how klass will be transformed.
|
131
|
-
# @option options [Boolean] :class_titleize (false) Determines whether or not klass should be titleized.
|
132
|
-
#
|
133
|
-
# @return [String] the transformed class as a string.
|
134
|
-
#
|
135
|
-
# @example
|
136
|
-
#
|
137
|
-
# to_class_string("MyClass") # => "MyClass"
|
138
|
-
# to_class_string("myClass", { class_titleize: true }) # => "MyClass"
|
139
|
-
# to_class_string(:MyClass) # => "MyClass"
|
140
|
-
# to_class_string(:myClass, { class_titleize: true }) # => "MyClass"
|
141
|
-
#
|
142
|
-
def to_class_string(klass, options = {})
|
143
|
-
klass = validate_klass(klass, options)
|
144
|
-
klass = klass.titleize if options[:class_titleize]
|
145
|
-
klass = camelize(klass) if options[:class_camelize]
|
146
|
-
klass
|
147
|
-
end
|
148
|
-
|
149
|
-
# Transforms a route into a module string
|
150
|
-
#
|
151
|
-
# @return [String] the camelized token.
|
152
|
-
#
|
153
|
-
# @example
|
154
|
-
#
|
155
|
-
# camelize("/api/app/auth/v1") # => "Api::App::Auth::V1"
|
156
|
-
# camelize("/api/app_name/auth/v1") # => "Api::AppName::Auth::V1"
|
157
|
-
#
|
158
|
-
def camelize(token)
|
159
|
-
raise ArgumentError, 'Token is not a String' unless token.instance_of? String
|
160
|
-
|
161
|
-
token.titlecase.camelize.sub(/^:*/, '').trim_all unless token.empty?
|
162
|
-
end
|
163
|
-
|
164
|
-
private
|
165
|
-
|
166
|
-
# @!visibility public
|
167
|
-
#
|
168
|
-
# Ensures options are initialized and valid before accessing them.
|
169
|
-
#
|
170
|
-
# @param [Hash] options the options that determine how processing and transformations will be handled.
|
171
|
-
# @option options [Boolean] :camelize (false) determines whether or not both class and module names
|
172
|
-
# should be camelized.
|
173
|
-
# @option options [Boolean] :titleize (false) determines whether or not both class and module names
|
174
|
-
# should be titleized.
|
175
|
-
# @option options [Boolean] :class_titleize (false) determines whether or not class names should be titleized.
|
176
|
-
# @option options [Boolean] :module_titleize (false) determines whether or not module names should be titleized.
|
177
|
-
# @option options [Boolean] :class_camelized (false) determines whether or not class names should be camelized.
|
178
|
-
# @option options [Boolean] :module_camelized (false) determines whether or not module names should be camelized.
|
179
|
-
#
|
180
|
-
# @return [Hash] the initialized, validated options.
|
181
|
-
#
|
182
|
-
def ensure_options(options)
|
183
|
-
options = {} unless options.instance_of? Hash
|
184
|
-
options = { camelize: false, titleize: false, class_titleize: false, module_titleize: false,
|
185
|
-
class_camelize: false, module_camelize: false }.merge(options)
|
186
|
-
|
187
|
-
options[:class_camelize] = options[:module_camelize] = true if options[:camelize]
|
188
|
-
|
189
|
-
options[:class_titleize] = options[:module_titleize] = true if options[:titleize]
|
190
|
-
|
191
|
-
options
|
192
|
-
end
|
193
|
-
|
194
|
-
# @!visibility public
|
195
|
-
#
|
196
|
-
# Validates klass and returns klass as a string after all blanks have been removed using klass.gsub(/\s+/, "").
|
197
|
-
#
|
198
|
-
# @param [Symbol or String] klass the class name to be validated. klass cannot be empty?
|
199
|
-
#
|
200
|
-
# @return [String] the validated class as a string with blanks removed.
|
201
|
-
#
|
202
|
-
# @raise [ArgumentError] if the klass is empty? or not of type String or Symbol.
|
203
|
-
#
|
204
|
-
# @example
|
205
|
-
#
|
206
|
-
# validate_klass(" My Class ") # => "MyClass"
|
207
|
-
# validate_klass(:MyClass) # => "MyClass"
|
208
|
-
#
|
209
|
-
def validate_klass(klass, _options)
|
210
|
-
unless klass.is_a?(Symbol) || klass.is_a?(String)
|
211
|
-
raise ArgumentError,
|
212
|
-
'Class is not a String or Symbol. Class must equal the class name of the ' \
|
213
|
-
'SimpleCommand or Command to call in the form of a String or Symbol.'
|
214
|
-
end
|
215
|
-
|
216
|
-
klass = klass.to_s.strip
|
217
|
-
|
218
|
-
raise ArgumentError, 'Class is empty?' if klass.empty?
|
219
|
-
|
220
|
-
klass
|
221
|
-
end
|
222
|
-
|
223
|
-
# @!visibility public
|
224
|
-
#
|
225
|
-
# Validates and returns klass_modules.
|
226
|
-
#
|
227
|
-
# @param [Symbol, Array or String] klass_modules the module(s) to be validated.
|
228
|
-
#
|
229
|
-
# @return [Symbol, Array or String] the validated module(s).
|
230
|
-
#
|
231
|
-
# @raise [ArgumentError] if the klass_modules is not of type String, Hash or Array.
|
232
|
-
#
|
233
|
-
# @example
|
234
|
-
#
|
235
|
-
# validate_modules(" Module ") # => " Module "
|
236
|
-
# validate_modules(:Module) # => "Module"
|
237
|
-
# validate_module("ModuleA::ModuleB") # => "ModuleA::ModuleB"
|
238
|
-
#
|
239
|
-
def validate_klass_modules(klass_modules)
|
240
|
-
return {} if klass_modules.nil? || (klass_modules.respond_to?(:empty?) && klass_modules.empty?)
|
241
|
-
|
242
|
-
if !klass_modules.instance_of?(String) && !klass_modules.instance_of?(Hash) && !klass_modules.instance_of?(Array)
|
243
|
-
raise ArgumentError, 'Class modules is not a String, Hash or Array.'
|
244
|
-
end
|
245
|
-
|
246
|
-
klass_modules
|
247
|
-
end
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
# rubocop:enable Style/OptionHash
|
@@ -1,222 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rake'
|
4
|
-
# Tasks to play with certain klass_transform module members
|
5
|
-
|
6
|
-
desc 'Test to_class_string'
|
7
|
-
task :to_class_string do
|
8
|
-
require 'colorize'
|
9
|
-
require 'simple_command_dispatcher'
|
10
|
-
|
11
|
-
class Test
|
12
|
-
prepend SimpleCommand::KlassTransform
|
13
|
-
end
|
14
|
-
|
15
|
-
puts "Testing SimpleCommand::KlassTransform#to_class_String...\n".cyan
|
16
|
-
puts 'Enter a blank line to exit.'.cyan
|
17
|
-
loop do
|
18
|
-
puts "\nUsage: \"-c [class], -o [options]\" where class = String or Symbol and options = Hash".cyan
|
19
|
-
puts "\n\t Examples:"
|
20
|
-
print "\n\t\t-c \"my_class\" -o { class_camelize: true }".cyan
|
21
|
-
print "\n\t\t-c :MyClass -o { class_camelize: false }".cyan
|
22
|
-
|
23
|
-
print "\n=> "
|
24
|
-
|
25
|
-
input = $stdin.gets.chomp
|
26
|
-
break if input.empty?
|
27
|
-
|
28
|
-
input = clean_input(input)
|
29
|
-
|
30
|
-
klass = get_option_class(input)
|
31
|
-
if klass.nil? || klass.empty?
|
32
|
-
print "=> Error: Class not a String or Symbol\n".red
|
33
|
-
next
|
34
|
-
elsif !klass.is_a?(String) && !klass.is_a?(Symbol)
|
35
|
-
print "=> Error: Class not a String or Symbol\n".red
|
36
|
-
next
|
37
|
-
end
|
38
|
-
|
39
|
-
options_hash = get_options_hash(input)
|
40
|
-
|
41
|
-
options_hash = { class_camelize: true }.merge(options_hash || {})
|
42
|
-
|
43
|
-
puts "\nCalling to_class_string with the following parameters: class: #{klass.class}, options: #{options_hash.class}".cyan
|
44
|
-
|
45
|
-
print "\nSuccess: => #to_class_String(#{klass}, #{options_hash}) => #{Test.new.to_class_string(klass,
|
46
|
-
options_hash)}\n".green
|
47
|
-
end
|
48
|
-
|
49
|
-
print
|
50
|
-
print 'Done'.yellow
|
51
|
-
print
|
52
|
-
end
|
53
|
-
|
54
|
-
desc 'Test to_modules_string'
|
55
|
-
task :to_modules_string do
|
56
|
-
require 'colorize'
|
57
|
-
require 'simple_command_dispatcher'
|
58
|
-
|
59
|
-
class Test
|
60
|
-
prepend SimpleCommand::KlassTransform
|
61
|
-
end
|
62
|
-
|
63
|
-
puts "Testing SimpleCommand::KlassTransform#to_modules_string...\n".cyan
|
64
|
-
puts 'Enter a blank line to exit.'.cyan
|
65
|
-
loop do
|
66
|
-
puts "\nUsage: \"-m [modules], -o [options]\" where modules = Hash, Array or String and options = Hash".cyan
|
67
|
-
puts "\n\t Examples:"
|
68
|
-
print "\n\t\t-m \"Module1::Module2::Module3\" -o { modules_camelize: false }".cyan
|
69
|
-
print "\n\t\t-m [:module1, :module2, :module3] -o { modules_camelize: true }".cyan
|
70
|
-
print "\n\t\t-m {api: :api, app: :crazy_buttons, version: :v1} -o { modules_camelize: true }".cyan
|
71
|
-
|
72
|
-
print "\n=> "
|
73
|
-
|
74
|
-
input = $stdin.gets.chomp
|
75
|
-
break if input.empty?
|
76
|
-
|
77
|
-
input = clean_input(input)
|
78
|
-
|
79
|
-
modules = get_option_modules(input)
|
80
|
-
p modules.class
|
81
|
-
if modules.nil? || modules.empty?
|
82
|
-
print "=> Error: Modules not a Hash, Array or String\n".red
|
83
|
-
next
|
84
|
-
elsif !modules.is_a?(Hash) && !modules.is_a?(Array) && !modules.is_a?(String)
|
85
|
-
print "=> Error: Modules not a Hash, Array or String\n".red
|
86
|
-
next
|
87
|
-
end
|
88
|
-
|
89
|
-
options_hash = get_options_hash(input)
|
90
|
-
options_hash = { module_camelize: true }.merge(options_hash || {})
|
91
|
-
|
92
|
-
puts "\nCalling to_modules_string with the following parameters: modules: #{modules}, type: #{modules.class}, options: #{options_hash}, type: #{options_hash.class}...".cyan
|
93
|
-
|
94
|
-
puts "\nSuccess: => #to_modules_string(#{modules}, #{options_hash}) => #{Test.new.to_modules_string(modules,
|
95
|
-
options_hash)}\n".green
|
96
|
-
end
|
97
|
-
|
98
|
-
print
|
99
|
-
print 'Done'.yellow
|
100
|
-
print
|
101
|
-
end
|
102
|
-
|
103
|
-
desc 'Test to_constantized_class_string'
|
104
|
-
task :to_constantized_class_string do
|
105
|
-
require 'colorize'
|
106
|
-
require 'simple_command_dispatcher'
|
107
|
-
|
108
|
-
class Test
|
109
|
-
prepend SimpleCommand::KlassTransform
|
110
|
-
end
|
111
|
-
|
112
|
-
puts "Testing SimpleCommand::KlassTransform#to_constantized_class_string...\n".cyan
|
113
|
-
puts 'Enter a blank line to exit.'.cyan
|
114
|
-
loop do
|
115
|
-
puts "\nUsage: \"-c [class] -m [modules], -o [options]\" where class = Symbol or String, modules = Hash, Array or String and options = Hash".cyan
|
116
|
-
puts "\n\t Examples:"
|
117
|
-
print "\n\t\t-c :MyClass -m \"Module1::Module2::Module3\" -o { modules_camelize: false }".cyan
|
118
|
-
print "\n\t\t-c \"my_class\" -m [:module1, :module2, :module3] -o { modules_camelize: true }".cyan
|
119
|
-
print "\n\t\t-c :myClass -m {api: :api, app: :crazy_buttons, version: :v1} -o { modules_camelize: true }".cyan
|
120
|
-
|
121
|
-
print "\n=> "
|
122
|
-
|
123
|
-
input = $stdin.gets.chomp
|
124
|
-
break if input.empty?
|
125
|
-
|
126
|
-
input = clean_input(input)
|
127
|
-
|
128
|
-
klass = get_option_class(input)
|
129
|
-
if klass.nil? || klass.empty?
|
130
|
-
print "=> Error: Class not a String or Symbol\n".red
|
131
|
-
next
|
132
|
-
elsif !klass.is_a?(String) && !klass.is_a?(Symbol)
|
133
|
-
print "=> Error: Class not a String or Symbol\n".red
|
134
|
-
next
|
135
|
-
end
|
136
|
-
|
137
|
-
modules = get_option_modules(input)
|
138
|
-
if modules.nil? || modules.empty?
|
139
|
-
print "=> Error: Modules not a Hash, Array or String\n".red
|
140
|
-
next
|
141
|
-
elsif !modules.is_a?(Hash) && !modules.is_a?(Array) && !modules.is_a?(String)
|
142
|
-
print "=> Error: Modules not a Hash, Array or String\n".red
|
143
|
-
next
|
144
|
-
end
|
145
|
-
|
146
|
-
options_hash = get_options_hash(input)
|
147
|
-
options_hash = { class_camelize: true, module_camelize: true }.merge(options_hash || {})
|
148
|
-
|
149
|
-
puts "\nCalling to_constantized_class_string with the following parameters: class: #{klass}, type: #{klass.class}, modules: #{modules}, type: #{modules.class}, options: #{options_hash}, type: #{options_hash.class}...".cyan
|
150
|
-
|
151
|
-
puts "\nSuccess: => #to_constantized_class_string(#{klass}, #{modules}, #{options_hash}) => #{Test.new.to_constantized_class_string(
|
152
|
-
klass, modules, options_hash
|
153
|
-
)}\n".green
|
154
|
-
end
|
155
|
-
|
156
|
-
print
|
157
|
-
print 'Done'.yellow
|
158
|
-
print
|
159
|
-
end
|
160
|
-
|
161
|
-
#
|
162
|
-
# Helper methods
|
163
|
-
#
|
164
|
-
|
165
|
-
def clean_input(input)
|
166
|
-
# Place a space before every dash that is not separated by a space.
|
167
|
-
input = input.gsub(/-c/, ' -c ').gsub(/-m/, ' -m ').gsub(/-o/, ' -o ')
|
168
|
-
input = input.gsub(/"/, ' " ').gsub(/\s+/, ' ').strip
|
169
|
-
|
170
|
-
puts "=> keyboard input after clean_input => #{input}".magenta
|
171
|
-
|
172
|
-
input
|
173
|
-
end
|
174
|
-
|
175
|
-
def get_option_class(options)
|
176
|
-
return '' if options.nil? || options.empty?
|
177
|
-
|
178
|
-
options_options = options[/-c\s*([:"].+?(["\s-]||$?))($|\s+|-)/m, 1]
|
179
|
-
|
180
|
-
return '' if options_options.nil?
|
181
|
-
|
182
|
-
options_options = options_options.gsub(/("|')+/, '')
|
183
|
-
klass = options_options.strip.gsub(/\s+/, ' ')
|
184
|
-
|
185
|
-
puts "=> class after get_option_class => #{klass}".magenta
|
186
|
-
|
187
|
-
klass
|
188
|
-
end
|
189
|
-
|
190
|
-
def get_options_hash(options)
|
191
|
-
return {} if options.nil? || options.empty?
|
192
|
-
|
193
|
-
options_options = options[/-o\s*([{].+?([}\s-]$?))($|\s+|-)/m, 1]
|
194
|
-
|
195
|
-
return {} if options_options.nil?
|
196
|
-
|
197
|
-
puts "=> options after get_options_hash => #{options_options}".magenta
|
198
|
-
|
199
|
-
begin
|
200
|
-
eval(options_options)
|
201
|
-
rescue StandardError
|
202
|
-
{}
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
def get_option_modules(modules)
|
207
|
-
return '' if modules.nil? || modules.empty?
|
208
|
-
|
209
|
-
extracted_modules = modules[/-m\s*([\[{"].+?(["}\]\s-]$?))($|\s+|-)/m, 1]
|
210
|
-
|
211
|
-
return '' if extracted_modules.nil?
|
212
|
-
|
213
|
-
extracted_modules = extracted_modules.strip.gsub(/\s+/, ' ')
|
214
|
-
|
215
|
-
puts "=> modules after get_option_modules => #{extracted_modules}".magenta
|
216
|
-
|
217
|
-
begin
|
218
|
-
eval(extracted_modules)
|
219
|
-
rescue StandardError
|
220
|
-
''
|
221
|
-
end
|
222
|
-
end
|