Class: DBus::Data::Variant

Inherits:
Container show all
Defined in:
lib/dbus/data.rb

Overview

A generic type.

Implementation note: @value is a Base.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Container

basic?, #eql?, #exact_value, fixed?

Methods inherited from Base

#==, assert_type_matches_class, basic?, #eql?, fixed?

Constructor Details

#initialize(value, member_type:) ⇒ Variant

Returns a new instance of Variant.

Parameters:



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# 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

Instance Attribute Details

#member_typeType (readonly)

Returns:



756
757
758
# File 'lib/dbus/data.rb', line 756

def member_type
  @member_type
end

Class Method Details

.alignmentObject



717
718
719
# File 'lib/dbus/data.rb', line 717

def self.alignment
  1
end

.from_items(value, mode:, member_type:) ⇒ Object

Parameters:

  • member_type (Type)


726
727
728
729
730
# 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:) ⇒ Variant

Parameters:

  • value (::Object)
  • type (Type)

Returns:



735
736
737
738
739
740
# 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) ⇒ Type

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.

Determine the type of value See also PacketMarshaller.make_variant

Parameters:

  • value (::Object)

Returns:



763
764
765
766
# File 'lib/dbus/data.rb', line 763

def self.guess_type(value)
  sct, = PacketMarshaller.make_variant(value)
  DBus.type(sct)
end

.typeType

Returns:



743
744
745
746
# File 'lib/dbus/data.rb', line 743

def self.type
  # memoize
  @type ||= Type.new(type_code).freeze
end

.type_codeObject



713
714
715
# File 'lib/dbus/data.rb', line 713

def self.type_code
  "v"
end

Instance Method Details

#[](index) ⇒ Object

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



792
793
794
795
796
797
798
799
800
801
# 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

#firstObject

See Also:



804
805
806
# File 'lib/dbus/data.rb', line 804

def first
  self[0]
end

#lastObject

See Also:



809
810
811
# File 'lib/dbus/data.rb', line 809

def last
  self[1]
end

#typeType

Note that for Variants type.to_s==“v”, for the specific see #member_type

Returns:

  • (Type)

    the exact type of this value



751
752
753
# File 'lib/dbus/data.rb', line 751

def type
  self.class.type
end

#valueObject



721
722
723
# File 'lib/dbus/data.rb', line 721

def value
  @value.value
end