Class: IcAgent::Candid::TupleClass

Inherits:
RecordClass show all
Defined in:
lib/ic_agent/candid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RecordClass

#_build_type_table_impl, #name, #try_as_tuple

Methods inherited from ConstructType

#check_type, #encode_type

Methods inherited from BaseType

_build_type_table_impl, #build_type_table, check_type, covariant, decode_value, encode_type, encode_value

Constructor Details

#initialize(*_components) ⇒ TupleClass

Returns a new instance of TupleClass.



851
852
853
854
855
856
857
858
# File 'lib/ic_agent/candid.rb', line 851

def initialize(*_components)
  x = {}
  _components.each_with_index do |v, i|
    x["_#{i}_"] = v
  end
  super(x)
  @components = _components
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



849
850
851
# File 'lib/ic_agent/candid.rb', line 849

def components
  @components
end

Instance Method Details

#covariant(x) ⇒ Object



860
861
862
863
864
865
866
867
868
869
870
871
# File 'lib/ic_agent/candid.rb', line 860

def covariant(x)
  unless x.is_a?(Array)
    raise ValueError, 'Expected tuple type input.'
  end

  @components.each_with_index do |v, idx|
    unless v.covariant(x[idx])
      return false
    end
  end
  x.length >= @fields.length
end

#decode_value(b, t) ⇒ Object



881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# File 'lib/ic_agent/candid.rb', line 881

def decode_value(b, t)
  tup = check_type(t)
  unless tup.is_a?(TupleClass)
    raise ValueError, 'not a tuple type'
  end
  unless tup.components.length == @components.length
    raise ValueError, 'tuple mismatch'
  end

  res = []
  tup.components.each_with_index do |wireType, i|
    if i >= @components.length
      wireType.decode_value(b, wireType)
    else
      res << @components[i].decode_value(b, wireType)
    end
  end
  res
end

#displayObject



905
906
907
908
# File 'lib/ic_agent/candid.rb', line 905

def display
  d = @components.map { |item| item.display() }
  "record {#{d.join(';')}}"
end

#encode_value(val) ⇒ Object



873
874
875
876
877
878
879
# File 'lib/ic_agent/candid.rb', line 873

def encode_value(val)
  bufs = ''.b
  @components.each_with_index do |_, i|
    bufs += @components[i].encode_value(val[i])
  end
  bufs
end

#idObject



901
902
903
# File 'lib/ic_agent/candid.rb', line 901

def id
  TypeIds::Tuple
end