Class: TOISB::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Helper
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.



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

def initialize object, trace = nil
  @object = object
  @trace = caller
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



10
11
12
# File 'lib/toisb/wrapper.rb', line 10

def object
  @object
end

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

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

#ancestorsObject

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_idObject

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

#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



63
64
65
66
67
# File 'lib/toisb/wrapper.rb', line 63

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



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

#klassObject

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

#singletonObject

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

#superklassObject

Returns the superclass of the object



41
42
43
# File 'lib/toisb/wrapper.rb', line 41

def superklass
  ancestor_klasses[2]
end