Module: Unisec::Utils::Arguments — Documentation by YARD 0.9.44

# 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

#### Examples:

    Unisec::Utils::Arguments.argenc2enc('utf8', target: 'standard') # => "UTF-8"
    Unisec::Utils::Arguments.argenc2enc('utf16be', target: 'class') # => #<Encoding:UTF-16BE (autoload)>

Parameters:

- argenc (`::String`) —

  Encoding name as used as argument in Unisec CLI (authorized values are: utf8 utf16be utf16le utf32be utf32le).

- target (`::String`) (defaults to: `'standard'`) —

  'standard' for standard encoding name, 'class' for Ruby class naming

Returns:

- (`::String|Class`)

Raises:

- (`ArgumentError`)

    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).

#### Examples:

    Unisec::Utils::Arguments.to_array_of_sym("arg") # => [:arg]
    Unisec::Utils::Arguments.to_array_of_sym("a,b,c") # => [:a, :b, :c]
    Unisec::Utils::Arguments.to_array_of_sym(:snake) # => [:snake]
    Unisec::Utils::Arguments.to_array_of_sym([:a, :b, :c]) # => [:a, :b, :c]

Parameters:

- input (`::String|Symbol`) —

  (anything else will be returned untransformed)

Returns:

- (`Array<Symbol>`) —

  (or anything else if input type is not respected)

    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