Sha256: b0f1603f8ae78e08fdc9e59c9e04cb3bda24b6343555b2a46c7365b6b5cc8b81

Contents?: true

Size: 686 Bytes

Versions: 5

Compression:

Stored size: 686 Bytes

Contents

# frozen_string_literal: true

# @private
module PGTrunk::Serializers
  # @private
  # 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'].
  class ArrayOfStringsSerializer < ActiveRecord::Type::Value
    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

    def serialize(value)
      value
    end
  end

  ActiveModel::Type.register(
    :pg_trunk_array_of_strings,
    ArrayOfStringsSerializer,
  )
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pg_trunk-0.2.0 lib/pg_trunk/core/serializers/array_of_strings_serializer.rb
pg_trunk-0.1.3 lib/pg_trunk/core/serializers/array_of_strings_serializer.rb
pg_trunk-0.1.2 lib/pg_trunk/core/serializers/array_of_strings_serializer.rb
pg_trunk-0.1.1 lib/pg_trunk/core/serializers/array_of_strings_serializer.rb
pg_trunk-0.1.0 lib/pg_trunk/core/serializers/array_of_strings_serializer.rb