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.
-
#initialize(ptr = nil) ⇒ Packer
constructor
Creates a new Packer.
-
#inspect ⇒ String
Inspects the packer.
-
#pack(msg) ⇒ Integer
Packs a Msg Object.
-
#to_s ⇒ String?
The contents of the buffer.
Constructor Details
#initialize(ptr = nil) ⇒ Packer
Creates a new FFI::MsgPack::Packer.
73 74 75 76 77 78 79 80 81 |
# File 'lib/ffi/msgpack/packer.rb', line 73 def initialize(ptr=nil) if ptr super(ptr) else super() end @object = MsgObject.new end |
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.
137 138 139 140 141 142 |
# File 'lib/ffi/msgpack/packer.rb', line 137 def <<(value) @object.set!(value) pack(@object) return self end |
#callback {|packed(,length)| ... } ⇒ Proc
Sets the write callback for the packer.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ffi/msgpack/packer.rb', line 99 def callback(&block) self[:callback] = Proc.new do |data_ptr,packed_buffer,length| @total += length packed = packed_buffer.read_bytes(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.
161 162 163 164 165 166 167 168 169 |
# File 'lib/ffi/msgpack/packer.rb', line 161 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.
124 125 126 |
# File 'lib/ffi/msgpack/packer.rb', line 124 def pack(msg) MsgPack.msgpack_pack_object(self,msg) end |
#to_s ⇒ String?
The contents of the buffer.
151 152 153 |
# File 'lib/ffi/msgpack/packer.rb', line 151 def to_s @buffer.to_s if @buffer end |