class DBus::Data::Variant

A generic type.

Implementation note: @value is a {Data::Base}.

Attributes

member_type[R]

@return [Type]

Public Class Methods

alignment() click to toggle source
# File lib/dbus/data.rb, line 717
def self.alignment
  1
end
from_items(value, mode:, member_type:) click to toggle source

@param member_type [Type]

# File lib/dbus/data.rb, line 726
def self.from_items(value, mode:, member_type:)
  return value if mode == :plain

  new(value, member_type: member_type)
end
from_typed(value, type:) click to toggle source

@param value [::Object] @param type [Type] @return [Variant]

# File lib/dbus/data.rb, line 735
def self.from_typed(value, type:)
  assert_type_matches_class(type)

  # nil: decide on type of value
  new(value, member_type: nil)
end
guess_type(value) click to toggle source

Determine the type of value @param value [::Object] @return [Type] @api private See also {PacketMarshaller.make_variant}

# File lib/dbus/data.rb, line 763
def self.guess_type(value)
  sct, = PacketMarshaller.make_variant(value)
  DBus.type(sct)
end
new(value, member_type:) click to toggle source

@param member_type [SingleCompleteType,Type,nil]

Calls superclass method DBus::Data::Base::new
# File lib/dbus/data.rb, line 769
def initialize(value, member_type:)
  member_type = Type::Factory.make_type(member_type) if member_type
  # TODO: validate that the given *member_type* matches *value*
  case value
  when Data::Variant
    # Copy the contained value instead of boxing it more
    # TODO: except perhaps for round-tripping in exact mode?
    @member_type = value.member_type
    value = value.exact_value
  when Data::Base
    @member_type = member_type || value.type
    raise ArgumentError, "Variant type #{@member_type} does not match value type #{value.type}" \
      unless @member_type == value.type
  else
    @member_type = member_type || self.class.guess_type(value)
    value = Data.make_typed(@member_type, value)
  end
  super(value)
end
type() click to toggle source

@return [Type]

# File lib/dbus/data.rb, line 743
def self.type
  # memoize
  @type ||= Type.new(type_code).freeze
end
type_code() click to toggle source
# File lib/dbus/data.rb, line 713
def self.type_code
  "v"
end

Public Instance Methods

[](index) click to toggle source

Internal helpers to keep the {DBus.variant} method working. Formerly it returned just a pair of [DBus.type(string_type), value] so let’s provide [0], [1], .first, .last

# File lib/dbus/data.rb, line 792
def [](index)
  case index
  when 0
    member_type
  when 1
    value
  else
    raise ArgumentError, "DBus.variant can only be indexed with 0 or 1, seen #{index.inspect}"
  end
end
first() click to toggle source

@see []

# File lib/dbus/data.rb, line 804
def first
  self[0]
end
last() click to toggle source

@see []

# File lib/dbus/data.rb, line 809
def last
  self[1]
end
type() click to toggle source

Note that for Variants type.to_s==“v”, for the specific see {Variant#member_type} @return [Type] the exact type of this value

# File lib/dbus/data.rb, line 751
def type
  self.class.type
end
value() click to toggle source
# File lib/dbus/data.rb, line 721
def value
  @value.value
end