Module: ActiveRecordCompose::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Attributes
Included in:
Inspectable, Model
Defined in:
lib/active_record_compose/attributes.rb,
lib/active_record_compose/attributes/querying.rb,
lib/active_record_compose/attributes/delegation.rb,
lib/active_record_compose/attributes/attribute_predicate.rb,
sig/_internal/package_private.rbs

Overview

Provides attribute-related functionality for use within ActiveRecordCompose::Model.

This module allows you to define attributes on your composed model, including support for query methods (e.g., #attribute?) and delegation of attributes to underlying ActiveRecord instances via macros.

For example, .delegate_attribute defines attribute accessors that delegate to a specific model, similar to:

delegate :name, :name=, to: :account

Additionally, delegated attributes are included in the composed model's #attributes hash.

Examples:

class AccountRegistration < ActiveRecordCompose::Model
  def initialize(, attributes = {})
    @account = 
    super(attributes)
    models.push()
  end

  attribute :original_attribute, :string, default: "qux"
  delegate_attribute :name, to: :account

  private

  attr_reader :account
end

 = Account.new
.name = "foo"

registration = AccountRegistration.new()
registration.name         # => "foo" (delegated)
registration.name?        # => true  (delegated attribute method + `?`)

registration.name = "bar" # => updates account.name
.name              # => "bar"
.name?             # => true

registration.attributes
# => { "original_attribute" => "qux", "name" => "bar" }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delegate_attributeObject

Parameters:

  • methods (Object)
  • to: (Object)
  • allow_nil: (Boolean)

Returns:

  • (Object)


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

def self.delegate_attribute: (*untyped methods, to: untyped, ?allow_nil: bool) -> untyped

.delegated_attributesArray[Delegation]

Returns:

  • (Array[Delegation])


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

def self.delegated_attributes: () -> Array[Delegation]

.delegated_attributes=Object

Parameters:

  • (Array[Delegation])

Returns:

  • (Object)


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

def self.delegated_attributes=: (Array[Delegation]) -> untyped

Instance Method Details

#_require_attributes_initializedvoid

This method returns an undefined value.



174
175
176
177
178
179
180
181
# File 'lib/active_record_compose/attributes.rb', line 174

def _require_attributes_initialized
  unless @attributes
    raise ActiveRecordCompose::UninitializedAttribute,
          "No attributes have been set. Is proper initialization performed, such as calling `super` in `initialize`?"
  end

  yield
end

#_write_attributevoid (private)

steep:ignore



170
# File 'lib/active_record_compose/attributes.rb', line 170

def _write_attribute(...) = _require_attributes_initialized { super } # steep:ignore

#attributevoid (private)



172
# File 'lib/active_record_compose/attributes.rb', line 172

def attribute(...) = _require_attributes_initialized { super }

#attribute_namesArray<String>

Returns a array of attribute name. Attributes declared with delegate_attribute are also merged.

class Foo < ActiveRecordCompose::Base
  def initialize(attributes = {})
    @account = Account.new
    super
  end

  attribute :confirmation, :boolean, default: false   # plain attribute
  delegate_attribute :name, to: :account              # delegated attribute

  private

  attr_reader :account
end

Foo.attribute_names                                   # Returns the merged state of plain and delegated attributes
# => ["confirmation" ,"name"]

foo = Foo.new
foo.attribute_names                                   # Similar behavior for instance method version
# => ["confirmation", "name"]

Returns:

  • (Array<String>)

    array of attribute name.

See Also:



131
132
133
134
135
# File 'lib/active_record_compose/attributes.rb', line 131

def attribute_names
  _require_attributes_initialized do
    super + delegated_attributes.to_a.map { _1.attribute_name }
  end
end

#attributesHash<String, Object>

Returns a hash with the attribute name as key and the attribute value as value. Attributes declared with delegate_attribute are also merged.

class Foo < ActiveRecordCompose::Base
  def initialize(attributes = {})
    @account = Account.new
    super
  end

  attribute :confirmation, :boolean, default: false   # plain attribute
  delegate_attribute :name, to: :account              # delegated attribute

  private

  attr_reader :account
end

foo = Foo.new
foo.name = "Alice"
foo.confirmation = true

foo.attributes                                        # Returns the merged state of plain and delegated attributes
# => { "confirmation" => true, "name" => "Alice" }

Returns:

  • (Hash<String, Object>)

    hash with the attribute name as key and the attribute value as value.



162
163
164
165
166
# File 'lib/active_record_compose/attributes.rb', line 162

def attributes
  _require_attributes_initialized do
    super.merge(*delegated_attributes.to_a.map { _1.attribute_hash(self) })
  end
end

#delegated_attributesArray[Delegation]

Returns:

  • (Array[Delegation])


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

def delegated_attributes: () -> Array[Delegation]