Class: MessagePack::Packer
- Inherits:
-
Object
- Object
- MessagePack::Packer
- Defined in:
- lib/msgpack/packer.rb,
ext/msgpack/packer_class.c
Instance Method Summary collapse
- #buffer ⇒ Object
- #compatibility_mode? ⇒ Boolean
- #empty? ⇒ Boolean
- #flush ⇒ Object
-
#full_pack ⇒ Object
Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);.
- #initialize(*args) ⇒ Object constructor
- #register_type(*args) ⇒ Object
-
#registered_types ⇒ Object
see ext for other methods.
-
#reset ⇒ Object
(also: #clear)
delegation methods.
- #size ⇒ Object
- #to_a ⇒ Object
- #to_str ⇒ Object (also: #to_s)
- #type_registered?(klass_or_type) ⇒ Boolean
- #write(v) ⇒ Object (also: #pack)
- #write_array(obj) ⇒ Object
- #write_array_header(n) ⇒ Object
- #write_bin(obj) ⇒ Object
- #write_bin_header(n) ⇒ Object
- #write_ext(type, data) ⇒ Object
- #write_extension(obj) ⇒ Object
- #write_false ⇒ Object
- #write_float(obj) ⇒ Object
- #write_float32(numeric) ⇒ Object
- #write_hash(obj) ⇒ Object
- #write_int(obj) ⇒ Object
- #write_map_header(n) ⇒ Object
- #write_nil ⇒ Object
- #write_string(obj) ⇒ Object
- #write_symbol(obj) ⇒ Object
- #write_to(io) ⇒ Object
- #write_true ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'ext/msgpack/packer_class.c', line 80
VALUE MessagePack_Packer_initialize(int argc, VALUE* argv, VALUE self)
{
if(argc > 2) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
}
VALUE io = Qnil;
VALUE options = Qnil;
if(argc >= 1) {
io = argv[0];
}
if(argc == 2) {
options = argv[1];
}
if (options == Qnil && rb_type(io) == T_HASH) {
options = io;
io = Qnil;
}
if(options != Qnil) {
Check_Type(options, T_HASH);
}
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_ext_registry_init(&pk->ext_registry);
pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
MessagePack_Buffer_set_options(PACKER_BUFFER_(pk), io, options);
if(options != Qnil) {
VALUE v;
v = rb_hash_aref(options, sym_compatibility_mode);
msgpack_packer_set_compat(pk, RTEST(v));
}
return self;
}
|
Instance Method Details
#buffer ⇒ Object
129 130 131 132 133 |
# File 'ext/msgpack/packer_class.c', line 129
static VALUE Packer_buffer(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
return pk->buffer_ref;
}
|
#compatibility_mode? ⇒ Boolean
123 124 125 126 127 |
# File 'ext/msgpack/packer_class.c', line 123
static VALUE Packer_compatibility_mode_p(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
return pk->compatibility_mode ? Qtrue : Qfalse;
}
|
#empty? ⇒ Boolean
308 309 310 311 312 313 314 315 316 |
# File 'ext/msgpack/packer_class.c', line 308
static VALUE Packer_empty_p(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
if(msgpack_buffer_top_readable_size(PACKER_BUFFER_(pk)) == 0) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#flush ⇒ Object
287 288 289 290 291 292 |
# File 'ext/msgpack/packer_class.c', line 287
static VALUE Packer_flush(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_buffer_flush(PACKER_BUFFER_(pk));
return self;
}
|
#full_pack ⇒ Object
Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'ext/msgpack/packer_class.c', line 402
VALUE Packer_full_pack(VALUE self)
{
VALUE retval;
msgpack_packer_t *pk = MessagePack_Packer_get(self);
if(msgpack_buffer_has_io(PACKER_BUFFER_(pk))) {
msgpack_buffer_flush(PACKER_BUFFER_(pk));
retval = Qnil;
} else {
retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
}
msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */
return retval;
}
|
#register_type(*args) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'ext/msgpack/packer_class.c', line 358
static VALUE Packer_register_type(int argc, VALUE* argv, VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
int ext_type;
VALUE ext_module;
VALUE proc;
VALUE arg;
switch (argc) {
case 2:
/* register_type(0x7f, Time) {|obj| block... } */
rb_need_block();
proc = rb_block_lambda();
arg = proc;
break;
case 3:
/* register_type(0x7f, Time, :to_msgpack_ext) */
arg = argv[2];
proc = rb_funcall(arg, rb_intern("to_proc"), 0);
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
}
ext_type = NUM2INT(argv[0]);
if(ext_type < -128 || ext_type > 127) {
rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
}
ext_module = argv[1];
if(rb_type(ext_module) != T_MODULE && rb_type(ext_module) != T_CLASS) {
rb_raise(rb_eArgError, "expected Module/Class but found %s.", rb_obj_classname(ext_module));
}
msgpack_packer_ext_registry_put(&pk->ext_registry, ext_module, ext_type, 0, proc, arg);
if (ext_module == rb_cSymbol) {
pk->has_symbol_ext_type = true;
}
return Qnil;
}
|
#registered_types ⇒ Object
see ext for other methods
5 6 7 8 9 10 11 12 13 |
# File 'lib/msgpack/packer.rb', line 5 def registered_types list = [] registered_types_internal.each_pair do |klass, ary| list << {type: ary[0], class: klass, packer: ary[2]} end list.sort{|a, b| a[:type] <=> b[:type] } end |
#reset ⇒ Object Also known as: clear
delegation methods
294 295 296 297 298 299 |
# File 'ext/msgpack/packer_class.c', line 294
static VALUE Packer_reset(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_buffer_clear(PACKER_BUFFER_(pk));
return Qnil;
}
|
#size ⇒ Object
301 302 303 304 305 306 |
# File 'ext/msgpack/packer_class.c', line 301
static VALUE Packer_size(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
return SIZET2NUM(size);
}
|
#to_a ⇒ Object
324 325 326 327 328 |
# File 'ext/msgpack/packer_class.c', line 324
static VALUE Packer_to_a(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
}
|
#to_str ⇒ Object Also known as: to_s
318 319 320 321 322 |
# File 'ext/msgpack/packer_class.c', line 318
static VALUE Packer_to_str(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
return msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
}
|
#type_registered?(klass_or_type) ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/msgpack/packer.rb', line 15 def type_registered?(klass_or_type) case klass_or_type when Class klass = klass_or_type registered_types.any?{|entry| klass <= entry[:class] } when Integer type = klass_or_type registered_types.any?{|entry| type == entry[:type] } else raise ArgumentError, "class or type id" end end |
#write(v) ⇒ Object Also known as: pack
135 136 137 138 139 140 |
# File 'ext/msgpack/packer_class.c', line 135
static VALUE Packer_write(VALUE self, VALUE v)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_value(pk, v);
return self;
}
|
#write_array(obj) ⇒ Object
190 191 192 193 194 195 196 |
# File 'ext/msgpack/packer_class.c', line 190
static VALUE Packer_write_array(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_ARRAY);
msgpack_packer_write_array_value(pk, obj);
return self;
}
|
#write_array_header(n) ⇒ Object
243 244 245 246 247 248 |
# File 'ext/msgpack/packer_class.c', line 243
static VALUE Packer_write_array_header(VALUE self, VALUE n)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_array_header(pk, NUM2UINT(n));
return self;
}
|
#write_bin(obj) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'ext/msgpack/packer_class.c', line 178
static VALUE Packer_write_bin(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_STRING);
VALUE enc = rb_enc_from_encoding(rb_ascii8bit_encoding());
obj = rb_str_encode(obj, enc, 0, Qnil);
msgpack_packer_write_string_value(pk, obj);
return self;
}
|
#write_bin_header(n) ⇒ Object
257 258 259 260 261 262 |
# File 'ext/msgpack/packer_class.c', line 257
static VALUE Packer_write_bin_header(VALUE self, VALUE n)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_bin_header(pk, NUM2UINT(n));
return self;
}
|
#write_ext(type, data) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 |
# File 'ext/msgpack/packer_class.c', line 275
static VALUE Packer_write_ext(VALUE self, VALUE type, VALUE data)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
int ext_type = NUM2INT(type);
if(ext_type < -128 || ext_type > 127) {
rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
}
StringValue(data);
msgpack_packer_write_ext(pk, ext_type, data);
return self;
}
|
#write_extension(obj) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'ext/msgpack/packer_class.c', line 227
static VALUE Packer_write_extension(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_STRUCT);
int ext_type = FIX2INT(RSTRUCT_GET(obj, 0));
if(ext_type < -128 || ext_type > 127) {
rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
}
VALUE payload = RSTRUCT_GET(obj, 1);
StringValue(payload);
msgpack_packer_write_ext(pk, ext_type, payload);
return self;
}
|
#write_false ⇒ Object
156 157 158 159 160 161 |
# File 'ext/msgpack/packer_class.c', line 156
static VALUE Packer_write_false(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_false(pk);
return self;
}
|
#write_float(obj) ⇒ Object
163 164 165 166 167 168 |
# File 'ext/msgpack/packer_class.c', line 163
static VALUE Packer_write_float(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_float_value(pk, obj);
return self;
}
|
#write_float32(numeric) ⇒ Object
264 265 266 267 268 269 270 271 272 273 |
# File 'ext/msgpack/packer_class.c', line 264
static VALUE Packer_write_float32(VALUE self, VALUE numeric)
{
if(!rb_obj_is_kind_of(numeric, rb_cNumeric)) {
rb_raise(rb_eArgError, "Expected numeric");
}
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_float(pk, (float)rb_num2dbl(numeric));
return self;
}
|
#write_hash(obj) ⇒ Object
198 199 200 201 202 203 204 |
# File 'ext/msgpack/packer_class.c', line 198
static VALUE Packer_write_hash(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_HASH);
msgpack_packer_write_hash_value(pk, obj);
return self;
}
|
#write_int(obj) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'ext/msgpack/packer_class.c', line 214
static VALUE Packer_write_int(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
if (FIXNUM_P(obj)) {
msgpack_packer_write_fixnum_value(pk, obj);
} else {
Check_Type(obj, T_BIGNUM);
msgpack_packer_write_bignum_value(pk, obj);
}
return self;
}
|
#write_map_header(n) ⇒ Object
250 251 252 253 254 255 |
# File 'ext/msgpack/packer_class.c', line 250
static VALUE Packer_write_map_header(VALUE self, VALUE n)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_map_header(pk, NUM2UINT(n));
return self;
}
|
#write_nil ⇒ Object
142 143 144 145 146 147 |
# File 'ext/msgpack/packer_class.c', line 142
static VALUE Packer_write_nil(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_nil(pk);
return self;
}
|
#write_string(obj) ⇒ Object
170 171 172 173 174 175 176 |
# File 'ext/msgpack/packer_class.c', line 170
static VALUE Packer_write_string(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_STRING);
msgpack_packer_write_string_value(pk, obj);
return self;
}
|
#write_symbol(obj) ⇒ Object
206 207 208 209 210 211 212 |
# File 'ext/msgpack/packer_class.c', line 206
static VALUE Packer_write_symbol(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_SYMBOL);
msgpack_packer_write_symbol_value(pk, obj);
return self;
}
|
#write_to(io) ⇒ Object
330 331 332 333 334 335 |
# File 'ext/msgpack/packer_class.c', line 330
static VALUE Packer_write_to(VALUE self, VALUE io)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
size_t sz = msgpack_buffer_flush_to_io(PACKER_BUFFER_(pk), io, s_write, true);
return SIZET2NUM(sz);
}
|
#write_true ⇒ Object
149 150 151 152 153 154 |
# File 'ext/msgpack/packer_class.c', line 149
static VALUE Packer_write_true(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_true(pk);
return self;
}
|