Class: DBus::Data::Struct

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

Overview

A fixed size, heterogenerous tuple.

(The item count is fixed, not the byte size.)

Direct Known Subclasses

DictEntry

Instance Attribute Summary

Attributes inherited from Container

#type

Attributes inherited from Base

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Container

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

Methods inherited from Base

assert_type_matches_class, basic?, #eql?, fixed?, #type

Constructor Details

#initialize(value, type:) ⇒ Struct

Returns a new instance of Struct.

Parameters:



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/dbus/data.rb', line 649

def initialize(value, type:)
  type = Type::Factory.make_type(type)
  self.class.assert_type_matches_class(type)
  @type = type

  typed_value = case value
                when self.class
                  unless value.type == type
                    raise ArgumentError,
                          "Specified type is #{type.inspect} but value type is #{value.type.inspect}"
                  end

                  value.exact_value
                else
                  member_types = type.members
                  unless value.size == member_types.size
                    raise ArgumentError, "Specified type has #{member_types.size} members " \
                                         "but value has #{value.size} members"
                  end

                  member_types.zip(value).map do |item_type, item|
                    Data.make_typed(item_type, item)
                  end
                end
  super(typed_value)
end

Class Method Details

.alignmentObject



628
629
630
# File 'lib/dbus/data.rb', line 628

def self.alignment
  8
end

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

Parameters:

  • value (::Array)


633
634
635
636
637
638
# File 'lib/dbus/data.rb', line 633

def self.from_items(value, mode:, type:)
  value.freeze
  return value if mode == :plain

  new(value, type: type)
end

.from_typed(value, type:) ⇒ Struct

Parameters:

  • value (::Object)

    (#size, #each)

  • type (Type)

Returns:



643
644
645
# File 'lib/dbus/data.rb', line 643

def self.from_typed(value, type:)
  new(value, type: type)
end

.type_codeObject



624
625
626
# File 'lib/dbus/data.rb', line 624

def self.type_code
  "r"
end

Instance Method Details

#==(other) ⇒ Object



676
677
678
679
680
681
682
683
684
# File 'lib/dbus/data.rb', line 676

def ==(other)
  case other
  when ::Struct
    @value.size == other.size &&
      @value.zip(other.to_a).all? { |i, other_i| i == other_i }
  else
    super
  end
end