# class PG::Coder

This is the base class for all type cast encoder and decoder classes.

It can be used for implicit type casts by a `PG::TypeMap` or to convert single values to/from their string representation by encode and decode.

Ruby `nil` values are not handled by encoders, but are always transmitted as SQL `NULL` value. Vice versa SQL `NULL` values are not handled by decoders, but are always returned as a `nil` value.

### Constants

FORMAT_ERROR_MASK
FORMAT_ERROR_TO_PARTIAL
FORMAT_ERROR_TO_RAISE
FORMAT_ERROR_TO_STRING
TIMESTAMP_APP_LOCAL
TIMESTAMP_APP_UTC
TIMESTAMP_DB_LOCAL
TIMESTAMP_DB_UTC

  `Coder#flags=`

### Attributes

name[RW]

Name of the coder or the corresponding data type.

This accessor is only used in `PG::Coder#inspect` .

### Public Class Methods

new(hash=nil, **kwargs) click to toggle source

Create a new coder object based on the attribute Hash.

    # File lib/pg/coder.rb, line 16
    def initialize(hash=nil, **kwargs)
            warn("PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}", category: :deprecated) if hash

            (hash || kwargs).each do |key, val|
                    send("#{key}=", val)
            end
    end

### Public Instance Methods

==(v) click to toggle source

    # File lib/pg/coder.rb, line 38
    def ==(v)
            self.class == v.class && to_h == v.to_h
    end

dup() click to toggle source

    # File lib/pg/coder.rb, line 24
    def dup
            self.class.new(**to_h)
    end

flags → Integer click to toggle source

Get current bitwise OR-ed coder flags.

    static VALUE
    pg_coder_flags_get(VALUE self)
    {
            t_pg_coder *this = RTYPEDDATA_DATA(self);
            return INT2NUM(this->flags);
    }

flags = Integer click to toggle source

Set coder specific bitwise OR-ed flags. See the particular en- or decoder description for available flags.

The default is `0`.

    static VALUE
    pg_coder_flags_set(VALUE self, VALUE flags)
    {
            t_pg_coder *this = RTYPEDDATA_DATA(self);
            rb_check_frozen(self);
            this->flags = NUM2INT(flags);
            return flags;
    }

format → Integer click to toggle source

The format code that is sent alongside with an encoded query parameter value.

    static VALUE
    pg_coder_format_get(VALUE self)
    {
            t_pg_coder *this = RTYPEDDATA_DATA(self);
            return INT2NUM(this->format);
    }

format = Integer click to toggle source

Specifies the format code that is sent alongside with an encoded query parameter value.

The default is `0`.

    static VALUE
    pg_coder_format_set(VALUE self, VALUE format)
    {
            t_pg_coder *this = RTYPEDDATA_DATA(self);
            rb_check_frozen(self);
            this->format = NUM2INT(format);
            return format;
    }

inspect() click to toggle source

    # File lib/pg/coder.rb, line 50
    def inspect
            str = self.to_s
            oid_str = " oid=#{oid}" unless oid==0
            format_str = " format=#{format}" unless format==0
            name_str = " #{name.inspect}" if name
            str[-1,0] = "#{name_str} #{oid_str}#{format_str}"
            str
    end

inspect_short() click to toggle source

    # File lib/pg/coder.rb, line 59
    def inspect_short
            str = case format
                    when 0 then "T"
                    when 1 then "B"
                    else format.to_s
            end
            str += "E" if respond_to?(:encode)
            str += "D" if respond_to?(:decode)

            "#{name || self.class.name}:#{str}"
    end

marshal_dump() click to toggle source

    # File lib/pg/coder.rb, line 42
    def marshal_dump
            Marshal.dump(to_h)
    end

marshal_load(str) click to toggle source

    # File lib/pg/coder.rb, line 46
    def marshal_load(str)
            initialize(**Marshal.load(str))
    end

oid → Integer click to toggle source

The type OID that is sent alongside with an encoded query parameter value.

    static VALUE
    pg_coder_oid_get(VALUE self)
    {
            t_pg_coder *this = RTYPEDDATA_DATA(self);
            return UINT2NUM(this->oid);
    }

oid = Integer click to toggle source

Specifies the type OID that is sent alongside with an encoded query parameter value.

The default is `0`.

    static VALUE
    pg_coder_oid_set(VALUE self, VALUE oid)
    {
            t_pg_coder *this = RTYPEDDATA_DATA(self);
            rb_check_frozen(self);
            this->oid = NUM2UINT(oid);
            return oid;
    }

to_h() click to toggle source

Returns coder attributes as Hash.

    # File lib/pg/coder.rb, line 29
    def to_h
            {
                    oid: oid,
                    format: format,
                    flags: flags,
                    name: name,
            }
    end