Module: SimpleCommand::KlassTransform
- Included in:
- Dispatcher
- Defined in:
- lib/simple_command_dispatcher/klass_transform.rb
Overview
Handles class and module transformations.
Instance Method Summary collapse
-
#camelize(token) ⇒ String
Transforms a route into a module string.
-
#ensure_options(options) ⇒ Hash
Ensures options are initialized and valid before accessing them.
-
#to_class_string(klass, options = {}) ⇒ String
Returns the klass as a string after transformations have been applied.
-
#to_constantized_class(klass, klass_modules = [], options = {}) ⇒ Class
Returns a constantized class (as a Class constant), given the klass and klass_modules.
-
#to_constantized_class_string(klass, klass_modules = [], options = {}) ⇒ String
Returns a fully-qualified constantized class (as a string), given the klass and klass_modules.
-
#to_modules_string(klass_modules = [], options = {}) ⇒ String
Returns a string of modules that can be subsequently prepended to a class, to create a constantized class.
-
#validate_klass(klass, _options) ⇒ String
Validates klass and returns klass as a string after all blanks have been removed using klass.gsub(/s+/, “”).
-
#validate_klass_modules(klass_modules) ⇒ Symbol, Array or String
Validates and returns klass_modules.
Instance Method Details
#camelize(token) ⇒ String
Transforms a route into a module string
143 144 145 146 147 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 143 def camelize(token) raise ArgumentError, 'Token is not a String' unless token.instance_of? String token = token.titlecase.camelize.sub(/^:*/, '').trim_all unless token.empty? end |
#ensure_options(options) ⇒ Hash
Ensures options are initialized and valid before accessing them.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 165 def () = {} unless .instance_of? Hash = { camelize: false, titleize: false, class_titleize: false, module_titleize: false, class_camelize: false, module_camelize: false }.merge() [:class_camelize] = [:module_camelize] = true if [:camelize] [:class_titleize] = [:module_titleize] = true if [:titleize] end |
#to_class_string(klass, options = {}) ⇒ String
Returns the klass as a string after transformations have been applied.
127 128 129 130 131 132 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 127 def to_class_string(klass, = {}) klass = validate_klass(klass, ) klass = klass.titleize if [:class_titleize] klass = camelize(klass) if [:class_camelize] klass end |
#to_constantized_class(klass, klass_modules = [], options = {}) ⇒ Class
Returns a constantized class (as a Class constant), given the klass and klass_modules.
32 33 34 35 36 37 38 39 40 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 32 def to_constantized_class(klass, klass_modules = [], = {}) constantized_class_string = to_constantized_class_string(klass, klass_modules, ) begin constantized_class_string.constantize rescue StandardError raise NameError, "\"#{constantized_class_string}\" is not a valid class constant." end end |
#to_constantized_class_string(klass, klass_modules = [], options = {}) ⇒ String
Returns a fully-qualified constantized class (as a string), given the klass and klass_modules.
59 60 61 62 63 64 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 59 def to_constantized_class_string(klass, klass_modules = [], = {}) = () klass_modules = to_modules_string(klass_modules, ) klass_string = to_class_string(klass, ) "#{klass_modules}#{klass_string}" end |
#to_modules_string(klass_modules = [], options = {}) ⇒ String
Returns a string of modules that can be subsequently prepended to a class, to create a constantized class.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 83 def to_modules_string(klass_modules = [], = {}) klass_modules = validate_klass_modules(klass_modules) = () klass_modules_string = '' unless klass_modules.empty? case klass_modules when String klass_modules_string = klass_modules when Array klass_modules_string = klass_modules.join('::').to_s when Hash klass_modules_string = '' klass_modules.to_a.each_with_index.map do |value, index| klass_modules_string = index.zero? ? value[1].to_s : "#{klass_modules_string}::#{value[1]}" end else raise ArgumentError, 'Class modules is not a String, Hash or Array.' end klass_modules_string = klass_modules_string.split('::').map(&:titleize).join('::') if [:module_titleize] klass_modules_string = camelize(klass_modules_string) if [:module_camelize] klass_modules_string = klass_modules_string.trim_all klass_modules_string = "#{klass_modules_string}::" unless klass_modules_string.empty? end klass_modules_string end |
#validate_klass(klass, _options) ⇒ String
Validates klass and returns klass as a string after all blanks have been removed using klass.gsub(/s+/, “”).
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 192 def validate_klass(klass, ) unless klass.is_a?(Symbol) || klass.is_a?(String) raise ArgumentError, '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.' end klass = klass.to_s.strip raise ArgumentError, 'Class is empty?' if klass.empty? klass end |
#validate_klass_modules(klass_modules) ⇒ Symbol, Array or String
Validates and returns klass_modules.
221 222 223 224 225 226 227 228 229 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 221 def validate_klass_modules(klass_modules) return {} if klass_modules.nil? || (klass_modules.respond_to?(:empty?) && klass_modules.empty?) if !klass_modules.instance_of?(String) && !klass_modules.instance_of?(Hash) && !klass_modules.instance_of?(Array) raise ArgumentError, 'Class modules is not a String, Hash or Array.' end klass_modules end |