Class: DRab::DRabObject

Inherits:
Object
  • Object
show all
Defined in:
lib/drab/eq.rb,
lib/drab/gw.rb,
lib/drab/drab.rb

Overview

class DRabURIOption # :nodoc: I don’t understand the purpose of this class… # <– which is why it shouldn’t exist

  def initialize(option)
    @option = option.to_s
  end
  attr_reader :option
  def to_s; @option; end

  def ==(other)
    return false unless DRabURIOption === other
    @option == other.option
  end

  def hash
    @option.hash
  end

  alias eql? ==
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, uri = nil) ⇒ DRabObject

Returns a new instance of DRabObject.



945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/drab/drab.rb', line 945

def initialize(obj, uri=nil)
  @uri = nil
  @ref = nil
  if obj.nil?
    return if uri.nil?
    @uri, option = DRabProtocol.uri_option(uri, DRab.config)
    #@ref = DRabURIOption.new(option) unless option.nil?
    @ref = option.to_s unless option.nil?
  else
    @uri = uri ? uri : (DRab.uri rescue nil)
    @ref = obj ? DRab.to_id(obj) : nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg_id, *a, &b) ⇒ Object



981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/drab/drab.rb', line 981

def method_missing(msg_id, *a, &b)
  if DRab.here?(@uri)
    obj = DRab.to_obj(@ref)
    DRab.current_server.check_insecure_method(obj, msg_id)
    return obj.__send__(msg_id, *a, &b)
  end

  succ, result = self.class.with_friend(@uri) do
    DRabConn.open(@uri) do |conn|
      suc, res = conn.send_message(self, msg_id, a, b)
    end
  end

  if succ
    return result
  elsif DRabUnknown === result
    raise result
  else
    bt = self.class.prepare_backtrace(@uri, result)
    result.set_backtrace(bt + caller)
    raise result
  end
end

Class Method Details

._load(s) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/drab/gw.rb', line 74

def self._load(s)
  #uri, ref = Marshal::load(s)
  uri, ref = JSON::load(s)
  if DRab.uri == uri
    return ref ? DRab.to_obj(ref) : DRab.front
  end

  self.new_with(DRab.uri, [:DRabObject, uri, ref])
end

.new_with(uri, ref) ⇒ Object



929
930
931
932
933
934
# File 'lib/drab/drab.rb', line 929

def self.new_with(uri, ref)
  it = self.allocate
  it.instance_variable_set(:@uri, uri)
  it.instance_variable_set(:@ref, ref)
  it
end

.new_with_uri(uri) ⇒ Object



936
937
938
# File 'lib/drab/drab.rb', line 936

def self.new_with_uri(uri)
  self.new(nil, uri)
end

.prepare_backtrace(uri, result) ⇒ Object



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/drab/drab.rb', line 1016

def self.prepare_backtrace(uri, result)
  prefix = "(#{uri}) "
  bt = []
  result.backtrace.each do |x|
    break if /`__send__'$/ =~ x
    if /^\(druby:\/\// =~ x
      bt.push(x)
    else
      bt.push(prefix + x)
    end
  end
  bt
end

.with_friend(uri) ⇒ Object



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/drab/drab.rb', line 1005

def self.with_friend(uri)
  friend = DRab.fetch_server(uri)
  return yield() unless friend

  save = Thread.current['DRab']
  Thread.current['DRab'] = { 'server' => friend }
  return yield
ensure
  Thread.current['DRab'] = save if friend
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



4
5
6
7
# File 'lib/drab/eq.rb', line 4

def ==(other)
  return false unless DRabObject === other
 (@ref == other.__drabref) && (@uri == other.__draburi)
end

#__drabrefObject



963
964
965
# File 'lib/drab/drab.rb', line 963

def __drabref
  @ref
end

#__draburiObject



959
960
961
# File 'lib/drab/drab.rb', line 959

def __draburi
  @uri
end

#_dump(lv) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/drab/gw.rb', line 84

def _dump(lv)
  if DRab.uri == @uri
    if Array === @ref && @ref[0] == :DRabObject
      #Marshal::dump([@ref[1], @ref[2]])
      JSON::dump([@ref[1], @ref[2]])
    else
      #Marshal::dump([@uri, @ref]) # ??
      JSON::dump([@uri, @ref]) # ??
    end
  else
    #Marshal::dump([DRab.uri, [:DRabObject, @uri, @ref]])
    JSON::dump([DRab.uri, [:DRabObject, @uri, @ref]])
  end
end

#hashObject



9
10
11
# File 'lib/drab/eq.rb', line 9

def hash
  [@uri, @ref].hash
end

#pretty_print(q) ⇒ Object



1030
1031
1032
# File 'lib/drab/drab.rb', line 1030

def pretty_print(q)
  q.pp_object(self)
end

#pretty_print_cycle(q) ⇒ Object



1034
1035
1036
1037
1038
1039
# File 'lib/drab/drab.rb', line 1034

def pretty_print_cycle(q)
  q.object_address_group(self) {
    q.breakable
    q.text '...'
  }
end

#respond_to?(msg_id, priv = false) ⇒ Boolean

Returns:

  • (Boolean)


970
971
972
973
974
975
976
977
978
979
# File 'lib/drab/drab.rb', line 970

def respond_to?(msg_id, priv=false)
  case msg_id
  when :_dump
    true
  when :marshal_dump
    false
  else
    method_missing(:respond_to?, msg_id, priv)
  end
end