Class: FFI::MsgPack::MsgObject
- Inherits:
-
Struct
- Object
- Struct
- FFI::MsgPack::MsgObject
- Defined in:
- lib/ffi/msgpack/msg_object.rb
Class Method Summary collapse
-
.new_array(values, ptr = nil) ⇒ MsgObject
Creates a new array Msg Object.
-
.new_boolean(value, ptr = nil) ⇒ MsgObject
Creates a new boolean Msg Object.
-
.new_double(value, ptr = nil) ⇒ MsgObject
Creates a new floating-point Msg Object.
- .new_float(value, ptr = nil) ⇒ Object
-
.new_integer(value, ptr = nil) ⇒ MsgObject
Creates a new integer Msg Object.
-
.new_map(values, ptr = nil) ⇒ MsgObject
Creates a new map Msg Object.
-
.new_nil(ptr = nil) ⇒ MsgObject
Creates a new
nil
Msg Object. -
.new_object(value, ptr = nil) ⇒ MsgObject
Creates a Msg Object from a given Ruby object.
-
.new_raw(data, ptr = nil) ⇒ MsgObject
Creates a new raw Msg Object.
Instance Method Summary collapse
-
#initialize(ptr = nil) ⇒ MsgObject
constructor
Initializes a new Msg Object.
-
#set!(value) ⇒ MsgObject
Sets the contents of the Msg Object using a Ruby Object.
-
#set_array!(values) ⇒ Object
Sets the Msg Objects value to an Array value.
-
#set_boolean!(value) ⇒ Object
Sets the Msg Objects value to a Boolean value.
-
#set_double!(value) ⇒ Object
(also: #set_float!)
Sets the Msg Objects value to a Floating Point value.
-
#set_integer!(value) ⇒ Object
Sets the Msg Objects value to an Integer value.
-
#set_map!(values) ⇒ Object
Sets the Msg Objects value to a Map value.
-
#set_nil! ⇒ Object
Sets the Msg Objects value to nil.
-
#set_raw!(value) ⇒ Object
Sets the Msg Objects value to a String value.
-
#to_ruby ⇒ Object
The value of the Msg Object.
-
#type ⇒ Symbol
The type of the Msg Object.
-
#values ⇒ MsgObjectUnion
The union of values for the Msg Object.
Constructor Details
#initialize(ptr = nil) ⇒ MsgObject
Initializes a new Msg Object.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ffi/msgpack/msg_object.rb', line 17 def initialize(ptr=nil) if ptr super(ptr) else super() end @object = nil @memory = nil end |
Class Method Details
.new_array(values, ptr = nil) ⇒ MsgObject
Creates a new array Msg Object.
136 137 138 139 140 |
# File 'lib/ffi/msgpack/msg_object.rb', line 136 def MsgObject.new_array(values,ptr=nil) obj = MsgObject.new(ptr) obj.set_array!(values) return obj end |
.new_boolean(value, ptr = nil) ⇒ MsgObject
Creates a new boolean Msg Object.
55 56 57 58 59 |
# File 'lib/ffi/msgpack/msg_object.rb', line 55 def MsgObject.new_boolean(value,ptr=nil) obj = MsgObject.new(ptr) obj.set_boolean!(value) return obj end |
.new_double(value, ptr = nil) ⇒ MsgObject
Creates a new floating-point Msg Object.
91 92 93 94 95 |
# File 'lib/ffi/msgpack/msg_object.rb', line 91 def MsgObject.new_double(value,ptr=nil) obj = MsgObject.new(ptr) obj.set_double!(value) return obj end |
.new_float(value, ptr = nil) ⇒ Object
100 101 102 103 104 |
# File 'lib/ffi/msgpack/msg_object.rb', line 100 def MsgObject.new_float(value,ptr=nil) obj = MsgObject.new(ptr) obj.set_double!(value) return obj end |
.new_integer(value, ptr = nil) ⇒ MsgObject
Creates a new integer Msg Object.
73 74 75 76 77 |
# File 'lib/ffi/msgpack/msg_object.rb', line 73 def MsgObject.new_integer(value,ptr=nil) obj = MsgObject.new(ptr) obj.set_integer!(value) return obj end |
.new_map(values, ptr = nil) ⇒ MsgObject
Creates a new map Msg Object.
154 155 156 157 158 |
# File 'lib/ffi/msgpack/msg_object.rb', line 154 def MsgObject.new_map(values,ptr=nil) obj = MsgObject.new(ptr) obj.set_map!(values) return obj end |
.new_nil(ptr = nil) ⇒ MsgObject
Creates a new nil
Msg Object.
37 38 39 40 41 |
# File 'lib/ffi/msgpack/msg_object.rb', line 37 def MsgObject.new_nil(ptr=nil) obj = MsgObject.new(ptr) obj.set_nil! return obj end |
Instance Method Details
#set!(value) ⇒ MsgObject
Sets the contents of the Msg Object using a Ruby Object.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/ffi/msgpack/msg_object.rb', line 337 def set!(value) case value when Hash set_map!(value) when Array set_array!(value) when String, Symbol set_raw!(value) when Float set_double!(value) when Integer set_integer!(value) when TrueClass, FalseClass set_boolean!(value) when NilClass set_nil! else raise(TypeError,"ambigious object to create MsgObject from: #{value.inspect}") end return value end |
#set_array!(values) ⇒ Object
Sets the Msg Objects value to an Array value.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/ffi/msgpack/msg_object.rb', line 279 def set_array!(values) @objects = [] @memory = FFI::MemoryPointer.new(MsgObject,values.length) values.each_with_index do |value,index| @objects << MsgObject.new_object(value,@memory[index]) end self[:type] = :array array = self[:values][:array] array[:size] = values.length array[:ptr] = @memory end |
#set_boolean!(value) ⇒ Object
Sets the Msg Objects value to a Boolean value.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ffi/msgpack/msg_object.rb', line 198 def set_boolean!(value) @objects = nil @memory = nil self[:type] = :boolean self[:values][:boolean] = if value 1 else 0 end end |
#set_double!(value) ⇒ Object Also known as: set_float!
Sets the Msg Objects value to a Floating Point value.
239 240 241 242 243 244 245 |
# File 'lib/ffi/msgpack/msg_object.rb', line 239 def set_double!(value) @objects = nil @memory = nil self[:type] = :double self[:values][:dec] = value end |
#set_integer!(value) ⇒ Object
Sets the Msg Objects value to an Integer value.
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ffi/msgpack/msg_object.rb', line 218 def set_integer!(value) @objects = nil @memory = nil if value < 0 self[:type] = :negative_integer self[:values][:i64] = value else self[:type] = :positive_integer self[:values][:u64] = value end end |
#set_map!(values) ⇒ Object
Sets the Msg Objects value to a Map value.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/ffi/msgpack/msg_object.rb', line 302 def set_map!(values) @objects = {} @memory = FFI::MemoryPointer.new(MsgKeyValue,values.length) values.each_with_index do |(key,value),index| pair = MsgKeyValue.new(@memory[index]) key_obj = MsgObject.new_object(key,pair[:key].to_ptr) value_obj = MsgObject.new_object(value,pair[:value].to_ptr) @objects[key_obj] = value_obj end self[:type] = :map map = self[:values][:map] map[:size] = values.length map[:ptr] = @memory end |
#set_nil! ⇒ Object
Sets the Msg Objects value to nil.
183 184 185 186 187 188 |
# File 'lib/ffi/msgpack/msg_object.rb', line 183 def set_nil! @objects = nil @memory = nil self[:type] = :nil end |
#set_raw!(value) ⇒ Object
Sets the Msg Objects value to a String value.
257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/ffi/msgpack/msg_object.rb', line 257 def set_raw!(value) value = value.to_s @objects = nil @memory = FFI::MemoryPointer.new(:uchar, value.length) @memory.put_bytes(0,value) self[:type] = :raw raw = self[:values][:raw] raw[:size] = value.length raw[:ptr] = @memory end |
#to_ruby ⇒ Object
The value of the Msg Object.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/ffi/msgpack/msg_object.rb', line 389 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("unknown msgpack object type") end end |
#type ⇒ Symbol
The type of the Msg Object.
366 367 368 |
# File 'lib/ffi/msgpack/msg_object.rb', line 366 def type self[:type] end |
#values ⇒ MsgObjectUnion
The union of values for the Msg Object.
376 377 378 |
# File 'lib/ffi/msgpack/msg_object.rb', line 376 def values self[:values] end |