Class: TOISB::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/toisb/wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/toisb/wrapper.rb', line 6

def object
  @object
end

#traceObject (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_klassesObject

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

#ancestorsObject

Collects the ancestors of an object



43
44
45
# File 'lib/toisb/wrapper.rb', line 43

def ancestors
  @ancestors ||= singleton.ancestors
end

#get_idObject

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

#inspectObject

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

#inspectorObject

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

#klassObject

Returns the class of the object



53
54
55
# File 'lib/toisb/wrapper.rb', line 53

def klass
  @klass ||= ancestor_klasses[0]
end

#klassinfoObject

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

Raises:

  • (NotImplementedError)


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_inspectorObject

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

#singletonObject

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

#subklassinfoObject

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

#superklassObject

Returns the superclass of the object



58
59
60
# File 'lib/toisb/wrapper.rb', line 58

def superklass
  @superklass ||= ancestor_klasses[1]
end