Module: Unisec::Utils::Arguments
- Defined in:
- lib/unisec/utils.rb
Class Method Summary collapse
-
.argenc2enc(argenc, target: 'standard') ⇒ ::String|Class
Converts encoding name from CLI to encoding name in standard format or Ruby Class.
-
.to_array_of_sym(input) ⇒ Array<Symbol>
Converts an argument that is a string, a string of arguments separated by comma, a symbol to an array of symbol.
Class Method Details
.argenc2enc(argenc, target: 'standard') ⇒ ::String|Class
Converts encoding name from CLI to encoding name in standard format or Ruby Class
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/unisec/utils.rb', line 275 def self.argenc2enc(argenc, target: 'standard') argument_encodings = %w[utf8 utf16be utf16le utf32be utf32le] raise ArgumentError unless argument_encodings.include?(argenc) if target == 'standard' argenc.upcase.insert(3, '-') elsif target == 'class' Encoding.const_get(argenc.upcase.insert(3, '_')) # const_get safe thanks to input whitelist else raise ArgumentError end end |
.to_array_of_sym(input) ⇒ Array<Symbol>
Converts an argument that is a string, a string of arguments separated by comma, a symbol to an array of symbol. Useful for methods that are expected to work on array of symbols but can receive various format of imputs (e.g. from CLI).
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/unisec/utils.rb', line 257 def self.to_array_of_sym(input) case input when ::String # a,b,c => [:a, :b, :c] input.split(',').map(&:to_sym) when ::Symbol # :a => [:a] [input] else input end end |