class IcAgent::Candid::OptClass

Represents an IDL Option

Public Class Methods

new(_type) click to toggle source
Calls superclass method IcAgent::Candid::ConstructType::new
# File lib/ic_agent/candid.rb, line 722
def initialize(_type)
  super()
  @type = _type
end

Public Instance Methods

_build_type_table_impl(type_table) click to toggle source
# File lib/ic_agent/candid.rb, line 739
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) click to toggle source
# File lib/ic_agent/candid.rb, line 727
def covariant(x)
  x.is_a?(Array) && (x.empty? || (x.length == 1 && @type.covariant(x[0])))
end
decode_value(b, t) click to toggle source
# File lib/ic_agent/candid.rb, line 746
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
display() click to toggle source
# File lib/ic_agent/candid.rb, line 768
def display
  "opt (#{@type.display})"
end
encode_value(val) click to toggle source
# File lib/ic_agent/candid.rb, line 731
def encode_value(val)
  if val.empty?
    "\x00".b
  else
    "\x01".b + @type.encode_value(val[0])
  end
end
id() click to toggle source
# File lib/ic_agent/candid.rb, line 764
def id
  TypeIds::Opt
end
name() click to toggle source
# File lib/ic_agent/candid.rb, line 760
def name
  "opt (#{@type.name})"
end