Class: IcAgent::Candid::VecClass

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

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.



662
663
664
665
# File 'lib/ic_agent/candid.rb', line 662

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

Instance Method Details

#_build_type_table_impl(type_table) ⇒ Object



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

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



667
668
669
# File 'lib/ic_agent/candid.rb', line 667

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

#decode_value(b, t) ⇒ Object



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

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



702
703
704
# File 'lib/ic_agent/candid.rb', line 702

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

#encode_value(val) ⇒ Object



671
672
673
674
675
# File 'lib/ic_agent/candid.rb', line 671

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



698
699
700
# File 'lib/ic_agent/candid.rb', line 698

def id
  TypeIds::Vec
end

#nameObject



694
695
696
# File 'lib/ic_agent/candid.rb', line 694

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