Method: Fera::Base#initialize

Defined in:
lib/fera/models/base.rb

#initialize(attributes = nil, persisted = nil, options = {}) ⇒ Base

Returns a new instance of Base.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fera/models/base.rb', line 97

def initialize(attributes = nil, persisted = nil, options = {})
  @options = options.to_h

  dynamic_attributes = attributes.to_h.dup

  association_keys = self.class.has_manys.keys + self.class.has_ones.keys + self.class.belongs_tos.keys

  dynamic_attributes.except!(*(association_keys + association_keys.map(&:to_s)))

  super(dynamic_attributes, persisted)

  return unless attributes.present?

  association_keys.each do |name, _opts|
    if attributes.key?(name.to_s) || attributes.key?(name.to_sym)
      val = attributes.to_h[name.to_s] || attributes.to_h[name.to_sym]
      self.send("#{ name }=", val) if respond_to?("#{ name }=")
    end
  end
end