Class: TOISB::Wrapper
- Inherits:
-
Object
- Object
- TOISB::Wrapper
- Defined in:
- lib/toisb/wrapper.rb
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 and not Modules or singleton 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.
-
#inspector! ⇒ Object
This version doesn’t rescue when #inspect raises.
-
#klass ⇒ Object
Returns the class of the object.
-
#klassinfo ⇒ Object
show a nicely formated version of the class and its superclass displays it in the style of a path, with the superclass as the parent directory.
-
#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.
-
#simple_inspector ⇒ Object
Simple inspect-ish output with a low chance of failure.
-
#singleton ⇒ Object
Obtain the singleton class of an object.
-
#subklassinfo ⇒ Object
show a nicely formated version of the class and its superclass displays it in the same style as the class definition.
-
#superklass ⇒ Object
Returns the superclass of the object.
Constructor Details
#initialize(object, trace = nil) ⇒ Wrapper
Returns a new instance of Wrapper.
2 3 4 5 |
# File 'lib/toisb/wrapper.rb', line 2 def initialize object, trace = nil @object = object @trace = caller end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
6 7 8 |
# File 'lib/toisb/wrapper.rb', line 6 def object @object end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
6 7 8 |
# File 'lib/toisb/wrapper.rb', line 6 def trace @trace end |
Instance Method Details
#ancestor_klasses ⇒ Object
Collects only the ancestors which are Classes and not Modules or singleton classes
48 49 50 |
# File 'lib/toisb/wrapper.rb', line 48 def ancestor_klasses @ancestor_klasses ||= ancestors.select {|a| Class === a && !a.singleton_class? } end |
#ancestors ⇒ Object
Collects the ancestors of an object
43 44 45 |
# File 'lib/toisb/wrapper.rb', line 43 def ancestors @ancestors ||= singleton.ancestors end |
#get_id ⇒ Object
Gets the object ID of an object
63 64 65 |
# File 'lib/toisb/wrapper.rb', line 63 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
81 82 83 84 85 |
# File 'lib/toisb/wrapper.rb', line 81 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
9 10 11 12 13 |
# File 'lib/toisb/wrapper.rb', line 9 def inspector inspector! rescue simple_inspector end |
#inspector! ⇒ Object
This version doesn’t rescue when #inspect raises
16 17 18 |
# File 'lib/toisb/wrapper.rb', line 16 def inspector! klass && klass.public_method_defined?(:inspect) ? object.inspect : simple_inspector end |
#klass ⇒ Object
Returns the class of the object
53 54 55 |
# File 'lib/toisb/wrapper.rb', line 53 def klass @klass ||= ancestor_klasses[0] end |
#klassinfo ⇒ Object
show a nicely formated version of the class and its superclass displays it in the style of a path, with the superclass as the parent directory
27 28 29 |
# File 'lib/toisb/wrapper.rb', line 27 def klassinfo [superklass, klass].compact.map(&:name).join "/" end |
#safe_send(method, *args, &block) ⇒ Object
Works around BasicObject and other objects that are missing/overwrite important methods
68 69 70 |
# File 'lib/toisb/wrapper.rb', line 68 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
73 74 75 76 |
# File 'lib/toisb/wrapper.rb', line 73 def safe_send_to target, method, *args, &block raise NotImplementedError, "Bind functionality wasn't added until Ruby 2.0." if ::RUBY_VERSION < "2.0.0" (Module === target ? Module : Object).instance_method(method).bind(target).call(*args, &block) end |
#simple_inspector ⇒ Object
Simple inspect-ish output with a low chance of failure
21 22 23 |
# File 'lib/toisb/wrapper.rb', line 21 def simple_inspector "#<#{klassinfo}:0x#{get_id}>" end |
#singleton ⇒ Object
Obtain the singleton class of an object
38 39 40 |
# File 'lib/toisb/wrapper.rb', line 38 def singleton @singleton ||= (class << object; self; end) rescue object.class end |
#subklassinfo ⇒ Object
show a nicely formated version of the class and its superclass displays it in the same style as the class definition
33 34 35 |
# File 'lib/toisb/wrapper.rb', line 33 def subklassinfo [klass, superklass].compact.join " < " end |
#superklass ⇒ Object
Returns the superclass of the object
58 59 60 |
# File 'lib/toisb/wrapper.rb', line 58 def superklass @superklass ||= ancestor_klasses[1] end |