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
- #initialize(*args) ⇒ Object constructor
- #register_type(*args) ⇒ Object
- #registered_types ⇒ Object
-
#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
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 122 |
# File 'ext/msgpack/packer_class.c', line 81
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 = Qnil;
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
130 131 132 133 134 135 136 137 |
# File 'ext/msgpack/packer_class.c', line 130
static VALUE Packer_buffer(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
if (!RTEST(pk->buffer_ref)) {
pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
}
return pk->buffer_ref;
}
|
#compatibility_mode? ⇒ Boolean
124 125 126 127 128 |
# File 'ext/msgpack/packer_class.c', line 124
static VALUE Packer_compatibility_mode_p(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
return pk->compatibility_mode ? Qtrue : Qfalse;
}
|
#empty? ⇒ Boolean
317 318 319 320 321 322 323 324 325 |
# File 'ext/msgpack/packer_class.c', line 317
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
296 297 298 299 300 301 |
# File 'ext/msgpack/packer_class.c', line 296
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
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'ext/msgpack/packer_class.c', line 403
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
355 356 357 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 401 |
# File 'ext/msgpack/packer_class.c', line 355
static VALUE Packer_register_type(int argc, VALUE* argv, VALUE self)
{
if (OBJ_FROZEN(self)) {
rb_raise(rb_eFrozenError, "can't modify frozen MessagePack::Packer");
}
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
9 10 11 12 13 14 15 16 17 |
# File 'lib/msgpack/packer.rb', line 9 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
303 304 305 306 307 308 |
# File 'ext/msgpack/packer_class.c', line 303
static VALUE Packer_reset(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_buffer_clear(PACKER_BUFFER_(pk));
return Qnil;
}
|
#size ⇒ Object
310 311 312 313 314 315 |
# File 'ext/msgpack/packer_class.c', line 310
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
333 334 335 336 337 |
# File 'ext/msgpack/packer_class.c', line 333
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
327 328 329 330 331 |
# File 'ext/msgpack/packer_class.c', line 327
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
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/msgpack/packer.rb', line 19 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
139 140 141 142 143 144 |
# File 'ext/msgpack/packer_class.c', line 139
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
194 195 196 197 198 199 200 |
# File 'ext/msgpack/packer_class.c', line 194
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
252 253 254 255 256 257 |
# File 'ext/msgpack/packer_class.c', line 252
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
182 183 184 185 186 187 188 189 190 191 192 |
# File 'ext/msgpack/packer_class.c', line 182
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
266 267 268 269 270 271 |
# File 'ext/msgpack/packer_class.c', line 266
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
284 285 286 287 288 289 290 291 292 293 294 |
# File 'ext/msgpack/packer_class.c', line 284
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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'ext/msgpack/packer_class.c', line 231
static VALUE Packer_write_extension(VALUE self, VALUE obj)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
Check_Type(obj, T_STRUCT);
VALUE rb_ext_type = RSTRUCT_GET(obj, 0);
if(!RB_TYPE_P(rb_ext_type, T_FIXNUM)) {
rb_raise(rb_eRangeError, "integer %s too big to convert to `signed char'", RSTRING_PTR(rb_String(rb_ext_type)));
}
int ext_type = FIX2INT(rb_ext_type);
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
160 161 162 163 164 165 |
# File 'ext/msgpack/packer_class.c', line 160
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
167 168 169 170 171 172 |
# File 'ext/msgpack/packer_class.c', line 167
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
273 274 275 276 277 278 279 280 281 282 |
# File 'ext/msgpack/packer_class.c', line 273
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
202 203 204 205 206 207 208 |
# File 'ext/msgpack/packer_class.c', line 202
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
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'ext/msgpack/packer_class.c', line 218
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
259 260 261 262 263 264 |
# File 'ext/msgpack/packer_class.c', line 259
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
146 147 148 149 150 151 |
# File 'ext/msgpack/packer_class.c', line 146
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
174 175 176 177 178 179 180 |
# File 'ext/msgpack/packer_class.c', line 174
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
210 211 212 213 214 215 216 |
# File 'ext/msgpack/packer_class.c', line 210
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
339 340 341 342 343 344 |
# File 'ext/msgpack/packer_class.c', line 339
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
153 154 155 156 157 158 |
# File 'ext/msgpack/packer_class.c', line 153
static VALUE Packer_write_true(VALUE self)
{
msgpack_packer_t *pk = MessagePack_Packer_get(self);
msgpack_packer_write_true(pk);
return self;
}
|