Class: OrderTree::UniqueProxy

Inherits:
BasicObject
Defined in:
lib/order_tree/unique_proxy.rb

Overview

Simple Proxy for distinguishing between the insertions of two identical objects in an order tree. Assign a unique ID to any object passed through the proxy, so you can always find the same object, even if you move it around in the tree. It also enables you to tell the differen between two different insertions of the same singleton object.

Direct Known Subclasses

OrderTreeNode

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ UniqueProxy

Returns a new instance of UniqueProxy.

Parameters:

  • obj (Object)
    • the proxy target



35
36
37
38
39
# File 'lib/order_tree/unique_proxy.rb', line 35

def initialize obj
  @is_proxy = true
  @obj = obj
  @uuid ||= ::SecureRandom.uuid
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Dispatches methods calls to proxy target



80
81
82
# File 'lib/order_tree/unique_proxy.rb', line 80

def method_missing(method, *args, &block)
  @obj.__send__ method, *args, &block
end

Class Attribute Details

.verbose_inspectObject

Returns the value of attribute verbose_inspect.



31
32
33
# File 'lib/order_tree/unique_proxy.rb', line 31

def verbose_inspect
  @verbose_inspect
end

Instance Method Details

#!Object



85
86
87
# File 'lib/order_tree/unique_proxy.rb', line 85

def !
  !@obj
end

#!=(arg) ⇒ Object



96
97
98
# File 'lib/order_tree/unique_proxy.rb', line 96

def != arg
  @obj != arg
end

#==(arg) ⇒ Object



90
91
92
93
# File 'lib/order_tree/unique_proxy.rb', line 90

def == arg
  return true if @obj.nil? and arg.nil?
  @obj == arg
end

#deproxyObject



75
76
77
# File 'lib/order_tree/unique_proxy.rb', line 75

def deproxy
  @obj
end

#equal?(other) ⇒ Boolean

Is true only if the other object has the same unique_id as self

Returns:

  • (Boolean)


66
67
68
# File 'lib/order_tree/unique_proxy.rb', line 66

def equal? other
  (@uuid == other.unique_id) rescue false
end

#inspectString

Returns a string describing the proxy if UniqueProxy.verbose_inspect is not false otherwise calls #inspect on the proxied object.

Returns:

  • (String)

    a string describing the proxy if UniqueProxy.verbose_inspect is not false otherwise calls #inspect on the proxied object



48
49
50
51
52
53
54
# File 'lib/order_tree/unique_proxy.rb', line 48

def inspect
  if UniqueProxy.verbose_inspect
    "#<#{UniqueProxy}::#{@uuid} => #{@obj.inspect}>"
  else
    @obj.inspect
  end
end

#origObject

Returns the unproxied target.

Returns:

  • (Object)

    the unproxied target



71
72
73
# File 'lib/order_tree/unique_proxy.rb', line 71

def orig
  @obj
end

#to_sString

Returns a eval-able string to create a new proxy over this proxied object.

Returns:

  • (String)

    a eval-able string to create a new proxy over this proxied object



57
58
59
60
61
62
63
# File 'lib/order_tree/unique_proxy.rb', line 57

def to_s
  if UniqueProxy.verbose_inspect
    "proxy(#{@obj.to_s})"
  else
    @obj.to_s
  end
end

#unique_idString

Returns the unique ID of the proxy.

Returns:

  • (String)

    the unique ID of the proxy



42
43
44
# File 'lib/order_tree/unique_proxy.rb', line 42

def unique_id
  @uuid
end