Sha256: 4a18b6f8e81c88f932675a07618e7cc6b912e0811f1a256b30d99f38346b7602

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

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

          attr_reader :name, :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)

            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)
          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

          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

2 entries across 2 versions & 1 rubygems

Version Path
torque-postgresql-1.0.1 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-1.0.0 lib/torque/postgresql/adapter/oid/enum.rb