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
-
#clear ⇒ Object
delegation methods.
- #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.
- #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
69 70 71 72 73 74 75 76 77 78 79 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 |
# File 'ext/msgpack/packer_class.c', line 69
VALUE MessagePack_Packer_initialize(int argc, VALUE* argv, VALUE self)
{
VALUE io = Qnil;
VALUE options = Qnil;
if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
/* Qnil */
} else if(argc == 1) {
VALUE v = argv[0];
if(rb_type(v) == T_HASH) {
options = v;
} else {
io = v;
}
} else if(argc == 2) {
io = argv[0];
options = argv[1];
if(rb_type(options) != T_HASH) {
rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
}
} else {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
}
PACKER(self, pk);
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, ID2SYM(rb_intern("compatibility_mode")));
msgpack_packer_set_compat(pk, RTEST(v));
}
return self;
}
|
Instance Method Details
#buffer ⇒ Object
119 120 121 122 123 |
# File 'ext/msgpack/packer_class.c', line 119
static VALUE Packer_buffer(VALUE self)
{
PACKER(self, pk);
return pk->buffer_ref;
}
|
#clear ⇒ Object
delegation methods
284 285 286 287 288 289 |
# File 'ext/msgpack/packer_class.c', line 284
static VALUE Packer_clear(VALUE self)
{
PACKER(self, pk);
msgpack_buffer_clear(PACKER_BUFFER_(pk));
return Qnil;
}
|
#compatibility_mode? ⇒ Boolean
113 114 115 116 117 |
# File 'ext/msgpack/packer_class.c', line 113
static VALUE Packer_compatibility_mode_p(VALUE self)
{
PACKER(self, pk);
return pk->compatibility_mode ? Qtrue : Qfalse;
}
|
#empty? ⇒ Boolean
298 299 300 301 302 303 304 305 306 |
# File 'ext/msgpack/packer_class.c', line 298
static VALUE Packer_empty_p(VALUE self)
{
PACKER(self, pk);
if(msgpack_buffer_top_readable_size(PACKER_BUFFER_(pk)) == 0) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#flush ⇒ Object
277 278 279 280 281 282 |
# File 'ext/msgpack/packer_class.c', line 277
static VALUE Packer_flush(VALUE self)
{
PACKER(self, pk);
msgpack_buffer_flush(PACKER_BUFFER_(pk));
return self;
}
|
#full_pack ⇒ Object
Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'ext/msgpack/packer_class.c', line 398
VALUE Packer_full_pack(VALUE self)
{
VALUE retval;
PACKER(self, pk);
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
349 350 351 352 353 354 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 |
# File 'ext/msgpack/packer_class.c', line 349
static VALUE Packer_register_type(int argc, VALUE* argv, VALUE self)
{
PACKER(self, pk);
int ext_type;
VALUE ext_module;
VALUE proc;
VALUE arg;
switch (argc) {
case 2:
/* register_type(0x7f, Time) {|obj| block... } */
rb_need_block();
#ifdef HAVE_RB_BLOCK_LAMBDA
proc = rb_block_lambda();
#else
/* MRI 1.8 */
proc = rb_block_proc();
#endif
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, 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 |
#size ⇒ Object
291 292 293 294 295 296 |
# File 'ext/msgpack/packer_class.c', line 291
static VALUE Packer_size(VALUE self)
{
PACKER(self, pk);
size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
return SIZET2NUM(size);
}
|
#to_a ⇒ Object
314 315 316 317 318 |
# File 'ext/msgpack/packer_class.c', line 314
static VALUE Packer_to_a(VALUE self)
{
PACKER(self, pk);
return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
}
|
#to_str ⇒ Object Also known as: to_s
308 309 310 311 312 |
# File 'ext/msgpack/packer_class.c', line 308
static VALUE Packer_to_str(VALUE self)
{
PACKER(self, pk);
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
125 126 127 128 129 130 |
# File 'ext/msgpack/packer_class.c', line 125
static VALUE Packer_write(VALUE self, VALUE v)
{
PACKER(self, pk);
msgpack_packer_write_value(pk, v);
return self;
}
|
#write_array(obj) ⇒ Object
180 181 182 183 184 185 186 |
# File 'ext/msgpack/packer_class.c', line 180
static VALUE Packer_write_array(VALUE self, VALUE obj)
{
PACKER(self, pk);
Check_Type(obj, T_ARRAY);
msgpack_packer_write_array_value(pk, obj);
return self;
}
|
#write_array_header(n) ⇒ Object
233 234 235 236 237 238 |
# File 'ext/msgpack/packer_class.c', line 233
static VALUE Packer_write_array_header(VALUE self, VALUE n)
{
PACKER(self, pk);
msgpack_packer_write_array_header(pk, NUM2UINT(n));
return self;
}
|
#write_bin(obj) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'ext/msgpack/packer_class.c', line 168
static VALUE Packer_write_bin(VALUE self, VALUE obj)
{
PACKER(self, pk);
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
247 248 249 250 251 252 |
# File 'ext/msgpack/packer_class.c', line 247
static VALUE Packer_write_bin_header(VALUE self, VALUE n)
{
PACKER(self, pk);
msgpack_packer_write_bin_header(pk, NUM2UINT(n));
return self;
}
|
#write_ext(type, data) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 |
# File 'ext/msgpack/packer_class.c', line 265
static VALUE Packer_write_ext(VALUE self, VALUE type, VALUE data)
{
PACKER(self, pk);
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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'ext/msgpack/packer_class.c', line 217
static VALUE Packer_write_extension(VALUE self, VALUE obj)
{
PACKER(self, pk);
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
146 147 148 149 150 151 |
# File 'ext/msgpack/packer_class.c', line 146
static VALUE Packer_write_false(VALUE self)
{
PACKER(self, pk);
msgpack_packer_write_false(pk);
return self;
}
|
#write_float(obj) ⇒ Object
153 154 155 156 157 158 |
# File 'ext/msgpack/packer_class.c', line 153
static VALUE Packer_write_float(VALUE self, VALUE obj)
{
PACKER(self, pk);
msgpack_packer_write_float_value(pk, obj);
return self;
}
|
#write_float32(numeric) ⇒ Object
254 255 256 257 258 259 260 261 262 263 |
# File 'ext/msgpack/packer_class.c', line 254
static VALUE Packer_write_float32(VALUE self, VALUE numeric)
{
if(!rb_obj_is_kind_of(numeric, rb_cNumeric)) {
rb_raise(rb_eArgError, "Expected numeric");
}
PACKER(self, pk);
msgpack_packer_write_float(pk, (float)rb_num2dbl(numeric));
return self;
}
|
#write_hash(obj) ⇒ Object
188 189 190 191 192 193 194 |
# File 'ext/msgpack/packer_class.c', line 188
static VALUE Packer_write_hash(VALUE self, VALUE obj)
{
PACKER(self, pk);
Check_Type(obj, T_HASH);
msgpack_packer_write_hash_value(pk, obj);
return self;
}
|
#write_int(obj) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'ext/msgpack/packer_class.c', line 204
static VALUE Packer_write_int(VALUE self, VALUE obj)
{
PACKER(self, pk);
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
240 241 242 243 244 245 |
# File 'ext/msgpack/packer_class.c', line 240
static VALUE Packer_write_map_header(VALUE self, VALUE n)
{
PACKER(self, pk);
msgpack_packer_write_map_header(pk, NUM2UINT(n));
return self;
}
|
#write_nil ⇒ Object
132 133 134 135 136 137 |
# File 'ext/msgpack/packer_class.c', line 132
static VALUE Packer_write_nil(VALUE self)
{
PACKER(self, pk);
msgpack_packer_write_nil(pk);
return self;
}
|
#write_string(obj) ⇒ Object
160 161 162 163 164 165 166 |
# File 'ext/msgpack/packer_class.c', line 160
static VALUE Packer_write_string(VALUE self, VALUE obj)
{
PACKER(self, pk);
Check_Type(obj, T_STRING);
msgpack_packer_write_string_value(pk, obj);
return self;
}
|
#write_symbol(obj) ⇒ Object
196 197 198 199 200 201 202 |
# File 'ext/msgpack/packer_class.c', line 196
static VALUE Packer_write_symbol(VALUE self, VALUE obj)
{
PACKER(self, pk);
Check_Type(obj, T_SYMBOL);
msgpack_packer_write_symbol_value(pk, obj);
return self;
}
|
#write_to(io) ⇒ Object
320 321 322 323 324 325 |
# File 'ext/msgpack/packer_class.c', line 320
static VALUE Packer_write_to(VALUE self, VALUE io)
{
PACKER(self, pk);
size_t sz = msgpack_buffer_flush_to_io(PACKER_BUFFER_(pk), io, s_write, true);
return ULONG2NUM(sz);
}
|
#write_true ⇒ Object
139 140 141 142 143 144 |
# File 'ext/msgpack/packer_class.c', line 139
static VALUE Packer_write_true(VALUE self)
{
PACKER(self, pk);
msgpack_packer_write_true(pk);
return self;
}
|