Module: ActiveRecordCompose::Inspectable

Extended by:
ActiveSupport::Concern
Includes:
Attributes
Included in:
Model
Defined in:
lib/active_record_compose/inspectable.rb

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

attribute_names, #attribute_names, #attributes, delegate_attribute

Class Method Details

.filter_attributesArray<Symbol>

Returns columns not to expose when invoking #inspect.

Returns:

  • (Array<Symbol>)

See Also:



64
65
66
67
68
69
70
# File 'lib/active_record_compose/inspectable.rb', line 64

def filter_attributes
  if @filter_attributes.nil?
    superclass.filter_attributes
  else
    @filter_attributes
  end
end

.filter_attributes=(value) ⇒ void

Specify columns not to expose when invoking #inspect.

Parameters:

  • value (Array<Symbol>)

See Also:



76
77
78
79
# File 'lib/active_record_compose/inspectable.rb', line 76

def filter_attributes=(value)
  @inspection_filter = nil
  @filter_attributes = value
end

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

It takes a PP and pretty prints that record.



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