Class: ObjectInspector::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/object_inspector/inspector.rb

Overview

Organizes inspection of the associated #object via the passed in options and via an BaseFormatter instance.

Examples:

ObjectInspector::Inspector.inspect(
  self,
  identification: self.class.name,
  flags: "FLAG1",
  issues: "ISSUE1",
  info: "INFO",
  name: "NAME",
)
# => "<Object(FLAG1) !!ISSUE1!! INFO :: NAME>"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, scope: ObjectInspector.configuration.default_scope, formatter: ObjectInspector.configuration.formatter_class, **kwargs) ⇒ Inspector

Returns a new instance of Inspector.

Parameters:

  • object (Object)

    the object being inspected

  • scope (Symbol) (defaults to: ObjectInspector.configuration.default_scope)

    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
    
  • formatter (ObjectInspector::BaseFormatter) (defaults to: ObjectInspector.configuration.formatter_class)

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

  • kwargs (Hash)

    Options to be sent to #object via ObjectInspector::InterrogateObject when calling the ‘inspect_*` methods.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/object_inspector/inspector.rb', line 40

def initialize(
  object,
  scope: ObjectInspector.configuration.default_scope,
  formatter: ObjectInspector.configuration.formatter_class,
  **kwargs
)
  @object = object
  @scope = ObjectInspector::Conversions.Scope(scope)
  @formatter_class = formatter
  @kwargs = kwargs
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



19
20
21
# File 'lib/object_inspector/inspector.rb', line 19

def object
  @object
end

Class Method Details

.inspectString

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

Returns:

  • (String)


25
# File 'lib/object_inspector/inspector.rb', line 25

def self.inspect(...) = new(...).to_s

Instance Method Details

#flagsString, NilClass

Boolean flags/states applicable to #object.

Returns:

  • (String)

    If given.

  • (NilClass)

    If not given.



87
88
89
# File 'lib/object_inspector/inspector.rb', line 87

def flags
  value(key: :flags)
end

#identificationString

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

Returns:

  • (String)


79
80
81
# File 'lib/object_inspector/inspector.rb', line 79

def identification
  (value(key: :identification) || object.class).to_s
end

#infoString, NilClass

Informational details applicable to #object.

Returns:

  • (String)

    If given.

  • (NilClass)

    If not given.



103
104
105
# File 'lib/object_inspector/inspector.rb', line 103

def info
  value(key: :info)
end

#issuesString, NilClass

Issues/Warnings applicable to #object.

Returns:

  • (String)

    If given.

  • (NilClass)

    If not given.



95
96
97
# File 'lib/object_inspector/inspector.rb', line 95

def issues
  value(key: :issues)
end

#nameString, NilClass

A human-friendly identifier for #object.

Returns:

  • (String)

    If given.

  • (NilClass)

    If not given.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/object_inspector/inspector.rb', line 111

def name
  key = :name

  if @kwargs.key?(key)
    value(key:)
  else
    interrogate_object_inspect_method(key) ||
      interrogate_object(
        method_name: :display_name,
        kwargs: object_method_keyword_arguments,
      )
  end
end

#to_sString

Generate the formatted inspect String.

Returns:

  • (String)


55
56
57
# File 'lib/object_inspector/inspector.rb', line 55

def to_s
  formatter.call
end

#wrapped_object_inspection_resultString, NilClass

Generate the inspect String for the wrapped object, if ‘self` is a wrapper object.

Returns:

  • (String)

    If #object_is_a_wrapper?.

  • (NilClass)

    If not #object_is_a_wrapper?.



64
65
66
67
68
69
70
71
72
73
# File 'lib/object_inspector/inspector.rb', line 64

def wrapped_object_inspection_result
  return unless object_is_a_wrapper?

  self.class.inspect(
    extract_wrapped_object,
    scope: @scope,
    formatter: @formatter_class,
    kwargs: @kwargs,
  )
end