class ObjectInspector::Inspector

ObjectInspector::Inspector organizes inspection of the associated {@object} via the passed in options and via a {ObjectInspector::BaseFormatter} instance.

Attributes

object[R]

Public Class Methods

inspect(object, **kargs) click to toggle source

Shortcuts the instantiation -> {#to_s} flow that would normally be required to use ObjectInspector::Inspector.

@return [String]

# File lib/object_inspector/inspector.rb, line 14
def self.inspect(object, **kargs)
  new(object, **kargs).to_s
end
new( object, scope: ObjectInspector.configuration.default_scope, formatter: ObjectInspector.configuration.formatter_class, **kargs) click to toggle source

@param object [Object] the object being inspected @param scope [Symbol] Object inspection type. For example:

:self (default) -- Means: Only interrogate self. Don't visit neighbors.
<custom>        -- Anything else that makes sense for {@object} to key
                   on

@param formatter [ObjectInspector::BaseFormatter]

(ObjectInspector.configuration.formatter) the formatter object type
to use for formatting the inspect String

@param kargs [Hash] options to be sent to {@object} via the

{ObjectInspector::ObjectInterrogator} when calling the `inspect_*`
methods
# File lib/object_inspector/inspector.rb, line 29
def initialize(
      object,
      scope: ObjectInspector.configuration.default_scope,
      formatter: ObjectInspector.configuration.formatter_class,
      **kargs)
  @object = object
  @scope = Conversions.Scope(scope)
  @formatter_klass = formatter
  @kargs = kargs
end

Public Instance Methods

flags() click to toggle source

Boolean flags/states applicable to {@object}.

@return [String] if given @return [NilClass] if not given

# File lib/object_inspector/inspector.rb, line 73
def flags
  value(key: :flags)
end
identification() click to toggle source

Core object identification details, such as the {@object} class name and any core-level attributes.

@return [String]

# File lib/object_inspector/inspector.rb, line 65
def identification
  (value(key: :identification) || @object.class).to_s
end
info() click to toggle source

Informational details applicable to {@object}.

@return [String] if given @return [NilClass] if not given

# File lib/object_inspector/inspector.rb, line 89
def info
  value(key: :info)
end
issues() click to toggle source

Issues/Warnings applicable to {@object}.

@return [String] if given @return [NilClass] if not given

# File lib/object_inspector/inspector.rb, line 81
def issues
  value(key: :issues)
end
name() click to toggle source

A human-friendly identifier for {@object}.

@return [String] if given @return [NilClass] if not given

# File lib/object_inspector/inspector.rb, line 97
def name
  key = :name

  if @kargs.key?(key)
    value(key: key)
  else
    interrogate_object_inspect_method(key) ||
      interrogate_object(method_name: :display_name,
                         kargs: object_method_keyword_arguments)
  end
end
to_s() click to toggle source

Generate the formatted inspect String.

@return [String]

# File lib/object_inspector/inspector.rb, line 43
def to_s
  formatter.call
end
wrapped_object_inspection_result() click to toggle source

Generate the inspect String for the wrapped object, if present.

@return [String] if {#object_is_a_wrapper} @return [NilClass] if not {#object_is_a_wrapper}

# File lib/object_inspector/inspector.rb, line 51
def wrapped_object_inspection_result
  return unless object_is_a_wrapper?

  self.class.inspect(
    extract_wrapped_object,
    scope: @scope,
    formatter: @formatter_klass,
    kargs: @kargs)
end

Private Instance Methods

evaluate_passed_in_value(value) click to toggle source

Call `value` on {@object} if it responds to it and the result is not nil, else just return `value`.

@return [#to_s] if {@object} responds to `value` and if the call result

isn't nil

@return [#nil] if {@object} doesn't respond to `value` or if the call

result is nil
# File lib/object_inspector/inspector.rb, line 136
def evaluate_passed_in_value(value)
  if value.is_a?(Symbol)
    interrogate_object(method_name: value) || value
  else
    value
  end
end
extract_wrapped_object() click to toggle source
# File lib/object_inspector/inspector.rb, line 179
def extract_wrapped_object
  @object.to_model
end
formatter() click to toggle source
# File lib/object_inspector/inspector.rb, line 111
def formatter
  @formatter_klass.new(self)
end
interrogate_object(method_name:, kargs: {}) click to toggle source
# File lib/object_inspector/inspector.rb, line 157
def interrogate_object(method_name:, kargs: {})
  interrogator =
    ObjectInterrogator.new(
      object: @object,
      method_name: method_name,
      kargs: kargs)

  interrogator.call
end
interrogate_object_inspect_method( name, prefix: ObjectInspector.configuration.inspect_method_prefix) click to toggle source

Attempt to call `inspect_*` on {@object} based on the passed in `name`.

@return [String] if {@object} responds to

`#{object_inspet_method_name}` (e.g. `inspect_flags`)

@return [NilClass] if not found on {@object}

# File lib/object_inspector/inspector.rb, line 149
def interrogate_object_inspect_method(
      name,
      prefix: ObjectInspector.configuration.inspect_method_prefix)
  interrogate_object(
    method_name: object_inspet_method_name(name, prefix: prefix),
    kargs: object_method_keyword_arguments)
end
object_inspet_method_name( name, prefix: ObjectInspector.configuration.inspect_method_prefix) click to toggle source
# File lib/object_inspector/inspector.rb, line 167
def object_inspet_method_name(
      name,
      prefix: ObjectInspector.configuration.inspect_method_prefix)
  "#{prefix}_#{name}"
end
object_is_a_wrapper?() click to toggle source
# File lib/object_inspector/inspector.rb, line 183
def object_is_a_wrapper?
  @object.respond_to?(:to_model) &&
    @object.to_model != @object
end
object_method_keyword_arguments() click to toggle source
# File lib/object_inspector/inspector.rb, line 173
def object_method_keyword_arguments
  {
    scope: @scope
  }
end
value(key:) click to toggle source

@return [String] if `key` is found in {#kargs} or if {@object} responds to

`#{object_inspet_method_name}` (e.g. `inspect_flags`)

@return [NilClass] if not found in {#kargs} or {@object}

# File lib/object_inspector/inspector.rb, line 118
def value(key:)
  return_value =
    if @kargs.key?(key)
      evaluate_passed_in_value(@kargs[key])
    else
      interrogate_object_inspect_method(key)
    end

  return_value&.to_s
end