Module: ActiveRecord::Embedded::DynamicAttributes

Defined in:
lib/active_record/embedded/dynamic_attributes.rb

Overview

Allow any attribute to be persisted to this embedded model.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, value = nil, *_arguments) ⇒ Object

Read and write attributes from the model when they are not explicitly defined on this object. For future method lookups, this will define a field on the object so that its methods will be cached on the embedded object.

Parameters:

  • method (Symbol)
    • Undefined method name

  • value (Object) (defaults to: nil)
    • (optional) Value to set

Returns:

  • (Object)

    value persisted to the model.


15
16
17
18
19
20
21
22
# File 'lib/active_record/embedded/dynamic_attributes.rb', line 15

def method_missing(method, value = nil, *_arguments)
  return super unless respond_to? method

  attribute = method.to_s.delete('=').to_sym

  self.class.field attribute, type: value.class
  self[attribute] = value
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ TrueClass

Respond to all missing methods.

Returns:

  • (TrueClass)

27
28
29
# File 'lib/active_record/embedded/dynamic_attributes.rb', line 27

def respond_to_missing?(method, include_private = false)
  method.to_s.end_with?('=') || super
end