Class: IcAgent::Candid::VecClass

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

Overview

Represents an IDL Array

Instance Method Summary collapse

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(_type) ⇒ VecClass

Returns a new instance of VecClass.



675
676
677
678
# File 'lib/ic_agent/candid.rb', line 675

def initialize(_type)
  super()
  @interior_type = _type
end

Instance Method Details

#_build_type_table_impl(type_table) ⇒ Object



690
691
692
693
694
695
# File 'lib/ic_agent/candid.rb', line 690

def _build_type_table_impl(type_table)
  @interior_type.build_type_table(type_table)
  op_code = LEB128.encode_signed(TypeIds::Vec).string
  buffer = @interior_type.encode_type(type_table)
  type_table.add(self, op_code + buffer)
end

#covariant(x) ⇒ Object



680
681
682
# File 'lib/ic_agent/candid.rb', line 680

def covariant(x)
  x.is_a?(Enumerable) && !x.any? { |item| !@interior_type.covariant(item) }
end

#decode_value(b, t) ⇒ Object



697
698
699
700
701
702
703
704
705
# File 'lib/ic_agent/candid.rb', line 697

def decode_value(b, t)
  vec = check_type(t)
  raise 'Not a vector type' unless vec.is_a?(VecClass)

  length = IcAgent::Candid.leb128u_decode(b)
  rets = []
  length.times { rets << @interior_type.decode_value(b, @interior_type) }
  rets
end

#displayObject



715
716
717
# File 'lib/ic_agent/candid.rb', line 715

def display
  "vec #{@interior_type.display}"
end

#encode_value(val) ⇒ Object



684
685
686
687
688
# File 'lib/ic_agent/candid.rb', line 684

def encode_value(val)
  length = LEB128.encode_signed(val.length).string
  vec = val.map { |v| @interior_type.encode_value(v) }
  (length + vec.join).b
end

#idObject



711
712
713
# File 'lib/ic_agent/candid.rb', line 711

def id
  TypeIds::Vec
end

#nameObject



707
708
709
# File 'lib/ic_agent/candid.rb', line 707

def name
  "vec (#{@interior_type.name})"
end