Sha256: 1f3de741424b7d25b33d15ce933b31c3693e4a8c555caae38f834c28706e1b47

Contents?: true

Size: 1.23 KB

Versions: 19

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Torque
  module PostgreSQL
    module Adapter
      module OID
        class EnumSet < Enum
          def initialize(name, enum_klass)
            @name  = name + '[]'
            @klass = Attributes::EnumSet.lookup(name, enum_klass)

            @set_klass = self
            @enum_klass = enum_klass
          end

          def type
            :enum_set
          end

          def deserialize(value)
            return unless value.present?
            value = value[1..-2].split(',') if value.is_a?(String)
            cast_value(value)
          end

          def serialize(value)
            return if value.blank?
            value = cast_value(value)

            return if value.blank?
            "{#{value.map(&:to_s).join(',')}}"
          end

          # Always use symbol values for schema dumper
          def type_cast_for_schema(value)
            cast_value(value).map(&:to_sym).inspect
          end

          private

            def cast_value(value)
              return if value.blank?
              return value if value.is_a?(@klass)
              @klass.new(value)
            rescue Attributes::EnumSet::EnumSetError
              nil
            end

        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
torque-postgresql-2.4.5 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.4.4 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.4.3 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.4.2 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.4.1 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.4.0 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.3.0 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.2.4 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.2.3 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.2.2 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.2.1 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.2.0 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.1.3 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.1.2 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.1.1 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.1.0 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.0.6 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.0.5 lib/torque/postgresql/adapter/oid/enum_set.rb
torque-postgresql-2.0.4 lib/torque/postgresql/adapter/oid/enum_set.rb