Class: DBus::Data::Array

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

Overview

An Array, or a Dictionary (Hash).

Instance Attribute Summary

Attributes inherited from Container

#type

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?, #type

Constructor Details

#initialize(value, type:) ⇒ Array

Returns a new instance of Array.

Parameters:



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/dbus/data.rb', line 597

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

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

                  value.exact_value
                else
                  # TODO: Dict??
                  value.map do |i|
                    Data.make_typed(type.child, i)
                  end
                end
  super(typed_value)
end

Class Method Details

.alignmentObject



555
556
557
# File 'lib/dbus/data.rb', line 555

def self.alignment
  4
end

.from_items(value, mode:, type:, hash: false) ⇒ Data::Array

TODO: check that Hash keys are basic types

Parameters:

  • mode (:plain, :exact)
  • type (Type)
  • hash (Boolean) (defaults to: false)

    are we unmarshalling an ARRAY of DICT_ENTRY

Returns:



564
565
566
567
568
569
# File 'lib/dbus/data.rb', line 564

def self.from_items(value, mode:, type:, hash: false)
  value = Hash[value] if hash
  return value if mode == :plain

  new(value, type: type)
end

.from_typed(value, type:) ⇒ Data::Array

Parameters:

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

Returns:



574
575
576
# File 'lib/dbus/data.rb', line 574

def self.from_typed(value, type:)
  new(value, type: type) # initialize(::Array<Data::Base>)
end

.type_codeObject



551
552
553
# File 'lib/dbus/data.rb', line 551

def self.type_code
  "a"
end

Instance Method Details

#valueObject



578
579
580
581
582
583
584
585
586
# File 'lib/dbus/data.rb', line 578

def value
  v = super
  if type.child.sigtype == Type::DICT_ENTRY
    # BTW this makes a copy so mutating it is pointless
    v.to_h
  else
    v
  end
end