module Is::Inspectable

public

Customized inspections for any object.

Public Class Methods

inspected_objects() click to toggle source
# File lib/is/inspectable.rb, line 95
def inspected_objects
  localized(:__corerb_inspected_objects) || localize(:__corerb_inspected_objects, {})
end
inspecting?(object) click to toggle source
# File lib/is/inspectable.rb, line 91
def inspecting?(object)
  inspected_objects[object.object_id]
end
prevent_recursion(object) { || ... } click to toggle source
# File lib/is/inspectable.rb, line 83
def prevent_recursion(object)
  object_id = object.object_id
  inspected_objects[object_id] = true
  yield
ensure
  inspected_objects.delete(object_id)
end

Public Instance Methods

inspect() click to toggle source
public

Inspects the object.

# File lib/is/inspectable.rb, line 42
def inspect
  inspection = +"#<#{self.class}:0x#{__id__.to_s(16)}"

  if Inspectable.inspecting?(self)
    "#{inspection} ...>"
  else
    Inspectable.prevent_recursion(self) do
      each_inspectable do |inspectable|
        value = if inspectable.start_with?("@")
          instance_variable_get(inspectable)
        else
          send(inspectable)
        end

        inspection << ", #{inspectable}=#{value.inspect}"
      end

      inspection.strip << ">"
    end
  end
end
inspects(*inspectables, without: []) click to toggle source
public

Sets one or more instance variables or instance methods as inspectable.

without - An array of instance variables or instance methods that should not be inspected.

# File lib/is/inspectable.rb, line 20
def inspects(*inspectables, without: [])
  if inspectables.any?
    mutate_state :__inspectables do |current_inspectables|
      current_inspectables.concat(inspectables.map(&:to_s)).uniq
    end
  end

  if without.any?
    mutate_state :__uninspectables do |current_uninspectables|
      current_uninspectables.concat(without.map(&:to_s)).uniq
    end
  end
end

Private Instance Methods

each_inspectable() { |inspectable| ... } click to toggle source
# File lib/is/inspectable.rb, line 64
        def each_inspectable
  inspectables = if __inspectables.any?
    __inspectables
  else
    instance_variables.map(&:to_s).reject { |instance_variable|
      instance_variable.start_with?("@_")
    }
  end

  inspectables.each do |inspectable|
    unless __uninspectables.include?(inspectable)
      yield inspectable
    end
  end
end