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
158 159 160 161 162 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 158 def camelize(token) raise ArgumentError, 'Token is not a String' unless token.instance_of? String token.titlecase.camelize.sub(/^:*/, '').trim_all unless token.empty? end |
#ensure_options(options) ⇒ Hash
Ensures options are initialized and valid before accessing them.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 182 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.
142 143 144 145 146 147 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 142 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.
41 42 43 44 45 46 47 48 49 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 41 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.
72 73 74 75 76 77 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 72 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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 98 def to_modules_string(klass_modules = [], = {}) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity 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+/, “”).
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 209 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.
239 240 241 242 243 244 245 246 247 |
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 239 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 |