Module: ActiveRecordCompose::DelegateAttribute::ClassMethods

Defined in:
lib/active_record_compose/delegate_attribute.rb

Instance Method Summary collapse

Instance Method Details

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

Defines the reader and writer for the specified attribute.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record_compose/delegate_attribute.rb', line 48

def delegate_attribute(*attributes, to:, allow_nil: nil, private: nil)
  delegates = attributes.flat_map do |attribute|
    reader = attribute.to_s
    writer = "#{attribute}="

    [reader, writer]
  end

  delegate(*delegates, to:, allow_nil:, private:)
  self.delegated_attributes = delegated_attributes.to_a + attributes.map { _1.to_s }
end