Class: RabbitMQ::FFI::FieldValue Private

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/rabbitmq/ffi.rb,
lib/rabbitmq/ffi/ext/field_value.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
# File 'lib/rabbitmq/ffi/ext/field_value.rb', line 53

def self.from(value)
  new.apply(value)
end

Instance Method Details

#apply(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rabbitmq/ffi/ext/field_value.rb', line 37

def apply(value)
  self[:kind], self[:value] = case value
  when ::String; [:utf8,  FieldValueValue.new(Bytes.from_s(value.encode(Encoding::UTF_8)).pointer)]
  when ::Symbol; [:utf8,  FieldValueValue.new(Bytes.from_s(value.to_s.encode!(Encoding::UTF_8)).pointer)]
  when ::Array;  [:array, FieldValueValue.new(Array.from_a(value).pointer)]
  when ::Hash;   [:table, FieldValueValue.new(Table.from(value).pointer)]
  when ::Fixnum; [:i64,       (v=FieldValueValue.new; v[:i64]=value; v)]
  when ::Float;  [:f64,       (v=FieldValueValue.new; v[:f64]=value; v)]
  when ::Time;   [:timestamp, (v=FieldValueValue.new; v[:u64]=value.to_i; v)]
  when true;     [:boolean,   (v=FieldValueValue.new; v[:boolean]=true; v)]
  when false;    [:boolean,   (v=FieldValueValue.new; v[:boolean]=false; v)]
  else raise NotImplementedError, "#{self.class}.from(#<#{value.class}>)"
  end
  self
end

#free!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
33
34
35
# File 'lib/rabbitmq/ffi/ext/field_value.rb', line 30

def free!
  kind  = self[:kind]
  value = self[:value][value_member(kind)]
  value.free! if value.respond_to? :free!
  self
end

#to_value(free = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rabbitmq/ffi/ext/field_value.rb', line 14

def to_value(free=false)
  kind   = self[:kind]
  value  = self[:value][value_member(kind)]
  result = case kind
  when :bytes;     value.to_s(free)
  when :utf8;      value.to_s(free).force_encoding(Encoding::UTF_8)
  when :timestamp; Time.at(value).utc
  when :table;     value.to_h(free)
  when :array;     value.to_a(free)
  else value
  end
  
  clear if free
  result
end