Module: EnvironmentHelpers::EnumerableHelpers

Included in:
EnvironmentHelpers
Defined in:
lib/environment_helpers/enumerable_helpers.rb

Constant Summary collapse

VALID_TYPES =
%i[strings symbols integers]
TYPE_HANDLERS =
{
  integers: :to_i,
  strings: :to_s,
  symbols: :to_sym
}
TYPE_MAP =
{
  integers: Integer,
  strings: String,
  symbols: Symbol
}

Instance Method Summary collapse

Instance Method Details

#array(key, of: :strings, delimiter: ",", default: nil, required: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/environment_helpers/enumerable_helpers.rb', line 17

def array(key, of: :strings, delimiter: ",", default: nil, required: false)
  check_default_type(:array, default, Array)
  check_valid_data_type!(of)
  check_default_data_types!(default, of)

  values = fetch_value(key, required: required)
  return default if values.nil?

  values.split(delimiter).map { |value| value.send(TYPE_HANDLERS[of]) }
end