Class: MessagePack::Packer

Inherits:
Object
  • Object
show all
Defined in:
ext/msgpack/packer_class.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



62
63
64
65
66
67
68
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
# File 'ext/msgpack/packer_class.c', line 62

static VALUE 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);
    MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);

    // TODO MessagePack_Unpacker_initialize and options

    return self;
}

Instance Method Details

#bufferObject



97
98
99
100
101
# File 'ext/msgpack/packer_class.c', line 97

static VALUE Packer_buffer(VALUE self)
{
    PACKER(self, pk);
    return pk->buffer_ref;
}

#clearObject

delegation methods



138
139
140
141
142
143
# File 'ext/msgpack/packer_class.c', line 138

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

#empty?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
# File 'ext/msgpack/packer_class.c', line 152

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



131
132
133
134
135
136
# File 'ext/msgpack/packer_class.c', line 131

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

#sizeObject



145
146
147
148
149
150
# File 'ext/msgpack/packer_class.c', line 145

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



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

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



162
163
164
165
166
# File 'ext/msgpack/packer_class.c', line 162

static VALUE Packer_to_str(VALUE self)
{
    PACKER(self, pk);
    return msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
}

#write(v) ⇒ Object Also known as: pack



103
104
105
106
107
108
# File 'ext/msgpack/packer_class.c', line 103

static VALUE Packer_write(VALUE self, VALUE v)
{
    PACKER(self, pk);
    msgpack_packer_write_value(pk, v);
    return self;
}

#write_array_header(n) ⇒ Object



117
118
119
120
121
122
# File 'ext/msgpack/packer_class.c', line 117

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

#write_map_header(n) ⇒ Object



124
125
126
127
128
129
# File 'ext/msgpack/packer_class.c', line 124

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



110
111
112
113
114
115
# File 'ext/msgpack/packer_class.c', line 110

static VALUE Packer_write_nil(VALUE self)
{
    PACKER(self, pk);
    msgpack_packer_write_nil(pk);
    return self;
}

#write_to(io) ⇒ Object



174
175
176
177
178
179
# File 'ext/msgpack/packer_class.c', line 174

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