Module: Funes::Inspection

Extended by:
ActiveSupport::Concern
Included in:
Event
Defined in:
lib/funes/inspection.rb

Overview

Provides a human-readable representation of your model instances through the inspect and pretty_print methods.

Ported from Rails PR #56521 (ActiveModel::Inspection). When that PR ships in a future Rails release, this module can be removed with no API change.

This module requires that the including class has an @attributes instance variable (as provided by ActiveModel::Attributes) and responds to attribute_names.

Configuration

The inspection output can be customized through two class attributes:

  • filter_attributes - An array of attribute names whose values should be masked in the output. Useful for sensitive data like passwords.

  • attributes_for_inspect - An array of attribute names to include in the output, or :all to show all attributes (default).

Example

class Order::Placed < Funes::Event
  attribute :total, :decimal
  attribute :customer_id, :string
  attribute :password, :string
end

event = Order::Placed.new(total: 99.99, customer_id: "cust-1", password: "secret")
event.inspect
# => "#<Order::Placed total: 99.99, customer_id: \"cust-1\", password: \"secret\">"

Order::Placed.filter_attributes = [:password]
event.inspect
# => "#<Order::Placed total: 99.99, customer_id: \"cust-1\", password: [FILTERED]>"

Order::Placed.attributes_for_inspect = [:total]
event.inspect
# => "#<Order::Placed total: 99.99>"

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attribute_for_inspect(attr_name) ⇒ Object

Returns a formatted string for the given attribute, suitable for use in inspect output.

Long strings are truncated, dates and times are formatted with to_fs(:inspect), and filtered attributes show [FILTERED].



129
130
131
132
133
# File 'lib/funes/inspection.rb', line 129

def attribute_for_inspect(attr_name)
  attr_name = attr_name.to_s
  value = @attributes.fetch_value(attr_name)
  format_for_inspect(attr_name, value)
end

#full_inspectObject

Returns all attributes of the model as a nicely formatted string, ignoring .attributes_for_inspect.



95
96
97
# File 'lib/funes/inspection.rb', line 95

def full_inspect
  inspect_with_attributes(all_attributes_for_inspect)
end

#inspectObject

Returns the attributes of the model as a nicely formatted string.



89
90
91
# File 'lib/funes/inspection.rb', line 89

def inspect
  inspect_with_attributes(attributes_for_inspect)
end

#pretty_print(pp) ⇒ Object

Takes a PP and prettily prints this model to it, allowing you to get a nice result from pp model when pp is required.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/funes/inspection.rb', line 101

def pretty_print(pp)
  return super if custom_inspect_method_defined?
  pp.object_address_group(self) do
    if @attributes
      attr_names = attributes_for_inspect.select { |name| @attributes.key?(name.to_s) }
      pp.seplist(attr_names, proc { pp.text "," }) do |attr_name|
        attr_name = attr_name.to_s
        pp.breakable " "
        pp.group(1) do
          pp.text attr_name
          pp.text ":"
          pp.breakable
          value = attribute_for_inspect(attr_name)
          pp.text value
        end
      end
    else
      pp.breakable " "
      pp.text "not initialized"
    end
  end
end