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
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 112 |
# File 'ext/msgpack/packer_class.c', line 71
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);
}
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, sym_compatibility_mode);
msgpack_packer_set_compat(pk, RTEST(v));
}
return self;
}
|
Instance Method Details
#buffer ⇒ Object
120 121 122 123 124 |
# File 'ext/msgpack/packer_class.c', line 120
static VALUE Packer_buffer(VALUE self)
{
PACKER(self, pk);
return pk->buffer_ref;
}
|
#compatibility_mode? ⇒ Boolean
114 115 116 117 118 |
# File 'ext/msgpack/packer_class.c', line 114
static VALUE Packer_compatibility_mode_p(VALUE self)
{
PACKER(self, pk);
return pk->compatibility_mode ? Qtrue : Qfalse;
}
|
#empty? ⇒ Boolean
299 300 301 302 303 304 305 306 307 |
# File 'ext/msgpack/packer_class.c', line 299
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
278 279 280 281 282 283 |
# File 'ext/msgpack/packer_class.c', line 278
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);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'ext/msgpack/packer_class.c', line 393
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 |
# 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();
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
285 286 287 288 289 290 |
# File 'ext/msgpack/packer_class.c', line 285
static VALUE Packer_reset(VALUE self)
{
PACKER(self, pk);
msgpack_buffer_clear(PACKER_BUFFER_(pk));
return Qnil;
}
|
#size ⇒ Object
292 293 294 295 296 297 |
# File 'ext/msgpack/packer_class.c', line 292
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
315 316 317 318 319 |
# File 'ext/msgpack/packer_class.c', line 315
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
309 310 311 312 313 |
# File 'ext/msgpack/packer_class.c', line 309
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
126 127 128 129 130 131 |
# File 'ext/msgpack/packer_class.c', line 126
static VALUE Packer_write(VALUE self, VALUE v)
{
PACKER(self, pk);
msgpack_packer_write_value(pk, v);
return self;
}
|
#write_array(obj) ⇒ Object
181 182 183 184 185 186 187 |
# File 'ext/msgpack/packer_class.c', line 181
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
234 235 236 237 238 239 |
# File 'ext/msgpack/packer_class.c', line 234
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
169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/msgpack/packer_class.c', line 169
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
248 249 250 251 252 253 |
# File 'ext/msgpack/packer_class.c', line 248
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
266 267 268 269 270 271 272 273 274 275 276 |
# File 'ext/msgpack/packer_class.c', line 266
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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'ext/msgpack/packer_class.c', line 218
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
147 148 149 150 151 152 |
# File 'ext/msgpack/packer_class.c', line 147
static VALUE Packer_write_false(VALUE self)
{
PACKER(self, pk);
msgpack_packer_write_false(pk);
return self;
}
|
#write_float(obj) ⇒ Object
154 155 156 157 158 159 |
# File 'ext/msgpack/packer_class.c', line 154
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
255 256 257 258 259 260 261 262 263 264 |
# File 'ext/msgpack/packer_class.c', line 255
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
189 190 191 192 193 194 195 |
# File 'ext/msgpack/packer_class.c', line 189
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
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'ext/msgpack/packer_class.c', line 205
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
241 242 243 244 245 246 |
# File 'ext/msgpack/packer_class.c', line 241
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
133 134 135 136 137 138 |
# File 'ext/msgpack/packer_class.c', line 133
static VALUE Packer_write_nil(VALUE self)
{
PACKER(self, pk);
msgpack_packer_write_nil(pk);
return self;
}
|
#write_string(obj) ⇒ Object
161 162 163 164 165 166 167 |
# File 'ext/msgpack/packer_class.c', line 161
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
197 198 199 200 201 202 203 |
# File 'ext/msgpack/packer_class.c', line 197
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
321 322 323 324 325 326 |
# File 'ext/msgpack/packer_class.c', line 321
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 SIZET2NUM(sz);
}
|
#write_true ⇒ Object
140 141 142 143 144 145 |
# File 'ext/msgpack/packer_class.c', line 140
static VALUE Packer_write_true(VALUE self)
{
PACKER(self, pk);
msgpack_packer_write_true(pk);
return self;
}
|