Class: FFI::MsgPack::MsgObject

Inherits:
Struct
  • Object
show all
Defined in:
lib/ffi/msgpack/msg_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr = nil) ⇒ MsgObject

Initializes a new Msg Object.

Parameters:

  • ptr (FFI::Pointer) (defaults to: nil)

    An optional pointer to an existing Msg Object.



17
18
19
20
21
22
23
# File 'lib/ffi/msgpack/msg_object.rb', line 17

def initialize(ptr=nil)
  if ptr
    super(ptr)
  else
    super()
  end
end

Class Method Details

.new_array(values, ptr = nil) ⇒ MsgObject

Creates a new array Msg Object.

Parameters:

  • values (Array)

    The values for the array.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ffi/msgpack/msg_object.rb', line 154

def MsgObject.new_array(values,ptr=nil)
  entries = FFI::MemoryPointer.new(MsgObject,values.length)

  values.each_with_index do |value,index|
    MsgObject.new_object(value,entries[index])
  end

  obj = MsgObject.new(ptr)
  obj[:type] = :array
  obj[:values][:array][:size] = values.length
  obj[:values][:array][:ptr] = entries

  return obj
end

.new_boolean(value, ptr = nil) ⇒ MsgObject

Creates a new boolean Msg Object.

Parameters:

  • value (Boolean)

    The boolean value.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:

  • (MsgObject)

    The new boolean Msg Object.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ffi/msgpack/msg_object.rb', line 53

def MsgObject.new_boolean(value,ptr=nil)
  obj = MsgObject.new(ptr)
  obj[:type] = :boolean
  obj[:values][:boolean] = if value
                             1
                           else
                             0
                           end

  return obj
end

.new_double(value, ptr = nil) ⇒ MsgObject

Creates a new floating-point Msg Object.

Parameters:

  • value (Double, Float)

    The floating-point value.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:

  • (MsgObject)

    The floating-point Msg Object.



103
104
105
106
107
108
109
# File 'lib/ffi/msgpack/msg_object.rb', line 103

def MsgObject.new_double(value,ptr=nil)
  obj = MsgObject.new(ptr)
  obj[:type] = :double
  obj[:values][:dec] = value

  return obj
end

.new_float(value, ptr = nil) ⇒ Object

See Also:



114
115
116
# File 'lib/ffi/msgpack/msg_object.rb', line 114

def MsgObject.new_float(value,ptr=nil)
  MsgObject.new_double(value,ptr)
end

.new_integer(value, ptr = nil) ⇒ MsgObject

Creates a new integer Msg Object.

Parameters:

  • value (Integer)

    The integer value.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:

  • (MsgObject)

    The new integer Msg Object.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ffi/msgpack/msg_object.rb', line 77

def MsgObject.new_integer(value,ptr=nil)
  obj = MsgObject.new(ptr)

  if value < 0
    obj[:type] = :negative_integer
    obj[:values][:i64] = value
  else
    obj[:type] = :positive_integer
    obj[:values][:u64] = value
  end

  return obj
end

.new_map(values, ptr = nil) ⇒ MsgObject

Creates a new map Msg Object.

Parameters:

  • values (Hash)

    The values for the map.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ffi/msgpack/msg_object.rb', line 181

def MsgObject.new_map(values,ptr=nil)
  entries = FFI::MemoryPointer.new(MsgKeyValue,values.length)

  values.each_with_index do |(key,value),index|
    pair = MsgKeyValue.new(entries[index])

    pair[:key] = MsgObject.new_object(key)
    pair[:value] = MsgObject.new_object(value)
  end

  obj = MsgObject.new
  obj[:type] = :map
  obj[:values][:map][:size] = values.length
  obj[:values][:map][:ptr] = entries

  return obj
end

.new_nil(ptr = nil) ⇒ MsgObject

Creates a new nil Msg Object.

Parameters:

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:

  • (MsgObject)

    A new Msg Object that represents the nil value.



34
35
36
37
38
39
# File 'lib/ffi/msgpack/msg_object.rb', line 34

def MsgObject.new_nil(ptr=nil)
  obj = MsgObject.new(ptr)
  obj[:type] = :nil

  return obj
end

.new_object(value, ptr = nil) ⇒ MsgObject

Creates a Msg Object from a given Ruby object.

Parameters:

  • value (Object)

    The Ruby object.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ffi/msgpack/msg_object.rb', line 211

def MsgObject.new_object(value,ptr=nil)
  case value
  when Hash
    MsgObject.new_map(value,ptr)
  when Array
    MsgObject.new_array(value,ptr)
  when String, Symbol
    MsgObject.new_raw(value.to_s,ptr)
  when Float
    MsgObject.new_double(value,ptr)
  when Integer
    MsgObject.new_integer(value,ptr)
  when TrueClass, FalseClass
    MsgObject.new_boolean(value,ptr)
  when NilClass
    MsgObject.new_nil(ptr)
  else
    raise(ArgumentError,"ambigious object to create MsgObject from: #{value.inspect}",caller)
  end
end

.new_raw(data, ptr = nil) ⇒ MsgObject

Creates a new raw Msg Object.

Parameters:

  • data (String)

    The raw data.

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to create the Msg Object at.

Returns:



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ffi/msgpack/msg_object.rb', line 130

def MsgObject.new_raw(data,ptr=nil)
  buffer = FFI::MemoryPointer.new(:uchar, data.length)
  buffer.put_bytes(0,data)

  obj = MsgObject.new(ptr)
  obj[:type] = :raw
  obj[:values][:raw][:size] = data.length
  obj[:values][:raw][:ptr] = buffer

  return obj
end

Instance Method Details

#to_rubyObject

The value of the Msg Object.

Returns:

  • (Object)

    The value of the Msg Object, expressed as a Ruby primitive.

Raises:

  • (RuntimeError)

    The type of the Msg Object was unknown.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ffi/msgpack/msg_object.rb', line 261

def to_ruby
  case self[:type]
  when :nil
    nil
  when :boolean
    if self[:values][:boolean] == 0
      false
    else
      true
    end
  when :positive_integer
    self[:values][:u64]
  when :negative_integer
    self[:values][:i64]
  when :double
    self[:values][:dec]
  when :raw
    self[:values][:raw].to_s
  when :array
    self[:values][:array].to_a
  when :map
    self[:values][:map].to_hash
  else
    raise(RuntimeError,"unknown msgpack object type",caller)
  end
end

#typeSymbol

The type of the Msg Object.

Returns:

  • (Symbol)

    The type of the Msg Object.



238
239
240
# File 'lib/ffi/msgpack/msg_object.rb', line 238

def type
  self[:type]
end

#valuesMsgObjectUnion

The union of values for the Msg Object.

Returns:



248
249
250
# File 'lib/ffi/msgpack/msg_object.rb', line 248

def values
  self[:values]
end