123456789_123456789_123456789_123456789_123456789_

Class: PG::TypeMapByClass

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, TypeMap
Instance Chain:
Inherits: PG::TypeMap
Defined in: ext/pg_type_map_by_class.c

Overview

This type map casts values based on the class or the ancestors of the given value to be sent.

This type map is usable for type casting query bind parameters and COPY data for Connection#put_copy_data . Therefore only encoders might be assigned by the #[]= method.

Instance Attribute Summary

TypeMap::DefaultTypeMappable - Included

#default_type_map

Returns the default TypeMap that is currently set for values that could not be casted by this type map.

#default_type_map=

Set the default TypeMap that is used for values that could not be casted by this type map.

Instance Method Summary

TypeMap::DefaultTypeMappable - Included

#with_default_type_map

Set the default TypeMap that is used for values that could not be casted by this type map.

Instance Method Details

#[](class) ⇒ coder

Returns the encoder object for the given class

[ GitHub ]

  
# File 'ext/pg_type_map_by_class.c', line 229

static VALUE
pg_tmbk_aref( VALUE self, VALUE klass )
{
	t_tmbk *this = RTYPEDDATA_DATA( self );

	return rb_hash_lookup(this->klass_to_coder, klass);
}

#[]=(class, coder)

Assigns a new Coder object to the type map. The encoder is chosen for all values that are a kind of the given class .

coder can be one of the following:

  • nil - Values are forwarded to the #default_type_map .

  • a Coder - Values are encoded by the given encoder

  • a Symbol - The method of this type map (or a derivation) that is called for each value to sent. It must return a PG::Coder or nil .

  • a Proc - The Proc object is called for each value. It must return a Coder or nil .

[ GitHub ]

  
# File 'ext/pg_type_map_by_class.c', line 203

static VALUE
pg_tmbk_aset( VALUE self, VALUE klass, VALUE coder )
{
	t_tmbk *this = RTYPEDDATA_DATA( self );

	rb_check_frozen(self);

	if(NIL_P(coder)){
		rb_hash_delete( this->klass_to_coder, klass );
	}else{
		rb_hash_aset( this->klass_to_coder, klass, coder );
	}

	/* The cache lookup key can be a derivation of the klass.
	 * So we can not expire the cache selectively. */
	memset( &this->cache_row, 0, sizeof(this->cache_row) );

	return coder;
}

#codersHash

Returns all classes and their assigned encoder object.

[ GitHub ]

  
# File 'ext/pg_type_map_by_class.c', line 243

static VALUE
pg_tmbk_coders( VALUE self )
{
	t_tmbk *this = RTYPEDDATA_DATA( self );

	return rb_obj_freeze(rb_hash_dup(this->klass_to_coder));
}