Module: SelectiveInspect

Defined in:
lib/selective_inspect.rb,
lib/selective_inspect/version.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.included(base_class) ⇒ Object

Add to classes that include this module some convenient class mathods to blacklist or whitelist methods by default.



74
75
76
77
78
79
80
# File 'lib/selective_inspect.rb', line 74

def self.included(base_class)
  base_class.class_eval do
    alias_method :default_inspect, :inspect
  end
  base_class.extend ClassMethods
  base_class.include InstanceMethods
end

.perform_inspect(target, *whitelist) ⇒ Object

Public: Inspects the given object in a customizable way.

target - The Object to be inspected. whitelist - The names of the instance variables to output.

Examples

SelectiveInspect.inspect(player, :id, :name)
# =>

Returns the String that describes the objects and its internals.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/selective_inspect.rb', line 49

def self.perform_inspect(target, *whitelist)
  klass = target.class
  return target.default_inspect if !klass.include?(self) && whitelist.size == 0


  whitelist = klass.get_inspectionable_vars.map { |name| '@' + name.to_s }
  if whitelist.size == 0
    whitelist = target.instance_variables - klass.get_uninspectionable_vars.map { |name| '@' + name.to_s }
  end

  fields = whitelist.map do |var_name|
    var_content = target.instance_variable_get(var_name)
    "#{var_name}=#{var_content.inspect}"
  end

  string = "#<#{klass.name}:0x#{target.object_id} "
  string + fields.join(", ") + ">"
end