Module: ActiveRecordCompose::DelegateAttribute::ClassMethods

Defined in:
lib/active_record_compose/delegate_attribute.rb

Instance Method Summary collapse

Instance Method Details

#attribute_namesArray<String>

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

Returns:

  • (Array<String>)

    array of attribute name.



78
# File 'lib/active_record_compose/delegate_attribute.rb', line 78

def attribute_names = super + delegated_attributes.to_a.map { _1.attribute }

#delegate_attribute(*attributes, to:, allow_nil: nil) ⇒ Object

Defines the reader and writer for the specified attribute.



63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record_compose/delegate_attribute.rb', line 63

def delegate_attribute(*attributes, to:, allow_nil: nil)
  delegations = attributes.map { Delegation.new(attribute: _1.to_s, to:, allow_nil:) }

  delegations.map do |delegation|
    delegate delegation.reader, delegation.writer, to: delegation.to, allow_nil: delegation.allow_nil
    define_attribute_methods delegation.attribute
  end

  self.delegated_attributes = (delegated_attributes.to_a + delegations).reverse.uniq { _1.attribute }.reverse
end