Class: Necromancer::ArrayConverters::StringToArrayConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/necromancer/converters/array.rb

Overview

An object that converts a String to an Array

Instance Attribute Summary

Attributes inherited from Converter

#config, #convert, #source, #target

Instance Method Summary collapse

Methods inherited from Converter

create, #initialize, #raise_conversion_type

Constructor Details

This class inherits a constructor from Necromancer::Converter

Instance Method Details

#call(value, strict: config.strict) ⇒ Object

Convert string value to array

Examples:

converter.call("a, b, c")  # => ["a", "b", "c"]
converter.call("1 - 2 - 3")  # => ["1", "2", "3"]


25
26
27
28
29
30
31
32
33
# File 'lib/necromancer/converters/array.rb', line 25

def call(value, strict: config.strict)
  return [] if value.to_s.empty?

  if match = value.to_s.match(ARRAY_MATCHER)
    value.to_s.split(match[:sep])
  else
    strict ? raise_conversion_type(value) : Array(value)
  end
end