simple_command_dispatcher 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +12 -1
- data/.reek.yml +19 -0
- data/.rspec +1 -0
- data/.rubocop.yml +199 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +99 -0
- data/Jenkinsfile +20 -0
- data/README.md +2 -2
- data/Rakefile +6 -5
- data/bin/console +4 -3
- data/lib/core_extensions/string.rb +9 -7
- data/lib/simple_command_dispatcher/configuration.rb +38 -39
- data/lib/simple_command_dispatcher/configure.rb +16 -14
- data/lib/simple_command_dispatcher/klass_transform.rb +221 -230
- data/lib/simple_command_dispatcher/version.rb +5 -3
- data/lib/simple_command_dispatcher.rb +101 -108
- data/lib/tasks/simple_command_dispatcher_sandbox.rake +168 -178
- data/simple_command_dispatcher.gemspec +30 -27
- metadata +103 -40
@@ -1,240 +1,231 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative '../core_extensions/string'
|
2
4
|
|
3
5
|
module SimpleCommand
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
rescue
|
38
|
-
raise NameError.new("\"#{constantized_class_string}\" is not a valid class constant.")
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# Returns a fully-qualified constantized class (as a string), given the klass and klass_modules.
|
43
|
-
#
|
44
|
-
# @param [Symbol or String] klass the class name.
|
45
|
-
# @param [Hash, Array or String] klass_modules the modules klass belongs to.
|
46
|
-
# @param [Hash] options the options that determine how klass_modules is transformed.
|
47
|
-
# @option options [Boolean] :class_titleize (false) Determines whether or not klass should be titleized.
|
48
|
-
# @option options [Boolean] :module_titleize (false) Determines whether or not klass_modules should be titleized.
|
49
|
-
#
|
50
|
-
# @return [String] the fully qualified class, which includes module(s) and class name.
|
51
|
-
#
|
52
|
-
# @example
|
53
|
-
#
|
54
|
-
# to_constantized_class_string("Authenticate", "Api") # => "Api::Authenticate"
|
55
|
-
# to_constantized_class_string(:Authenticate, [:Api, :AppName, :V1]) # => "Api::AppName::V1::Authenticate"
|
56
|
-
# to_constantized_class_string(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 }) # => "Api::AppName::V2::Authenticate"
|
57
|
-
# to_constantized_class_string("authenticate", { :api :api, app_name: :app_name, api_version: :v1 }, { class_titleize: true, module_titleize: true }) # => "Api::AppName::V1::Authenticate"
|
58
|
-
#
|
59
|
-
def to_constantized_class_string(klass, klass_modules = [], options = {})
|
60
|
-
options = ensure_options(options)
|
61
|
-
klass_modules = to_modules_string(klass_modules, options)
|
62
|
-
klass_string = to_class_string(klass, options)
|
63
|
-
"#{klass_modules}#{klass_string}"
|
64
|
-
end
|
65
|
-
|
66
|
-
# Returns a string of modules that can be subsequently prepended to a class, to create a constantized class.
|
67
|
-
#
|
68
|
-
# @param [Hash, Array or String] klass_modules the modules a class belongs to.
|
69
|
-
# @param [Hash] options the options that determine how klass_modules is transformed.
|
70
|
-
# @option options [Boolean] :module_titleize (false) Determines whether or not klass_modules should be titleized.
|
71
|
-
#
|
72
|
-
# @return [String] a string of modules that can be subsequently prepended to a class, to create a constantized class.
|
73
|
-
#
|
74
|
-
# @raise [ArgumentError] if the klass_modules is not of type String, Hash or Array.
|
75
|
-
#
|
76
|
-
# @example
|
77
|
-
#
|
78
|
-
# to_modules_string("Api") # => "Api::"
|
79
|
-
# to_modules_string([:Api, :AppName, :V1]) # => "Api::AppName::V1::"
|
80
|
-
# to_modules_string({ :api :Api, app_name: :AppName, api_version: :V1 }) # => "Api::AppName::V1::"
|
81
|
-
# to_modules_string({ :api :api, app_name: :app_name, api_version: :v1 }, { module_titleize: true }) # => "Api::AppName::V1::"
|
82
|
-
#
|
83
|
-
def to_modules_string(klass_modules = [], options = {})
|
84
|
-
klass_modules = validate_klass_modules(klass_modules)
|
85
|
-
|
86
|
-
options = ensure_options(options)
|
87
|
-
|
88
|
-
klass_modules_string = ''
|
89
|
-
if !klass_modules.empty?
|
90
|
-
case klass_modules
|
91
|
-
when String
|
92
|
-
klass_modules_string = klass_modules
|
93
|
-
when Array
|
94
|
-
klass_modules_string = "#{klass_modules.join('::')}"
|
95
|
-
when Hash
|
96
|
-
klass_modules_string = ''
|
97
|
-
klass_modules.to_a.each_with_index.map { | value, index |
|
98
|
-
klass_modules_string = index == 0 ? value[1].to_s : "#{klass_modules_string}::#{value[1]}"
|
99
|
-
}
|
100
|
-
else
|
101
|
-
raise ArgumentError.new('Class modules is not a String, Hash or Array.')
|
102
|
-
end
|
103
|
-
klass_modules_string = klass_modules_string.split('::').map(&:titleize).join('::') if options[:module_titleize]
|
104
|
-
klass_modules_string = camelize(klass_modules_string) if options[:module_camelize]
|
105
|
-
klass_modules_string = klass_modules_string.trim_all
|
106
|
-
klass_modules_string = "#{klass_modules_string}::" unless klass_modules_string.empty?
|
107
|
-
end
|
108
|
-
|
109
|
-
klass_modules_string
|
6
|
+
# Handles class and module transformations.
|
7
|
+
module KlassTransform
|
8
|
+
# Returns a constantized class (as a Class constant), given the klass and klass_modules.
|
9
|
+
#
|
10
|
+
# @param klass [Symbol or String] the class name.
|
11
|
+
# @param klass_modules [Hash, Array or String] the modules klass belongs to.
|
12
|
+
# @param options [Hash] the options that determine how klass_modules is transformed.
|
13
|
+
# @option options [Boolean] :camelize (false) determines whether or not both klass and klass_modules should be camelized.
|
14
|
+
# @option options [Boolean] :titleize (false) determines whether or not both klass and klass_modules should be titleized.
|
15
|
+
# @option options [Boolean] :class_titleize (false) determines whether or not klass names should be titleized.
|
16
|
+
# @option options [Boolean] :class_camelized (false) determines whether or not klass names should be camelized.
|
17
|
+
# @option options [Boolean] :module_titleize (false) determines whether or not klass_modules names should be titleized.
|
18
|
+
# @option options [Boolean] :module_camelized (false) determines whether or not klass_modules names should be camelized.
|
19
|
+
#
|
20
|
+
# @return [Class] the class constant. Can be used to call ClassConstant.constantize.
|
21
|
+
#
|
22
|
+
# @raise [NameError] if the constantized class string cannot be constantized; that is, if it is not
|
23
|
+
# a valid class constant.
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
#
|
27
|
+
# to_constantized_class("Authenticate", "Api") # => Api::Authenticate
|
28
|
+
# to_constantized_class(:Authenticate, [:Api, :AppName, :V1]) # => Api::AppName::V1::Authenticate
|
29
|
+
# to_constantized_class(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 }) # => Api::AppName::V2::Authenticate
|
30
|
+
# to_constantized_class("authenticate", { :api :api, app_name: :app_name, api_version: :v1 }, { class_titleize: true, module_titleize: true }) # => Api::AppName::V1::Authenticate
|
31
|
+
#
|
32
|
+
def to_constantized_class(klass, klass_modules = [], options = {})
|
33
|
+
constantized_class_string = to_constantized_class_string(klass, klass_modules, options)
|
34
|
+
|
35
|
+
begin
|
36
|
+
constantized_class_string.constantize
|
37
|
+
rescue StandardError
|
38
|
+
raise NameError, "\"#{constantized_class_string}\" is not a valid class constant."
|
110
39
|
end
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a fully-qualified constantized class (as a string), given the klass and klass_modules.
|
43
|
+
#
|
44
|
+
# @param [Symbol or String] klass the class name.
|
45
|
+
# @param [Hash, Array or String] klass_modules the modules klass belongs to.
|
46
|
+
# @param [Hash] options the options that determine how klass_modules is transformed.
|
47
|
+
# @option options [Boolean] :class_titleize (false) Determines whether or not klass should be titleized.
|
48
|
+
# @option options [Boolean] :module_titleize (false) Determines whether or not klass_modules should be titleized.
|
49
|
+
#
|
50
|
+
# @return [String] the fully qualified class, which includes module(s) and class name.
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
#
|
54
|
+
# to_constantized_class_string("Authenticate", "Api") # => "Api::Authenticate"
|
55
|
+
# to_constantized_class_string(:Authenticate, [:Api, :AppName, :V1]) # => "Api::AppName::V1::Authenticate"
|
56
|
+
# to_constantized_class_string(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 }) # => "Api::AppName::V2::Authenticate"
|
57
|
+
# to_constantized_class_string("authenticate", { :api :api, app_name: :app_name, api_version: :v1 }, { class_titleize: true, module_titleize: true }) # => "Api::AppName::V1::Authenticate"
|
58
|
+
#
|
59
|
+
def to_constantized_class_string(klass, klass_modules = [], options = {})
|
60
|
+
options = ensure_options(options)
|
61
|
+
klass_modules = to_modules_string(klass_modules, options)
|
62
|
+
klass_string = to_class_string(klass, options)
|
63
|
+
"#{klass_modules}#{klass_string}"
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns a string of modules that can be subsequently prepended to a class, to create a constantized class.
|
67
|
+
#
|
68
|
+
# @param [Hash, Array or String] klass_modules the modules a class belongs to.
|
69
|
+
# @param [Hash] options the options that determine how klass_modules is transformed.
|
70
|
+
# @option options [Boolean] :module_titleize (false) Determines whether or not klass_modules should be titleized.
|
71
|
+
#
|
72
|
+
# @return [String] a string of modules that can be subsequently prepended to a class, to create a constantized class.
|
73
|
+
#
|
74
|
+
# @raise [ArgumentError] if the klass_modules is not of type String, Hash or Array.
|
75
|
+
#
|
76
|
+
# @example
|
77
|
+
#
|
78
|
+
# to_modules_string("Api") # => "Api::"
|
79
|
+
# to_modules_string([:Api, :AppName, :V1]) # => "Api::AppName::V1::"
|
80
|
+
# to_modules_string({ :api :Api, app_name: :AppName, api_version: :V1 }) # => "Api::AppName::V1::"
|
81
|
+
# to_modules_string({ :api :api, app_name: :app_name, api_version: :v1 }, { module_titleize: true }) # => "Api::AppName::V1::"
|
82
|
+
#
|
83
|
+
def to_modules_string(klass_modules = [], options = {})
|
84
|
+
klass_modules = validate_klass_modules(klass_modules)
|
85
|
+
|
86
|
+
options = ensure_options(options)
|
87
|
+
|
88
|
+
klass_modules_string = ''
|
89
|
+
unless klass_modules.empty?
|
90
|
+
case klass_modules
|
91
|
+
when String
|
92
|
+
klass_modules_string = klass_modules
|
93
|
+
when Array
|
94
|
+
klass_modules_string = klass_modules.join('::').to_s
|
95
|
+
when Hash
|
96
|
+
klass_modules_string = ''
|
97
|
+
klass_modules.to_a.each_with_index.map do |value, index|
|
98
|
+
klass_modules_string = index.zero? ? value[1].to_s : "#{klass_modules_string}::#{value[1]}"
|
99
|
+
end
|
100
|
+
else
|
101
|
+
raise ArgumentError, 'Class modules is not a String, Hash or Array.'
|
102
|
+
end
|
103
|
+
klass_modules_string = klass_modules_string.split('::').map(&:titleize).join('::') if options[:module_titleize]
|
104
|
+
klass_modules_string = camelize(klass_modules_string) if options[:module_camelize]
|
105
|
+
klass_modules_string = klass_modules_string.trim_all
|
106
|
+
klass_modules_string = "#{klass_modules_string}::" unless klass_modules_string.empty?
|
136
107
|
end
|
137
108
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
109
|
+
klass_modules_string
|
110
|
+
end
|
111
|
+
|
112
|
+
# Returns the klass as a string after transformations have been applied.
|
113
|
+
#
|
114
|
+
# @param [Symbol or String] klass the class name to be transformed.
|
115
|
+
# @param [Hash] options the options that determine how klass will be transformed.
|
116
|
+
# @option options [Boolean] :class_titleize (false) Determines whether or not klass should be titleized.
|
117
|
+
#
|
118
|
+
# @return [String] the transformed class as a string.
|
119
|
+
#
|
120
|
+
# @example
|
121
|
+
#
|
122
|
+
# to_class_string("MyClass") # => "MyClass"
|
123
|
+
# to_class_string("myClass", { class_titleize: true }) # => "MyClass"
|
124
|
+
# to_class_string(:MyClass) # => "MyClass"
|
125
|
+
# to_class_string(:myClass, { class_titleize: true }) # => "MyClass"
|
126
|
+
#
|
127
|
+
def to_class_string(klass, options = {})
|
128
|
+
klass = validate_klass(klass, options)
|
129
|
+
klass = klass.titleize if options[:class_titleize]
|
130
|
+
klass = camelize(klass) if options[:class_camelize]
|
131
|
+
klass
|
132
|
+
end
|
133
|
+
|
134
|
+
# Transforms a route into a module string
|
135
|
+
#
|
136
|
+
# @return [String] the camelized token.
|
137
|
+
#
|
138
|
+
# @example
|
139
|
+
#
|
140
|
+
# camelize("/api/app/auth/v1") # => "Api::App::Auth::V1"
|
141
|
+
# camelize("/api/app_name/auth/v1") # => "Api::AppName::Auth::V1"
|
142
|
+
#
|
143
|
+
def camelize(token)
|
144
|
+
raise ArgumentError, 'Token is not a String' unless token.instance_of? String
|
145
|
+
|
146
|
+
token = token.titlecase.camelize.sub(/^:*/, '').trim_all unless token.empty?
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
# @!visibility public
|
152
|
+
#
|
153
|
+
# Ensures options are initialized and valid before accessing them.
|
154
|
+
#
|
155
|
+
# @param [Hash] options the options that determine how processing and transformations will be handled.
|
156
|
+
# @option options [Boolean] :camelize (false) determines whether or not both class and module names should be camelized.
|
157
|
+
# @option options [Boolean] :titleize (false) determines whether or not both class and module names should be titleized.
|
158
|
+
# @option options [Boolean] :class_titleize (false) determines whether or not class names should be titleized.
|
159
|
+
# @option options [Boolean] :module_titleize (false) determines whether or not module names should be titleized.
|
160
|
+
# @option options [Boolean] :class_camelized (false) determines whether or not class names should be camelized.
|
161
|
+
# @option options [Boolean] :module_camelized (false) determines whether or not module names should be camelized.
|
162
|
+
#
|
163
|
+
# @return [Hash] the initialized, validated options.
|
164
|
+
#
|
165
|
+
def ensure_options(options)
|
166
|
+
options = {} unless options.instance_of? Hash
|
167
|
+
options = { camelize: false, titleize: false, class_titleize: false, module_titleize: false,
|
168
|
+
class_camelize: false, module_camelize: false }.merge(options)
|
169
|
+
|
170
|
+
options[:class_camelize] = options[:module_camelize] = true if options[:camelize]
|
171
|
+
|
172
|
+
options[:class_titleize] = options[:module_titleize] = true if options[:titleize]
|
173
|
+
|
174
|
+
options
|
175
|
+
end
|
176
|
+
|
177
|
+
# @!visibility public
|
178
|
+
#
|
179
|
+
# Validates klass and returns klass as a string after all blanks have been removed using klass.gsub(/\s+/, "").
|
180
|
+
#
|
181
|
+
# @param [Symbol or String] klass the class name to be validated. klass cannot be empty?
|
182
|
+
#
|
183
|
+
# @return [String] the validated class as a string with blanks removed.
|
184
|
+
#
|
185
|
+
# @raise [ArgumentError] if the klass is empty? or not of type String or Symbol.
|
186
|
+
#
|
187
|
+
# @example
|
188
|
+
#
|
189
|
+
# validate_klass(" My Class ") # => "MyClass"
|
190
|
+
# validate_klass(:MyClass) # => "MyClass"
|
191
|
+
#
|
192
|
+
def validate_klass(klass, _options)
|
193
|
+
unless klass.is_a?(Symbol) || klass.is_a?(String)
|
194
|
+
raise ArgumentError,
|
195
|
+
'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.'
|
152
196
|
end
|
153
197
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
options
|
198
|
+
klass = klass.to_s.strip
|
199
|
+
|
200
|
+
raise ArgumentError, 'Class is empty?' if klass.empty?
|
201
|
+
|
202
|
+
klass
|
203
|
+
end
|
204
|
+
|
205
|
+
# @!visibility public
|
206
|
+
#
|
207
|
+
# Validates and returns klass_modules.
|
208
|
+
#
|
209
|
+
# @param [Symbol, Array or String] klass_modules the module(s) to be validated.
|
210
|
+
#
|
211
|
+
# @return [Symbol, Array or String] the validated module(s).
|
212
|
+
#
|
213
|
+
# @raise [ArgumentError] if the klass_modules is not of type String, Hash or Array.
|
214
|
+
#
|
215
|
+
# @example
|
216
|
+
#
|
217
|
+
# validate_modules(" Module ") # => " Module "
|
218
|
+
# validate_modules(:Module) # => "Module"
|
219
|
+
# validate_module("ModuleA::ModuleB") # => "ModuleA::ModuleB"
|
220
|
+
#
|
221
|
+
def validate_klass_modules(klass_modules)
|
222
|
+
return {} if klass_modules.nil? || (klass_modules.respond_to?(:empty?) && klass_modules.empty?)
|
223
|
+
|
224
|
+
if !klass_modules.instance_of?(String) && !klass_modules.instance_of?(Hash) && !klass_modules.instance_of?(Array)
|
225
|
+
raise ArgumentError, 'Class modules is not a String, Hash or Array.'
|
183
226
|
end
|
184
227
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
# @param [Symbol or String] klass the class name to be validated. klass cannot be empty?
|
190
|
-
#
|
191
|
-
# @return [String] the validated class as a string with blanks removed.
|
192
|
-
#
|
193
|
-
# @raise [ArgumentError] if the klass is empty? or not of type String or Symbol.
|
194
|
-
#
|
195
|
-
# @example
|
196
|
-
#
|
197
|
-
# validate_klass(" My Class ") # => "MyClass"
|
198
|
-
# validate_klass(:MyClass) # => "MyClass"
|
199
|
-
#
|
200
|
-
def validate_klass(klass, options)
|
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 class name of the SimpleCommand or Command to call in the form of a String or Symbol.')
|
203
|
-
end
|
204
|
-
|
205
|
-
klass = klass.to_s.strip
|
206
|
-
|
207
|
-
if klass.empty?
|
208
|
-
raise ArgumentError.new('Class is empty?')
|
209
|
-
end
|
210
|
-
|
211
|
-
klass
|
212
|
-
end
|
213
|
-
|
214
|
-
# @!visibility public
|
215
|
-
#
|
216
|
-
# Validates and returns klass_modules.
|
217
|
-
#
|
218
|
-
# @param [Symbol, Array or String] klass_modules the module(s) to be validated.
|
219
|
-
#
|
220
|
-
# @return [Symbol, Array or String] the validated module(s).
|
221
|
-
#
|
222
|
-
# @raise [ArgumentError] if the klass_modules is not of type String, Hash or Array.
|
223
|
-
#
|
224
|
-
# @example
|
225
|
-
#
|
226
|
-
# validate_modules(" Module ") # => " Module "
|
227
|
-
# validate_modules(:Module) # => "Module"
|
228
|
-
# validate_module("ModuleA::ModuleB") # => "ModuleA::ModuleB"
|
229
|
-
#
|
230
|
-
def validate_klass_modules(klass_modules)
|
231
|
-
return {} if klass_modules.nil? || (klass_modules.respond_to?(:empty?) && klass_modules.empty?)
|
232
|
-
|
233
|
-
if !klass_modules.instance_of?(String) && !klass_modules.instance_of?(Hash) && !klass_modules.instance_of?(Array)
|
234
|
-
raise ArgumentError.new('Class modules is not a String, Hash or Array.')
|
235
|
-
end
|
236
|
-
|
237
|
-
klass_modules
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
228
|
+
klass_modules
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|