Module: PG::BasicTypeRegistry
- Defined in:
- lib/pg/basic_type_mapping.rb
Overview
This module defines the mapping between OID and encoder/decoder classes for PG::BasicTypeMapForResults, PG::BasicTypeMapForQueries and PG::BasicTypeMapBasedOnResult.
Additional types can be added like so:
require 'pg'
require 'ipaddr'
class InetDecoder < PG::SimpleDecoder
def decode(string, tuple=nil, field=nil)
IPAddr.new(string)
end
end
class InetEncoder < PG::SimpleEncoder
def encode(ip_addr)
ip_addr.to_s
end
end
# 0 if for text format, can also be 1 for binary
PG::BasicTypeRegistry.register_type(0, 'inet', InetEncoder, InetDecoder)
Defined Under Namespace
Classes: CoderMap
Class Method Summary collapse
-
.alias_type(format, new, old) ⇒ Object
Alias the
old
type to thenew
type. -
.register_coder(coder) ⇒ Object
Register an encoder or decoder instance for casting a PostgreSQL type.
-
.register_type(format, name, encoder_class, decoder_class) ⇒ Object
Register the given
encoder_class
and/ordecoder_class
for casting a PostgreSQL type.
Class Method Details
.alias_type(format, new, old) ⇒ Object
Alias the old
type to the new
type.
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pg/basic_type_mapping.rb', line 181 def self.alias_type(format, new, old) [:encoder, :decoder].each do |ende| enc = CODERS_BY_NAME[format][ende][old] if enc CODERS_BY_NAME[format][ende][new] = enc else CODERS_BY_NAME[format][ende].delete(new) end end end |
.register_coder(coder) ⇒ Object
Register an encoder or decoder instance for casting a PostgreSQL type.
Coder#name must correspond to the typname
column in the pg_type
table. Coder#format can be 0 for text format and 1 for binary.
164 165 166 167 168 169 |
# File 'lib/pg/basic_type_mapping.rb', line 164 def self.register_coder(coder) h = CODERS_BY_NAME[coder.format] ||= { encoder: {}, decoder: {} } name = coder.name || raise(ArgumentError, "name of #{coder.inspect} must be defined") h[:encoder][name] = coder if coder.respond_to?(:encode) h[:decoder][name] = coder if coder.respond_to?(:decode) end |
.register_type(format, name, encoder_class, decoder_class) ⇒ Object
Register the given encoder_class
and/or decoder_class
for casting a PostgreSQL type.
name
must correspond to the typname
column in the pg_type
table. format
can be 0 for text format and 1 for binary.
175 176 177 178 |
# File 'lib/pg/basic_type_mapping.rb', line 175 def self.register_type(format, name, encoder_class, decoder_class) register_coder(encoder_class.new(name: name, format: format)) if encoder_class register_coder(decoder_class.new(name: name, format: format)) if decoder_class end |