Class: MessagePack::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/packer.rb,
ext/msgpack/packer_class.c

Instance Method Summary collapse

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

#bufferObject



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;
}

#clearObject

delegation methods



265
266
267
268
269
270
# File 'ext/msgpack/packer_class.c', line 265

static VALUE Packer_clear(VALUE self)
{
    PACKER(self, pk);
    msgpack_buffer_clear(PACKER_BUFFER_(pk));
    return Qnil;
}

#compatibility_mode?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


279
280
281
282
283
284
285
286
287
# File 'ext/msgpack/packer_class.c', line 279

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;
    }
}

#flushObject



258
259
260
261
262
263
# File 'ext/msgpack/packer_class.c', line 258

static VALUE Packer_flush(VALUE self)
{
    PACKER(self, pk);
    msgpack_buffer_flush(PACKER_BUFFER_(pk));
    return self;
}

#full_packObject

Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'ext/msgpack/packer_class.c', line 379

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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
# File 'ext/msgpack/packer_class.c', line 330

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_typesObject

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

#sizeObject



272
273
274
275
276
277
# File 'ext/msgpack/packer_class.c', line 272

static VALUE Packer_size(VALUE self)
{
    PACKER(self, pk);
    size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
    return SIZET2NUM(size);
}

#to_aObject



295
296
297
298
299
# File 'ext/msgpack/packer_class.c', line 295

static VALUE Packer_to_a(VALUE self)
{
    PACKER(self, pk);
    return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
}

#to_strObject Also known as: to_s



289
290
291
292
293
# File 'ext/msgpack/packer_class.c', line 289

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

Returns:

  • (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



168
169
170
171
172
173
174
# File 'ext/msgpack/packer_class.c', line 168

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



221
222
223
224
225
226
# File 'ext/msgpack/packer_class.c', line 221

static VALUE Packer_write_array_header(VALUE self, VALUE n)
{
    PACKER(self, pk);
    msgpack_packer_write_array_header(pk, NUM2UINT(n));
    return self;
}

#write_ext(type, data) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
# File 'ext/msgpack/packer_class.c', line 246

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/msgpack/packer_class.c', line 205

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_falseObject



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



235
236
237
238
239
240
241
242
243
244
# File 'ext/msgpack/packer_class.c', line 235

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



176
177
178
179
180
181
182
# File 'ext/msgpack/packer_class.c', line 176

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



192
193
194
195
196
197
198
199
200
201
202
203
# File 'ext/msgpack/packer_class.c', line 192

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



228
229
230
231
232
233
# File 'ext/msgpack/packer_class.c', line 228

static VALUE Packer_write_map_header(VALUE self, VALUE n)
{
    PACKER(self, pk);
    msgpack_packer_write_map_header(pk, NUM2UINT(n));
    return self;
}

#write_nilObject



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



184
185
186
187
188
189
190
# File 'ext/msgpack/packer_class.c', line 184

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



301
302
303
304
305
306
# File 'ext/msgpack/packer_class.c', line 301

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_trueObject



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;
}