module NormalizeIt::BaseClassMethods

Attributes

_normalized_attributes[R]

Public Class Methods

new(attributes = {}) click to toggle source
Calls superclass method
# 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

Public Instance Methods

handle_normalized_objects() click to toggle source
# 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