simple_command_dispatcher 1.2.2 → 1.2.5
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 +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 +14 -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 +18 -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 +100 -109
- data/lib/tasks/simple_command_dispatcher_sandbox.rake +168 -178
- data/simple_command_dispatcher.gemspec +30 -26
- metadata +109 -45
@@ -1,9 +1,9 @@
|
|
1
|
-
|
2
|
-
require "simple_command_dispatcher/klass_transform"
|
3
|
-
require "simple_command"
|
4
|
-
require "active_support/core_ext/string/inflections"
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
require 'simple_command_dispatcher/
|
3
|
+
require 'simple_command_dispatcher/version'
|
4
|
+
require 'simple_command_dispatcher/klass_transform'
|
5
|
+
require 'simple_command'
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
7
7
|
require 'simple_command_dispatcher/configure'
|
8
8
|
|
9
9
|
module Kernel
|
@@ -15,116 +15,107 @@ module Kernel
|
|
15
15
|
end
|
16
16
|
|
17
17
|
module SimpleCommand
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
class << self
|
26
|
-
include SimpleCommand::KlassTransform
|
27
|
-
|
28
|
-
public
|
29
|
-
|
30
|
-
# Calls a *SimpleCommand* or *Command* given the command name, the modules the command belongs to and the parameters to pass to the command.
|
31
|
-
#
|
32
|
-
# @param command [Symbol, String] the name of the SimpleCommand or Command to call.
|
33
|
-
#
|
34
|
-
# @param command_modules [Hash, Array] the ruby modules that qualify the SimpleCommand to call. When passing a Hash, the Hash
|
35
|
-
# keys serve as documentation only. For example, ['Api', 'AppName', 'V1'] and { :api :Api, app_name: :AppName, api_version: :V1 }
|
36
|
-
# will both produce 'Api::AppName::V1', this string will be prepended to the command to form the SimpleCommand to call
|
37
|
-
# (e.g. 'Api::AppName::V1::MySimpleCommand' = Api::AppName::V1::MySimpleCommand.call(*command_parameters)).
|
38
|
-
#
|
39
|
-
# @param [Hash] options the options that determine how command and command_module are transformed.
|
40
|
-
# @option options [Boolean] :camelize (false) determines whether or not both class and module names should be camelized.
|
41
|
-
# @option options [Boolean] :titleize (false) determines whether or not both class and module names should be titleized.
|
42
|
-
# @option options [Boolean] :class_titleize (false) determines whether or not class names should be titleized.
|
43
|
-
# @option options [Boolean] :class_camelized (false) determines whether or not class names should be camelized.
|
44
|
-
# @option options [Boolean] :module_titleize (false) determines whether or not module names should be titleized.
|
45
|
-
# @option options [Boolean] :module_camelized (false) determines whether or not module names should be camelized.
|
46
|
-
#
|
47
|
-
# @param command_parameters [Array<Symbol>] the parameters to pass to the call method of the SimpleCommand . This parameter is simply
|
48
|
-
# passed through to the call method of the SimpleCommand/Command.
|
49
|
-
#
|
50
|
-
# @return [SimpleCommand, Object] the SimpleCommand or Object returned as a result of calling the SimpleCommand#call method or the Command#call method respectfully.
|
51
|
-
#
|
52
|
-
# @example
|
53
|
-
#
|
54
|
-
# # Below call equates to the following: Api::Carz4Rent::V1::Authenticate.call({ email: 'sam@gmail.com', password: 'AskM3!' })
|
55
|
-
# SimpleCommand::Dispatcher.call(:Authenticate, { api: :Api, app_name: :Carz4Rent, api_version: :V1 },
|
56
|
-
# { email: 'sam@gmail.com', password: 'AskM3!' } ) # => SimpleCommand result
|
57
|
-
#
|
58
|
-
# # Below equates to the following: Api::Carz4Rent::V2::Authenticate.call('sam@gmail.com', 'AskM3!')
|
59
|
-
# SimpleCommand::Dispatcher.call(:Authenticate, ['Api', 'Carz4Rent', 'V2'], 'sam@gmail.com', 'AskM3!') # => SimpleCommand result
|
60
|
-
#
|
61
|
-
# # Below equates to the following: Api::Auth::JazzMeUp::V1::Authenticate.call('jazz_me@gmail.com', 'JazzM3!')
|
62
|
-
# SimpleCommand::Dispatcher.call(:Authenticate, ['Api::Auth::JazzMeUp', :V1], 'jazz_me@gmail.com', 'JazzM3!') # => SimpleCommand result
|
63
|
-
#
|
64
|
-
def call(command = "", command_modules = {}, options = {}, *command_parameters)
|
18
|
+
# Provides a way to call SimpleCommands or your own custom commands in a more dymanic manner.
|
19
|
+
#
|
20
|
+
# For information about the simple_command gem, visit {https://rubygems.org/gems/simple_command}
|
21
|
+
#
|
22
|
+
module Dispatcher
|
23
|
+
class << self
|
24
|
+
include SimpleCommand::KlassTransform
|
65
25
|
|
66
|
-
|
67
|
-
|
26
|
+
# Calls a *SimpleCommand* or *Command* given the command name, the modules the command belongs to and the parameters to pass to the command.
|
27
|
+
#
|
28
|
+
# @param command [Symbol, String] the name of the SimpleCommand or Command to call.
|
29
|
+
#
|
30
|
+
# @param command_modules [Hash, Array] the ruby modules that qualify the SimpleCommand to call. When passing a Hash, the Hash
|
31
|
+
# keys serve as documentation only. For example, ['Api', 'AppName', 'V1'] and { :api :Api, app_name: :AppName, api_version: :V1 }
|
32
|
+
# will both produce 'Api::AppName::V1', this string will be prepended to the command to form the SimpleCommand to call
|
33
|
+
# (e.g. 'Api::AppName::V1::MySimpleCommand' = Api::AppName::V1::MySimpleCommand.call(*command_parameters)).
|
34
|
+
#
|
35
|
+
# @param [Hash] options the options that determine how command and command_module are transformed.
|
36
|
+
# @option options [Boolean] :camelize (false) determines whether or not both class and module names should be camelized.
|
37
|
+
# @option options [Boolean] :titleize (false) determines whether or not both class and module names should be titleized.
|
38
|
+
# @option options [Boolean] :class_titleize (false) determines whether or not class names should be titleized.
|
39
|
+
# @option options [Boolean] :class_camelized (false) determines whether or not class names should be camelized.
|
40
|
+
# @option options [Boolean] :module_titleize (false) determines whether or not module names should be titleized.
|
41
|
+
# @option options [Boolean] :module_camelized (false) determines whether or not module names should be camelized.
|
42
|
+
#
|
43
|
+
# @param command_parameters [Array<Symbol>] the parameters to pass to the call method of the SimpleCommand . This parameter is simply
|
44
|
+
# passed through to the call method of the SimpleCommand/Command.
|
45
|
+
#
|
46
|
+
# @return [SimpleCommand, Object] the SimpleCommand or Object returned as a result of calling the SimpleCommand#call method or the Command#call method respectfully.
|
47
|
+
#
|
48
|
+
# @example
|
49
|
+
#
|
50
|
+
# # Below call equates to the following: Api::Carz4Rent::V1::Authenticate.call({ email: 'sam@gmail.com', password: 'AskM3!' })
|
51
|
+
# SimpleCommand::Dispatcher.call(:Authenticate, { api: :Api, app_name: :Carz4Rent, api_version: :V1 },
|
52
|
+
# { email: 'sam@gmail.com', password: 'AskM3!' } ) # => SimpleCommand result
|
53
|
+
#
|
54
|
+
# # Below equates to the following: Api::Carz4Rent::V2::Authenticate.call('sam@gmail.com', 'AskM3!')
|
55
|
+
# SimpleCommand::Dispatcher.call(:Authenticate, ['Api', 'Carz4Rent', 'V2'], 'sam@gmail.com', 'AskM3!') # => SimpleCommand result
|
56
|
+
#
|
57
|
+
# # Below equates to the following: Api::Auth::JazzMeUp::V1::Authenticate.call('jazz_me@gmail.com', 'JazzM3!')
|
58
|
+
# SimpleCommand::Dispatcher.call(:Authenticate, ['Api::Auth::JazzMeUp', :V1], 'jazz_me@gmail.com', 'JazzM3!') # => SimpleCommand result
|
59
|
+
#
|
60
|
+
def call(command = '', command_modules = {}, options = {}, *command_parameters)
|
61
|
+
# Create a constantized class from our command and command_modules...
|
62
|
+
command_class_constant = to_constantized_class(command, command_modules, options)
|
68
63
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
if !is_simple_command?(command_class_constant)
|
76
|
-
raise ArgumentError.new("Class \"#{command_class_constant}\" must prepend module SimpleCommand if Configuration#allow_custom_commands is true.")
|
77
|
-
end
|
78
|
-
end
|
64
|
+
# If we're NOT allowing custom commands, make sure we're dealing with a a command class
|
65
|
+
# that prepends the SimpleCommand module.
|
66
|
+
if !SimpleCommand::Dispatcher.configuration.allow_custom_commands && !is_simple_command?(command_class_constant)
|
67
|
+
raise ArgumentError,
|
68
|
+
"Class \"#{command_class_constant}\" must prepend module SimpleCommand if Configuration#allow_custom_commands is true."
|
69
|
+
end
|
79
70
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
71
|
+
if is_valid_command(command_class_constant)
|
72
|
+
# We know we have a valid SimpleCommand; all we need to do is call #call,
|
73
|
+
# pass the command_parameter variable arguments to the call, and return the results.
|
74
|
+
run_command(command_class_constant, command_parameters)
|
75
|
+
else
|
76
|
+
raise NameError, "Class \"#{command_class_constant}\" does not respond_to? method ::call."
|
77
|
+
end
|
78
|
+
end
|
88
79
|
|
89
|
-
|
80
|
+
private
|
90
81
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
82
|
+
# Returns true or false depending on whether or not the class constant has a public
|
83
|
+
# class method named ::call defined. Commands that do not have a public class method
|
84
|
+
# named ::call, are considered invalid.
|
85
|
+
#
|
86
|
+
# @param klass_constant [String] a class constant that will be validated to see whether or not the class is a valid command.
|
87
|
+
#
|
88
|
+
# @return [Boolean] true if klass_constant has a public class method named ::call defined, false otherwise.
|
89
|
+
#
|
90
|
+
# @!visibility public
|
91
|
+
def is_valid_command(klass_constant)
|
92
|
+
klass_constant.eigenclass.public_method_defined?(:call)
|
93
|
+
end
|
103
94
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
95
|
+
# Returns true or false depending on whether or not the class constant prepends module SimpleCommand::ClassMethods.
|
96
|
+
#
|
97
|
+
# @param klass_constant [String] a class constant that will be validated to see whether or not the class prepends module SimpleCommand::ClassMethods.
|
98
|
+
#
|
99
|
+
# @return [Boolean] true if klass_constant prepends Module SimpleCommand::ClassMethods, false otherwise.
|
100
|
+
#
|
101
|
+
# @!visibility public
|
102
|
+
def is_simple_command?(klass_constant)
|
103
|
+
klass_constant.eigenclass.included_modules.include? SimpleCommand::ClassMethods
|
104
|
+
end
|
114
105
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
end
|
106
|
+
# Runs the command given the parameters and returns the result.
|
107
|
+
#
|
108
|
+
# @param klass_constant [String] a class constant that will be called.
|
109
|
+
# @param parameters [Array] an array of parameters to pass to the command that will be called.
|
110
|
+
#
|
111
|
+
# @return [Object] returns the object (if any) that results from calling the command.
|
112
|
+
#
|
113
|
+
# @!visibility public
|
114
|
+
def run_command(klass_constant, parameters)
|
115
|
+
klass_constant.call(*parameters)
|
116
|
+
# rescue NameError
|
117
|
+
# raise NameError.new("Class \"#{klass_constant}\" does not respond_to? method ::call.")
|
128
118
|
end
|
129
|
-
|
119
|
+
end
|
120
|
+
end
|
130
121
|
end
|
@@ -1,232 +1,222 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rake'
|
2
4
|
# Tasks to play with certain klass_transform module members
|
3
5
|
|
4
|
-
|
5
6
|
desc 'Test to_class_string'
|
6
7
|
task :to_class_string do
|
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
|
-
|
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
|
37
38
|
|
38
|
-
|
39
|
+
options_hash = get_options_hash(input)
|
39
40
|
|
40
|
-
|
41
|
+
options_hash = { class_camelize: true }.merge(options_hash || {})
|
41
42
|
|
42
|
-
|
43
|
+
puts "\nCalling to_class_string with the following parameters: class: #{klass.class}, options: #{options_hash.class}".cyan
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
print "\nSuccess: => #to_class_String(#{klass}, #{options_hash}) => #{Test.new.to_class_string(klass,
|
46
|
+
options_hash)}\n".green
|
47
|
+
end
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
print
|
50
|
+
print 'Done'.yellow
|
51
|
+
print
|
50
52
|
end
|
51
53
|
|
52
|
-
|
53
54
|
desc 'Test to_modules_string'
|
54
55
|
task :to_modules_string do
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
options_hash = get_options_hash(input)
|
89
|
-
options_hash = { module_camelize: true }.merge(options_hash || {})
|
90
|
-
|
91
|
-
puts "\nCalling to_modules_string with the following parameters: modules: #{modules}, type: #{modules.class}, options: #{options_hash}, type: #{options_hash.class}...".cyan
|
92
|
-
|
93
|
-
puts "\nSuccess: => #to_modules_string(#{modules}, #{options_hash}) => #{Test.new.to_modules_string(modules, options_hash)}\n".green
|
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
|
94
87
|
end
|
95
88
|
|
96
|
-
|
97
|
-
|
98
|
-
print
|
89
|
+
options_hash = get_options_hash(input)
|
90
|
+
options_hash = { module_camelize: true }.merge(options_hash || {})
|
99
91
|
|
100
|
-
|
92
|
+
puts "\nCalling to_modules_string with the following parameters: modules: #{modules}, type: #{modules.class}, options: #{options_hash}, type: #{options_hash.class}...".cyan
|
101
93
|
|
94
|
+
puts "\nSuccess: => #to_modules_string(#{modules}, #{options_hash}) => #{Test.new.to_modules_string(modules,
|
95
|
+
options_hash)}\n".green
|
96
|
+
end
|
102
97
|
|
103
|
-
|
104
|
-
|
105
|
-
|
98
|
+
print
|
99
|
+
print 'Done'.yellow
|
100
|
+
print
|
101
|
+
end
|
106
102
|
|
107
103
|
desc 'Test to_constantized_class_string'
|
108
104
|
task :to_constantized_class_string do
|
109
|
-
|
110
|
-
|
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
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
modules = get_option_modules(input)
|
142
|
-
if modules.nil? || modules.empty?
|
143
|
-
print "=> Error: Modules not a Hash, Array or String\n".red
|
144
|
-
next
|
145
|
-
elsif !modules.is_a?(Hash) && !modules.is_a?(Array) && !modules.is_a?(String)
|
146
|
-
print "=> Error: Modules not a Hash, Array or String\n".red
|
147
|
-
next
|
148
|
-
end
|
149
|
-
|
150
|
-
options_hash = get_options_hash(input)
|
151
|
-
options_hash = { class_camelize: true, module_camelize: true }.merge(options_hash || {})
|
152
|
-
|
153
|
-
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
|
154
|
-
|
155
|
-
puts "\nSuccess: => #to_constantized_class_string(#{klass}, #{modules}, #{options_hash}) => #{Test.new.to_constantized_class_string(klass, modules, options_hash)}\n".green
|
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
|
156
135
|
end
|
157
136
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
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 || {})
|
162
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
|
163
160
|
|
164
161
|
#
|
165
162
|
# Helper methods
|
166
|
-
#
|
163
|
+
#
|
167
164
|
|
168
165
|
def clean_input(input)
|
169
|
-
|
170
|
-
|
171
|
-
|
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
|
172
169
|
|
173
|
-
|
170
|
+
puts "=> keyboard input after clean_input => #{input}".magenta
|
174
171
|
|
175
|
-
|
172
|
+
input
|
176
173
|
end
|
177
174
|
|
178
175
|
def get_option_class(options)
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
options_options = options[/-c\s*([:"].+?(["\s-]||$?))($|\s+|-)/m,1]
|
176
|
+
return '' if options.nil? || options.empty?
|
177
|
+
|
178
|
+
options_options = options[/-c\s*([:"].+?(["\s-]||$?))($|\s+|-)/m, 1]
|
183
179
|
|
184
|
-
|
180
|
+
return '' if options_options.nil?
|
185
181
|
|
186
|
-
|
187
|
-
|
182
|
+
options_options = options_options.gsub(/("|')+/, '')
|
183
|
+
klass = options_options.strip.gsub(/\s+/, ' ')
|
188
184
|
|
189
|
-
|
185
|
+
puts "=> class after get_option_class => #{klass}".magenta
|
190
186
|
|
191
|
-
|
187
|
+
klass
|
192
188
|
end
|
193
189
|
|
194
190
|
def get_options_hash(options)
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
options_options = options[/-o\s*([{].+?([}\s-]$?))($|\s+|-)/m,1]
|
191
|
+
return {} if options.nil? || options.empty?
|
192
|
+
|
193
|
+
options_options = options[/-o\s*([{].+?([}\s-]$?))($|\s+|-)/m, 1]
|
199
194
|
|
200
|
-
|
195
|
+
return {} if options_options.nil?
|
201
196
|
|
202
|
-
|
197
|
+
puts "=> options after get_options_hash => #{options_options}".magenta
|
203
198
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
199
|
+
begin
|
200
|
+
eval(options_options)
|
201
|
+
rescue StandardError
|
202
|
+
{}
|
203
|
+
end
|
209
204
|
end
|
210
205
|
|
211
206
|
def get_option_modules(modules)
|
212
|
-
|
213
|
-
return ""
|
214
|
-
end
|
207
|
+
return '' if modules.nil? || modules.empty?
|
215
208
|
|
216
|
-
|
209
|
+
extracted_modules = modules[/-m\s*([\[{"].+?(["}\]\s-]$?))($|\s+|-)/m, 1]
|
217
210
|
|
218
|
-
|
211
|
+
return '' if extracted_modules.nil?
|
219
212
|
|
220
|
-
|
213
|
+
extracted_modules = extracted_modules.strip.gsub(/\s+/, ' ')
|
221
214
|
|
222
|
-
|
215
|
+
puts "=> modules after get_option_modules => #{extracted_modules}".magenta
|
223
216
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
217
|
+
begin
|
218
|
+
eval(extracted_modules)
|
219
|
+
rescue StandardError
|
220
|
+
''
|
221
|
+
end
|
229
222
|
end
|
230
|
-
|
231
|
-
|
232
|
-
|