Class: TOISB::Wrapper
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#trace ⇒ Object
readonly
Returns the value of attribute trace.
Instance Method Summary collapse
-
#ancestor_klasses ⇒ Object
Collects only the ancestors which are Classes.
-
#ancestors ⇒ Object
Collects the ancestors of an object.
-
#get_id ⇒ Object
Gets the object ID of an object.
-
#initialize(object, trace = nil) ⇒ Wrapper
constructor
A new instance of Wrapper.
-
#inspect ⇒ Object
Calling #inspect will call it recursively on contained objects BasicObject does not define an #inspect instance method So it will raise an error if the user hasn’t defined one intentionally.
-
#inspector ⇒ Object
Checks for an inspect method and falls back to Superclass/Class:ID.
-
#klass ⇒ Object
Returns the class of the object if it isn’t already a class.
-
#safe_send(method, *args, &block) ⇒ Object
Works around BasicObject and other objects that are missing/overwrite important methods.
-
#safe_send_to(target, method, *args, &block) ⇒ Object
This is here so you can pass in an arbitrary object.
-
#singleton ⇒ Object
Obtain the singleton class of an object.
-
#superklass ⇒ Object
Returns the superclass of the object.
Constructor Details
#initialize(object, trace = nil) ⇒ Wrapper
Returns a new instance of Wrapper.
6 7 8 9 |
# File 'lib/toisb/wrapper.rb', line 6 def initialize object, trace = nil @object = object @trace = caller end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
10 11 12 |
# File 'lib/toisb/wrapper.rb', line 10 def object @object end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
10 11 12 |
# File 'lib/toisb/wrapper.rb', line 10 def trace @trace end |
Instance Method Details
#ancestor_klasses ⇒ Object
Collects only the ancestors which are Classes
31 32 33 |
# File 'lib/toisb/wrapper.rb', line 31 def ancestor_klasses @ancestor_klasses ||= ancestors.select {|a| Class === a } end |
#ancestors ⇒ Object
Collects the ancestors of an object
26 27 28 |
# File 'lib/toisb/wrapper.rb', line 26 def ancestors @ancestors ||= safe_send_to singleton, :ancestors end |
#get_id ⇒ Object
Gets the object ID of an object
46 47 48 |
# File 'lib/toisb/wrapper.rb', line 46 def get_id @object_id ||= object.__id__.to_s(16) rescue 0 end |
#inspect ⇒ Object
Calling #inspect will call it recursively on contained objects BasicObject does not define an #inspect instance method So it will raise an error if the user hasn’t defined one intentionally
63 64 65 66 67 |
# File 'lib/toisb/wrapper.rb', line 63 def inspect super rescue "#<#{self.class}:0x#{object_id} @object=#{inspector}>" end |
#inspector ⇒ Object
Checks for an inspect method and falls back to Superclass/Class:ID
13 14 15 16 17 18 |
# File 'lib/toisb/wrapper.rb', line 13 def inspector fallback = "#<#{superklass}/#{klass}:0x#{get_id}>" klass && klass.public_method_defined?(:inspect) ? object.inspect : fallback rescue fallback end |
#klass ⇒ Object
Returns the class of the object if it isn’t already a class
36 37 38 |
# File 'lib/toisb/wrapper.rb', line 36 def klass @klass ||= Module === object ? object : ancestor_klasses[1] end |
#safe_send(method, *args, &block) ⇒ Object
Works around BasicObject and other objects that are missing/overwrite important methods
51 52 53 |
# File 'lib/toisb/wrapper.rb', line 51 def safe_send method, *args, &block safe_send_to object, method, *args, &block end |
#safe_send_to(target, method, *args, &block) ⇒ Object
This is here so you can pass in an arbitrary object
56 57 58 |
# File 'lib/toisb/wrapper.rb', line 56 def safe_send_to target, method, *args, &block (Module === target ? Module : Object).instance_method(method).bind(target).call(*args, &block) end |
#singleton ⇒ Object
Obtain the singleton class of an object
21 22 23 |
# File 'lib/toisb/wrapper.rb', line 21 def singleton @singleton ||= (class << object; self; end) rescue object.class end |
#superklass ⇒ Object
Returns the superclass of the object
41 42 43 |
# File 'lib/toisb/wrapper.rb', line 41 def superklass ancestor_klasses[2] end |