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

#convert, #source, #target

Instance Method Summary collapse

Methods inherited from Converter

create, #fail_conversion_type, #initialize

Constructor Details

This class inherits a constructor from Necromancer::Converter

Instance Method Details

#call(value, options = {}) ⇒ Object

Convert string value to array

Examples:

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


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/necromancer/converters/array.rb', line 21

def call(value, options = {})
  strict = options.fetch(:strict, config.strict)
  case value.to_s
  when /^\s*?((\d+)(\s*(,|-)\s*)?)+\s*?$/
    value.to_s.split($4).map(&:to_i)
  when /^((\w)(\s*(,|-)\s*)?)+$/
    value.to_s.split($4)
  else
    strict ? fail_conversion_type(value) : Array(value)
  end
end