class Vorpal::DbLoader

Handles loading of objects from the database.

@private

Public Class Methods

new(only_owned, db_driver) click to toggle source
# File lib/vorpal/db_loader.rb, line 9
def initialize(only_owned, db_driver)
  @only_owned = only_owned
  @db_driver = db_driver
end

Public Instance Methods

load_from_db(ids, config) click to toggle source
# File lib/vorpal/db_loader.rb, line 14
def load_from_db(ids, config)
  db_roots = @db_driver.load_by_unique_key(config.db_class, ids, "id")
  load_from_db_objects(db_roots, config)
end
load_from_db_objects(db_roots, config) click to toggle source
# File lib/vorpal/db_loader.rb, line 19
def load_from_db_objects(db_roots, config)
  @loaded_objects = LoadedObjects.new
  @loaded_objects.add(config, db_roots)
  @lookup_instructions = LookupInstructions.new
  explore_objects(config, db_roots)

  until @lookup_instructions.empty?
    lookup = @lookup_instructions.next_lookup
    newly_loaded_objects = lookup.load_all(@db_driver)
    unexplored_objects = @loaded_objects.add(lookup.config, newly_loaded_objects)
    explore_objects(lookup.config, unexplored_objects)
  end

  @loaded_objects
end

Private Instance Methods

explore_association?(association_config) click to toggle source
# File lib/vorpal/db_loader.rb, line 53
def explore_association?(association_config)
  !@only_owned || association_config.owned == true
end
explore_objects(config, objects_to_explore) click to toggle source
# File lib/vorpal/db_loader.rb, line 37
def explore_objects(config, objects_to_explore)
  objects_to_explore.each do |db_object|
    config.has_manys.each do |has_many_config|
      lookup_by_fk(db_object, has_many_config) if explore_association?(has_many_config)
    end

    config.has_ones.each do |has_one_config|
      lookup_by_fk(db_object, has_one_config) if explore_association?(has_one_config)
    end

    config.belongs_tos.each do |belongs_to_config|
      lookup_by_id(db_object, belongs_to_config) if explore_association?(belongs_to_config)
    end
  end
end
lookup_by_fk(db_object, has_some_config) click to toggle source
# File lib/vorpal/db_loader.rb, line 65
def lookup_by_fk(db_object, has_some_config)
  associated_class_config = has_some_config.associated_class_config
  fk_info = has_some_config.foreign_key_info
  fk_value = has_some_config.get_unique_key_value(db_object)
  @lookup_instructions.lookup_by_fk(associated_class_config, fk_info, fk_value)
end
lookup_by_id(db_object, belongs_to_config) click to toggle source
# File lib/vorpal/db_loader.rb, line 57
def lookup_by_id(db_object, belongs_to_config)
  associated_class_config = belongs_to_config.associated_class_config(db_object)
  unique_key_value = belongs_to_config.fk_value(db_object)
  unique_key_name = belongs_to_config.unique_key_name
  return if unique_key_value.nil? || @loaded_objects.already_loaded_by_unique_key?(associated_class_config, unique_key_name, unique_key_value)
  @lookup_instructions.lookup_by_unique_key(associated_class_config, unique_key_name, unique_key_value)
end