Class: IcAgent::Candid::OptClass

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) ⇒ OptClass

Returns a new instance of OptClass.



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

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

Instance Method Details

#_build_type_table_impl(type_table) ⇒ Object



725
726
727
728
729
730
# File 'lib/ic_agent/candid.rb', line 725

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

#covariant(x) ⇒ Object



713
714
715
# File 'lib/ic_agent/candid.rb', line 713

def covariant(x)
  x.is_a?(Array) && (x.empty? || (x.length == 1 && @type.covariant(x[0])))
end

#decode_value(b, t) ⇒ Object

Raises:



732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/ic_agent/candid.rb', line 732

def decode_value(b, t)
  opt = check_type(t)
  raise ValueError, 'Not an option type' unless opt.is_a?(OptClass)

  flag = IcAgent::Candid.safe_read_byte(b)
  if flag == '00'
    []
  elsif flag == '01'
    [@type.decode_value(b, opt.instance_variable_get(:@type))]
  else
    raise ValueError, 'Not an option value'
  end
end

#displayObject



754
755
756
# File 'lib/ic_agent/candid.rb', line 754

def display
  "opt (#{@type.display})"
end

#encode_value(val) ⇒ Object



717
718
719
720
721
722
723
# File 'lib/ic_agent/candid.rb', line 717

def encode_value(val)
  if val.empty?
    "\x00".b
  else
    "\x01".b + @type.encode_value(val[0])
  end
end

#idObject



750
751
752
# File 'lib/ic_agent/candid.rb', line 750

def id
  TypeIds::Opt
end

#nameObject



746
747
748
# File 'lib/ic_agent/candid.rb', line 746

def name
  "opt (#{@type.name})"
end