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.
-
#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.
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
27 28 29 |
# File 'lib/toisb/wrapper.rb', line 27 def ancestor_klasses @ancestor_klasses ||= ancestors.select {|a| Class === a } end |
#ancestors ⇒ Object
Collects the ancestors of an object
22 23 24 |
# File 'lib/toisb/wrapper.rb', line 22 def ancestors @ancestors ||= singleton.ancestors end |
#get_id ⇒ Object
Gets the object ID of an object
42 43 44 |
# File 'lib/toisb/wrapper.rb', line 42 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
60 61 62 63 64 |
# File 'lib/toisb/wrapper.rb', line 60 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 14 |
# File 'lib/toisb/wrapper.rb', line 9 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
32 33 34 |
# File 'lib/toisb/wrapper.rb', line 32 def klass @klass ||= singleton.superclass end |
#safe_send(method, *args, &block) ⇒ Object
Works around BasicObject and other objects that are missing/overwrite important methods
47 48 49 |
# File 'lib/toisb/wrapper.rb', line 47 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
52 53 54 55 |
# File 'lib/toisb/wrapper.rb', line 52 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 |
#singleton ⇒ Object
Obtain the singleton class of an object
17 18 19 |
# File 'lib/toisb/wrapper.rb', line 17 def singleton @singleton ||= (class << object; self; end) rescue object.class end |
#superklass ⇒ Object
Returns the superclass of the object
37 38 39 |
# File 'lib/toisb/wrapper.rb', line 37 def superklass klass.superclass end |