class DBus::Data::Variant
A generic type.
Implementation note: @value is a {Data::Base}.
Attributes
@return [Type]
Public Class Methods
Source
# 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
@param member_type
[Type]
Source
# 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
@param value [::Object] @param type [Type] @return [Variant]
Source
# File lib/dbus/data.rb, line 763 def self.guess_type(value) sct, = PacketMarshaller.make_variant(value) DBus.type(sct) end
Determine the type of value @param value [::Object] @return [Type] @api private See also {PacketMarshaller.make_variant}
Source
# 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
@param member_type
[SingleCompleteType,Type,nil]
Calls superclass method
DBus::Data::Base::new
Source
# File lib/dbus/data.rb, line 743 def self.type # memoize @type ||= Type.new(type_code).freeze end
@return [Type]
Public Instance Methods
Source
# 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
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
Source
# File lib/dbus/data.rb, line 751 def type self.class.type end
Note that for Variants type.to_s==“v”, for the specific see {Variant#member_type} @return [Type] the exact type of this value