Sha256: 70ff642c13048f46237dab7ddd03d00a62111804db8adcc754faed1d9a2d1204

Contents?: true

Size: 1.74 KB

Versions: 30

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Torque
  module PostgreSQL
    module Adapter
      module OID
        class Enum < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Enum

          attr_reader :name, :klass, :set_klass, :enum_klass

          def self.create(row, type_map)
            name    = row['typname']
            oid     = row['oid'].to_i
            arr_oid = row['typarray'].to_i

            oid_klass     = Enum.new(name)
            oid_set_klass = EnumSet.new(name, oid_klass.klass)
            oid_klass.instance_variable_set(:@set_klass, oid_set_klass)

            type_map.register_type(oid,     oid_klass)
            type_map.register_type(arr_oid, oid_set_klass)
          end

          def initialize(name)
            @name  = name
            @klass = Attributes::Enum.lookup(name)

            @enum_klass = self
          end

          def hash
            [self.class, name].hash
          end

          def serialize(value)
            return if value.blank?
            value = cast_value(value)
            value.to_s unless value.nil?
          end

          def assert_valid_value(value)
            cast_value(value)
          end

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

          def ==(other)
            self.class == other.class &&
              other.klass == klass &&
              other.type == type
          end

          private

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

        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
torque-postgresql-3.4.1 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.4.5 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.4.0 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.3.3 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.4.4 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.3.2 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.4.3 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.3.1 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.3.0 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.2.2 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.4.2 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.4.1 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.2.1 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.4.0 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.2.0 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.1.0 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.3.0 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.0.1 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-2.2.4 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-3.0.0 lib/torque/postgresql/adapter/oid/enum.rb