module Denormalized::Core

Public Class Methods

included(base) click to toggle source
# File lib/denormalized/core.rb, line 5
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

assign_attributes(new_attributes) click to toggle source
Calls superclass method
# File lib/denormalized/core.rb, line 88
def assign_attributes(new_attributes)
  if contains_denormalized_attributes(new_attributes)
    gifted_attributes = extract_denormalized_attributes(new_attributes)

    denormalized_tables.each do |table|
      denormalized_subjects(
        table: table,
        where_attrs: extract_existing_denormalized_attributes(new_attributes)
      ).each { |obj| obj.assign_attributes(gifted_attributes) }
    end
  end

  super
end
contains_denormalized_attributes(attributes) click to toggle source
# File lib/denormalized/core.rb, line 25
def contains_denormalized_attributes(attributes)
  attributes.keys.any? do |name|
    self.class.denormalized_attribute?(name)
  end
end
denormalized_subject(table) click to toggle source
# File lib/denormalized/core.rb, line 9
def denormalized_subject(table)
  table.to_s.classify.constantize
end
denormalized_subjects(table:, where_attrs:) click to toggle source
# File lib/denormalized/core.rb, line 13
def denormalized_subjects(table:, where_attrs:)
  denormalized_subject(
    table
  ).where(
    where_attrs
  ).uniq
end
denormalized_tables() click to toggle source
# File lib/denormalized/core.rb, line 21
def denormalized_tables
  denormalized_configuration[:tables].uniq
end
extract_denormalized_attributes(new_attributes) click to toggle source
# File lib/denormalized/core.rb, line 39
def extract_denormalized_attributes(new_attributes)
  {}.tap do |extracted_attributes|
    (denormalized_configuration[:columns] & new_attributes.keys).each do |attribute|
      extracted_attributes[attribute] = new_attributes[attribute]
    end
  end
end
extract_existing_denormalized_attributes(new_attributes) click to toggle source
# File lib/denormalized/core.rb, line 31
def extract_existing_denormalized_attributes(new_attributes)
  {}.tap do |extracted_attributes|
    (denormalized_configuration[:columns] & new_attributes.keys).each do |attribute|
      extracted_attributes[attribute] = read_attribute(attribute)
    end
  end
end
update(attributes) click to toggle source
Calls superclass method
# File lib/denormalized/core.rb, line 73
def update(attributes)
  if contains_denormalized_attributes(attributes)
    gifted_attributes = extract_denormalized_attributes(attributes)

    denormalized_tables.each do |table|
      denormalized_subjects(
        table: table,
        where_attrs: extract_existing_denormalized_attributes(attributes)
      ).each { |obj| obj.update(gifted_attributes) }
    end
  end

  super
end
update_attribute(name, value) click to toggle source
Calls superclass method
# File lib/denormalized/core.rb, line 60
def update_attribute(name, value)
  if self.class.denormalized_attribute?(attr_name)
    denormalized_tables.each do |table|
      denormalized_subjects(
        table: table,
        where_attrs: { name => read_attribute(name) }
      ).each { |obj| obj.send(:update_attribute, name, value) }
    end
  end

  super
end
update_columns(attributes) click to toggle source
Calls superclass method
# File lib/denormalized/core.rb, line 103
def update_columns(attributes)
  if contains_denormalized_attributes(attributes)
    gifted_attributes = extract_denormalized_attributes(attributes)

    denormalized_tables.each do |table|
      denormalized_subjects(
        table: table,
        where_attrs: extract_existing_denormalized_attributes(attributes)
      ).each { |obj| obj.send(:update_columns, gifted_attributes) }
    end
  end

  super
end
write_attribute(attr_name, value) click to toggle source
Calls superclass method
# File lib/denormalized/core.rb, line 47
def write_attribute(attr_name, value)
  if self.class.denormalized_attribute?(attr_name)
    denormalized_tables.each do |table|
      denormalized_subjects(
        table: table,
        where_attrs: { attr_name => read_attribute(attr_name) }
      ).each { |obj| obj.send(:write_attribute, attr_name, value) }
    end
  end

  super
end