Class: PG::TypeMapByMriType

Inherits:
TypeMap
  • Object
show all
Defined in:
ext/pg_type_map_by_mri_type.c,
ext/pg_type_map_by_mri_type.c

Overview

This type map casts values based on the Ruby object type code of the given value to be sent.

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

Direct Known Subclasses

BasicTypeMapForQueries

Instance Method Summary collapse

Methods inherited from TypeMap

#fit_to_query, #fit_to_result

Instance Method Details

#[]Object

Returns the encoder object for the given mri_type

See #[]= for allowed mri_type .



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/pg_type_map_by_mri_type.c', line 208

static VALUE
pg_tmbmt_aref( VALUE self, VALUE mri_type )
{
	VALUE coder;
	t_tmbmt *this = DATA_PTR( self );
	char *p_mri_type;

	p_mri_type = StringValueCStr(mri_type);

	if(0){}
	FOR_EACH_MRI_TYPE( COMPARE_AND_GET )
	else{
		VALUE mri_type_inspect = rb_inspect( mri_type );
		rb_raise(rb_eArgError, "unknown mri_type %s", StringValueCStr(mri_type_inspect));
	}

	return coder;
}

#[]=Object

Assigns a new PG::Coder object to the type map. The decoder is registered for type casts of the given mri_type .

coder can be one of the following:

  • nil - Values are encoded by #to_str

  • a PG::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.

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

mri_type must be one of the following strings:

  • T_FIXNUM

  • T_TRUE

  • T_FALSE

  • T_FLOAT

  • T_BIGNUM

  • T_COMPLEX

  • T_RATIONAL

  • T_ARRAY

  • T_STRING

  • T_SYMBOL

  • T_OBJECT

  • T_CLASS

  • T_MODULE

  • T_REGEXP

  • T_HASH

  • T_STRUCT

  • T_FILE

  • T_DATA



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/pg_type_map_by_mri_type.c', line 177

static VALUE
pg_tmbmt_aset( VALUE self, VALUE mri_type, VALUE coder )
{
	t_tmbmt *this = DATA_PTR( self );
	char *p_mri_type;

	p_mri_type = StringValueCStr(mri_type);

	if(0){}
	FOR_EACH_MRI_TYPE( COMPARE_AND_ASSIGN )
	else{
		VALUE mri_type_inspect = rb_inspect( mri_type );
		rb_raise(rb_eArgError, "unknown mri_type %s", StringValueCStr(mri_type_inspect));
	}

	return self;
}

#codersHash

Returns all mri types and their assigned encoder object.

Returns:

  • (Hash)


237
238
239
240
241
242
243
244
245
246
# File 'ext/pg_type_map_by_mri_type.c', line 237

static VALUE
pg_tmbmt_coders( VALUE self )
{
	t_tmbmt *this = DATA_PTR( self );
	VALUE hash_coders = rb_hash_new();

	FOR_EACH_MRI_TYPE( ADD_TO_HASH );

	return rb_obj_freeze(hash_coders);
}