Class: CBOR::Item
- Inherits:
-
Struct
- Object
- Struct
- CBOR::Item
- Defined in:
- lib/libcbor/item.rb
Overview
Wraps native cbor_item_t * to allow extracting Ruby objects
Takes responsibility for high-level native interfacing and object lifetime management.
Instance Attribute Summary collapse
-
#handle ⇒ FFI::Pointer
Native memory handle.
Instance Method Summary collapse
-
#type ⇒ Symbol
Type of the underlying item.
-
#value ⇒ Fixnum, ...
Ruby representation of the Item.
Instance Attribute Details
#handle ⇒ FFI::Pointer
Native memory handle
8 9 10 |
# File 'lib/libcbor/item.rb', line 8 def handle @handle end |
Instance Method Details
#type ⇒ Symbol
Type of the underlying item
12 13 14 |
# File 'lib/libcbor/item.rb', line 12 def type LibCBOR.cbor_typeof(handle) end |
#value ⇒ Fixnum, ...
Ruby representation of the CBOR::Item
Arrays and hash maps are constructed recursively
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/libcbor/item.rb', line 21 def value case type when :uint LibCBOR.cbor_get_int(handle) when :negint -LibCBOR.cbor_get_int(handle) - 1 when :string load_string when :bytestring load_bytestring when :array LibCBOR .cbor_array_handle(handle) .read_array_of_type(LibCBOR::CborItemTRef, :read_pointer, LibCBOR.cbor_array_size(handle)) .map { |item| Item.new(item).value } when :map pairs_handle = LibCBOR.cbor_map_handle(handle) Hash[LibCBOR.cbor_map_size(handle).times.map { |idx| pair = LibCBOR::CborPair.new(pairs_handle + LibCBOR::CborPair.size * idx) [pair[:key], pair[:value]].map { |ptr| Item.new(ptr).value } }] when :tag Tag.new( LibCBOR.cbor_tag_value(handle), Item.new(LibCBOR.cbor_tag_item(handle)).value ) when :float_ctrl load_float else raise 'Unknown type - the FFI enum mapping is probably broken. Please report this bug.' end end |