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.
-
#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 |
# 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.
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.
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.
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
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.
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.
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.
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.
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.
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_ruby ⇒ Object
The value of the Msg Object.
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 |
#type ⇒ Symbol
The type of the Msg Object.
238 239 240 |
# File 'lib/ffi/msgpack/msg_object.rb', line 238 def type self[:type] end |
#values ⇒ MsgObjectUnion
The union of values for the Msg Object.
248 249 250 |
# File 'lib/ffi/msgpack/msg_object.rb', line 248 def values self[:values] end |