class Universa::Ref

A reference to any Java-object that can call its methods like usual methods:

key = umi.instantiate "PrivateKey", 2048 # this returns Ref
address = key.getPublicKey().getShortAddress().toString()

Notice that all methods called after key are java methods of PrivateKey, PublicKey and KeyAddress Java classes, whose references are created on-the-fly automatically (and will be reclaimed by GC on both ends soon).

Instances are unique

What means, if some calls will return the same Java object instance, it will be returned as the same {Ref} instance.

Reference equality (== and !=)

References are equal if they refer the same objects OR their are equals - the java.equals() is called to compare different referenced objects. Therefore, to compare two references use:

Reference lifespan

Each {Ref} has an allocated object in the remote side which is retained until explicetly freed by the proper UMI call. {Ref} class rakes care of it: when the ruby Ref instance is garbage collected, its remote counterpart will shortly receive drop command preventing memory leakage.

Arguments

you can use basic ruby objects as arguments: strings, numbers, arrays, hashes, and {Ref} instances too. Note that:

Return value

Will be deep-converted to corresponding ruby objects: hashes, arrays, sets, numbers, strings and {Ref} instances as need. It is, generally, inverse of converting arguments covered above.

Constants

LOCAL_METHODS

Public Class Methods

new(umi, ref) click to toggle source

Create new reference. Do not call it directly: the {UMI} instance will do it in a correct order. @param [UMI] umi instance to bind to @param [Hash] ref UMI reference structure

# File lib/universa/umi.rb, line 414
def initialize(umi, ref)
  @umi, @ref = umi, ref
  @id = ref.id
end

Public Instance Methods

==(other) click to toggle source

Checks that references are euqal: either both point to the same remote object or respective remote objects are reported equals by the remote +equals()+ call.

# File lib/universa/umi.rb, line 462
def ==(other)
  (other.is_a?(Ref) || other.is_a?(RemoteAdapter)) && other._umi == @umi &&
      (other._remote_id == @id || other.equals(self))
end
===(other) click to toggle source

Equal references. Both point to the same remote object. Note that it should never happen with {UMI} class as it do cache non-recycled references and share them between calls.

# File lib/universa/umi.rb, line 469
def ===(other)
  other.is_a?(Ref) && other._umi == @umi && other._remote_id == @id
end
_as_umi_arg(umi) click to toggle source

Internal use only. This allow Ref instance to be an argument to the remote call. Convert it to proper UMI structure.

# File lib/universa/umi.rb, line 450
def _as_umi_arg(umi)
  umi == @umi or raise InterchangeError
  @ref
end
_remote_class_name() click to toggle source

name of the remote class (provided by remote)

# File lib/universa/umi.rb, line 430
def _remote_class_name
  @ref.className
end
_remote_id() click to toggle source

@return [Object] remote object id. Could be of any type actually.

# File lib/universa/umi.rb, line 425
def _remote_id
  @id
end
_umi() click to toggle source

@return [UMI] interface that this reference is bound to (and created by)

# File lib/universa/umi.rb, line 420
def _umi
  @umi
end
inspect() click to toggle source

short data label for instance

# File lib/universa/umi.rb, line 456
def inspect
  "<UMI:Ref:#{@umi.__id__}:#{@ref.className}:#{@id}>"
end
method_missing(method_name, *args, &block) click to toggle source

Internal use only. Call remote method as needed. This is where all the magick comes from: it call remote method instead of the local one, exactly like it is local.

Calls superclass method
# File lib/universa/umi.rb, line 441
def method_missing(method_name, *args, &block)
  if method_name[0] == '_'
    super
  else
    @umi.invoke self, method_name, *args
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source

Internal use only. Allow processing remote commands as local calls

Calls superclass method
# File lib/universa/umi.rb, line 435
def respond_to_missing?(method_name, include_private = false)
  method_name[0] == '_' || LOCAL_METHODS.include?(method_name) ? super : true
end