Module: Arx::Inspector

Included in:
Author, Category, Paper
Defined in:
lib/arx/inspector.rb

Overview

Restricts inspect to dump a whitelist of methods on an object. It will always provide ‘object_id` at a minimum.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(source) ⇒ Object

Defines helper inspector_fields instance variable & method, and inspector instance method on the target object.

Parameters:

  • source (Object)

    An arbitrary object (the object that includes the Inspector module).



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/arx/inspector.rb', line 30

def included(source)
  inspected << source
  source.class_eval do
    def self.inspector(*fields)
      @inspector_fields = *fields
    end

    def self.inspector_fields
      @inspector_fields ||= []
    end
  end
end

.inspectedObject

Returns the inspected instance variable, or sets it if undefined.



23
24
25
# File 'lib/arx/inspector.rb', line 23

def inspected
  @inspected ||= []
end

Instance Method Details

#inspectObject

Overwrites the object’s own inspect method.



10
11
12
13
14
15
16
17
18
19
# File 'lib/arx/inspector.rb', line 10

def inspect
  pairs = {}

  self.class.inspector_fields.each do |field|
    pairs[field] = self.send(field).inspect
  rescue
  end

  "#<#{self.class.name}:#{self.object_id} #{pairs.map {|k,v| "#{k}=#{v}"}.join(", ")}>"
end