Sha256: 46d2ae8df6f6ab3c837aca2aa4db623aa17b8439318cec30db3d176617dc27fe

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 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)
            new(row['typname'])
          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

6 entries across 6 versions & 1 rubygems

Version Path
torque-postgresql-0.2.13 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-0.2.12 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-0.2.11 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-0.2.10 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-0.2.9 lib/torque/postgresql/adapter/oid/enum.rb
torque-postgresql-0.2.8 lib/torque/postgresql/adapter/oid/enum.rb