Class: ShallowAttributes::Type::Array Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/shallow_attributes/type/array.rb

Overview

This class is abstract.

Abstract class for typecast object to Array type.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#coerce(values, options = {}) ⇒ Array

Convert value to Array type

Examples:

Convert integer array to string array

ShallowAttributes::Type::Array.new.coerce([1, 2], String)
  # => ['1', '2']

Parameters:

  • values (Array)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :of (String)

    the type of array element class

Returns:

Raises:

Since:

  • 0.1.0


26
27
28
29
30
31
32
# File 'lib/shallow_attributes/type/array.rb', line 26

def coerce(values, options = {})
  unless values.is_a? ::Array
    raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{values}" for type "Array")
  end

  values.map! { |value| ShallowAttributes::Type.coerce(options[:of], value) }
end