Class: ActiveAttrExtended::Typecasting::ArrayTypecaster

Inherits:
Object
  • Object
show all
Defined in:
lib/active_attr_extended/typecasting/array_typecaster.rb

Overview

Typecasts an Object to an Array

Examples:

Usage

ArrayTypecaster.new.call(1) # => [1]

Since:

  • 0.6.0

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Array?

Typecasts an Object to an Array

Will typecast any Object except nil to an Array, first by attempting to call ‘#to_a`, then by wrapping `value` in an Array (`[value]`).

Examples:

Usage

ArrayTypecaster.new.call(1) # => [1]

Parameters:

  • value (Object)

    The object to typecast

Returns:

  • (Array, nil)

    The result of typecasting

Since:

  • 0.6.0



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_attr_extended/typecasting/array_typecaster.rb', line 25

def call(value)
  #treat incoming strings as serialized JSON
  value = JSON::parse(value) if value.is_a? String

  if value.nil?
    nil
  elsif value.respond_to? :to_a
    value.to_a
  else
    [value]
  end
end