Class: DBus::Data::Variant
Overview
A generic type.
Implementation note: @value is a Base.
Instance Attribute Summary collapse
- #member_type ⇒ Type readonly
Class Method Summary collapse
- .alignment ⇒ Object
- .from_items(value, mode:, member_type:) ⇒ Object
- .from_typed(value, type:) ⇒ Variant
-
.guess_type(value) ⇒ Type
private
Determine the type of value See also PacketMarshaller.make_variant.
- .type ⇒ Type
- .type_code ⇒ Object
Instance Method Summary collapse
-
#[](index) ⇒ Object
Internal helpers to keep the DBus.variant method working.
- #first ⇒ Object
-
#initialize(value, member_type:) ⇒ Variant
constructor
A new instance of Variant.
- #last ⇒ Object
-
#type ⇒ Type
Note that for Variants type.to_s==“v”, for the specific see #member_type.
- #value ⇒ Object
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.
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_type ⇒ Type (readonly)
756 757 758 |
# File 'lib/dbus/data.rb', line 756 def member_type @member_type end |
Class Method Details
.alignment ⇒ Object
717 718 719 |
# File 'lib/dbus/data.rb', line 717 def self.alignment 1 end |
.from_items(value, mode:, member_type:) ⇒ Object
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
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
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 |
.type ⇒ Type
743 744 745 746 |
# File 'lib/dbus/data.rb', line 743 def self.type # memoize @type ||= Type.new(type_code).freeze end |
.type_code ⇒ Object
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 |
#type ⇒ Type
Note that for Variants type.to_s==“v”, for the specific see #member_type
751 752 753 |
# File 'lib/dbus/data.rb', line 751 def type self.class.type end |
#value ⇒ Object
721 722 723 |
# File 'lib/dbus/data.rb', line 721 def value @value.value end |