Class: FFI::MsgPack::Packer
- Inherits:
-
Struct
- Object
- Struct
- FFI::MsgPack::Packer
- Defined in:
- lib/ffi/msgpack/packer.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
The optional buffer to write packed Msg Objects into.
-
#total ⇒ Object
The total length of the buffer.
Class Method Summary collapse
-
.create(buffer = nil) {|packed(,length)| ... } ⇒ Packer
Creates a new packer.
Instance Method Summary collapse
-
#<<(value) ⇒ Packer
Packs a Ruby object.
-
#callback {|packed(,length)| ... } ⇒ Proc
Sets the write callback for the packer.
-
#inspect ⇒ String
Inspects the packer.
-
#pack(msg) ⇒ Integer
Packs a Msg Object.
-
#to_s ⇒ String?
The contents of the buffer.
Instance Attribute Details
#buffer ⇒ Object
The optional buffer to write packed Msg Objects into.
12 13 14 |
# File 'lib/ffi/msgpack/packer.rb', line 12 def buffer @buffer end |
#total ⇒ Object
The total length of the buffer
15 16 17 |
# File 'lib/ffi/msgpack/packer.rb', line 15 def total @total end |
Class Method Details
.create(buffer = nil) {|packed(,length)| ... } ⇒ Packer
Creates a new packer.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ffi/msgpack/packer.rb', line 36 def Packer.create(buffer=nil,&block) packer = Packer.new() # zero the memory packer[:data] = nil packer[:callback] = nil # zero the total packer.total = 0 if block # disable the buffer packer.buffer = nil # custom callback packer.callback(&block) else # set the buffer packer.buffer = (buffer || '') # setup the default callback packer.callback do |packed,length| packer.buffer << packed end end return packer end |
Instance Method Details
#<<(value) ⇒ Packer
Packs a Ruby object.
119 120 121 122 |
# File 'lib/ffi/msgpack/packer.rb', line 119 def <<(value) pack(MsgObject.new_object(value)) return self end |
#callback {|packed(,length)| ... } ⇒ Proc
Sets the write callback for the packer.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ffi/msgpack/packer.rb', line 81 def callback(&block) self[:callback] = Proc.new do |data_ptr,packed_ptr,length| @total += length packed = packed_ptr.get_bytes(0,length) if block.arity == 2 block.call(packed,length) else block.call(packed) end 0 # return 0 to indicate success end end |
#inspect ⇒ String
Inspects the packer.
141 142 143 144 145 146 147 148 149 |
# File 'lib/ffi/msgpack/packer.rb', line 141 def inspect addr = ('0x%x' % self.pointer.address) if @buffer "#<#{self.class}:#{addr}: #{@buffer.inspect}>" else "#<#{self.class}:#{addr}>" end end |
#pack(msg) ⇒ Integer
Packs a Msg Object.
106 107 108 |
# File 'lib/ffi/msgpack/packer.rb', line 106 def pack(msg) MsgPack.msgpack_pack_object(self,msg) end |
#to_s ⇒ String?
The contents of the buffer.
131 132 133 |
# File 'lib/ffi/msgpack/packer.rb', line 131 def to_s @buffer.to_s if @buffer end |