Module: NormalizeIt::BaseClassMethods

Defined in:
lib/normalize_it.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_normalized_attributesObject (readonly)

Returns the value of attribute _normalized_attributes.



130
131
132
# File 'lib/normalize_it.rb', line 130

def _normalized_attributes
  @_normalized_attributes
end

Instance Method Details

#handle_normalized_objectsObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/normalize_it.rb', line 148

def handle_normalized_objects
  self.transaction do
    @_normalized_attributes.each do |klass, attribs|
      existing_obj = klass.to_s.camelize.constantize.where( attribs ).first
      if existing_obj
        self.send("__#{klass.to_s}=", existing_obj)
      end
    end
  end
end

#initialize(attributes = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/normalize_it.rb', line 132

def initialize(attributes = {})
  @_normalized_attributes = {}
  self.class._normalized_models.each do |model_hash|
    @_normalized_attributes[model_hash[:class_name].underscore.to_sym] ||= {}
    attributes.each do |key, value|
      if key.to_s.foreign_key == model_hash[:foreign_key]
        @_normalized_attributes[model_hash[:class_name].underscore.to_sym][key] = attributes.delete(key)
      end
    end
  end
  super
  @_normalized_attributes.each do |klass, attribs|
    self.send("__#{klass.to_s}=", klass.to_s.camelize.constantize.new(attribs))
  end
end