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
Class Attribute Summary collapse
-
.verbose_inspect ⇒ Object
Returns the value of attribute verbose_inspect.
Instance Method Summary collapse
- #! ⇒ Object
- #!=(arg) ⇒ Object
- #==(arg) ⇒ Object
-
#equal?(other) ⇒ Boolean
Is true only if the other object has the same unique_id as self.
-
#initialize(obj) ⇒ UniqueProxy
constructor
A new instance of UniqueProxy.
-
#inspect ⇒ String
A string describing the proxy if UniqueProxy.verbose_inspect is not false otherwise calls #inspect on the proxied object.
-
#method_missing(method, *args, &block) ⇒ Object
Dispatches methods calls to proxy target.
-
#orig ⇒ Object
The unproxied target.
-
#to_s ⇒ String
A eval-able string to create a new proxy over this proxied object.
-
#unique_id ⇒ String
The unique ID of the proxy.
Constructor Details
#initialize(obj) ⇒ UniqueProxy
Returns a new instance of UniqueProxy.
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
76 77 78 |
# File 'lib/order_tree/unique_proxy.rb', line 76 def method_missing(method, *args, &block) @obj.__send__ method, *args, &block end |
Class Attribute Details
.verbose_inspect ⇒ Object
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
81 82 83 |
# File 'lib/order_tree/unique_proxy.rb', line 81 def ! !@obj end |
#!=(arg) ⇒ Object
91 92 93 |
# File 'lib/order_tree/unique_proxy.rb', line 91 def != arg @obj != arg end |
#==(arg) ⇒ Object
86 87 88 |
# File 'lib/order_tree/unique_proxy.rb', line 86 def == arg @obj == arg end |
#equal?(other) ⇒ Boolean
Is true only if the other object has the same unique_id as self
66 67 68 |
# File 'lib/order_tree/unique_proxy.rb', line 66 def equal? other (@uuid == other.unique_id) rescue false end |
#inspect ⇒ String
Returns 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 |
#orig ⇒ Object
Returns the unproxied target.
71 72 73 |
# File 'lib/order_tree/unique_proxy.rb', line 71 def orig @obj end |
#to_s ⇒ String
Returns 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_id ⇒ String
Returns the unique ID of the proxy.
42 43 44 |
# File 'lib/order_tree/unique_proxy.rb', line 42 def unique_id @uuid end |