Module: ActiveRecordCompose::Inspectable

Extended by:
ActiveSupport::Concern
Includes:
Attributes
Included in:
Model
Defined in:
lib/active_record_compose/inspectable.rb,
sig/_internal/package_private.rbs

Overview

It provides #inspect behavior. It tries to replicate the inspect format provided by ActiveRecord as closely as possible.

Examples:

class Model < ActiveRecordCompose::Model
  def initialize(ar_model)
    @ar_model = ar_model
    super
  end

  attribute :foo, :date, default: -> { Date.today }
  delegate_attribute :bar, to: :ar_model

  private attr_reader :ar_model
end

m = Model.new(ar_model)
m.inspect  #=> #<Model:0x00007ff0fe75fe58 foo: "2025-11-14", bar: "bar">
class Model < ActiveRecordCompose::Model
  self.filter_attributes += %i[foo]

  # ...
end

m = Model.new(ar_model)
m.inspect  #=> #<Model:0x00007ff0fe75fe58 foo: [FILTERED], bar: "bar">

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

#_require_attributes_initialized, #_write_attribute, #attribute, #attribute_names, #attributes, delegate_attribute, delegated_attributes, #delegated_attributes, delegated_attributes=

Class Method Details

.filter_attributesArray[untyped]

Returns:

  • (Array[untyped])


94
# File 'sig/_internal/package_private.rbs', line 94

def self.filter_attributes: () -> Array[untyped]

.filter_attributes=void

This method returns an undefined value.

Parameters:

  • (Array[untyped])


95
# File 'sig/_internal/package_private.rbs', line 95

def self.filter_attributes=: (Array[untyped]) -> void

.inspection_filterActiveSupport::ParameterFilter

Returns:

  • (ActiveSupport::ParameterFilter)


93
# File 'sig/_internal/package_private.rbs', line 93

def self.inspection_filter: () -> ActiveSupport::ParameterFilter

Instance Method Details

#inspectString

Returns a formatted string representation of the record's attributes. It tries to replicate the inspect format provided by ActiveRecord as closely as possible.

Examples:

class Model < ActiveRecordCompose::Model
  def initialize(ar_model)
    @ar_model = ar_model
    super
  end

  attribute :foo, :date, default: -> { Date.today }
  delegate_attribute :bar, to: :ar_model

  private attr_reader :ar_model
end

m = Model.new(ar_model)
m.inspect  #=> #<Model:0x00007ff0fe75fe58 foo: "2025-11-14", bar: "bar">
class Model < ActiveRecordCompose::Model
  self.filter_attributes += %i[foo]

  # ...
end

m = Model.new(ar_model)
m.inspect  #=> #<Model:0x00007ff0fe75fe58 foo: [FILTERED], bar: "bar">

Returns:

  • (String)

    formatted string representation of the record's attributes.

See Also:



135
136
137
138
139
140
141
142
143
144
# File 'lib/active_record_compose/inspectable.rb', line 135

def inspect
  inspection =
    if @attributes
      attributes.map { |k, v| "#{k}: #{format_for_inspect(k, v)}" }.join(", ")
    else
      "not initialized"
    end

  "#<#{self.class} #{inspection}>"
end

#pretty_print(pp) ⇒ void

This method returns an undefined value.

It takes a PP and pretty prints that record.

Parameters:

  • q (Object)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/active_record_compose/inspectable.rb', line 148

def pretty_print(pp)
  pp.object_address_group(self) do
    if @attributes
      attrs = attributes
      pp.seplist(attrs.keys, proc { pp.text "," }) do |attr|
        pp.breakable " "
        pp.group(1) do
          pp.text attr
          pp.text ":"
          pp.breakable
          pp.text format_for_inspect(attr, attrs[attr])
        end
      end
    else
      pp.breakable " "
      pp.text "not initialized"
    end
  end
end