Class: PGTrunk::Serializers::ArrayOfStringsSerializer

Inherits:
ActiveRecord::Type::Value
  • Object
show all
Defined in:
lib/pg_trunk/core/serializers/array_of_strings_serializer.rb

Overview

Cast the attribute value as an array of strings. It knows how to cast arrays returned by PostgreSQL as a string like 'USD,EUR,GBP' into ['USD', 'EUR', 'GBP'].

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/pg_trunk/core/serializers/array_of_strings_serializer.rb', line 10

def cast(value)
  case value
  when ::String
    value.gsub(/^\{|\}$/, "").split(",")
  when ::NilClass then []
  when ::Array then value.map(&:to_s)
  else [value.to_s]
  end
end

#serialize(value) ⇒ Object



20
21
22
# File 'lib/pg_trunk/core/serializers/array_of_strings_serializer.rb', line 20

def serialize(value)
  value
end