Sha256: f7f221cbab10f6f977662b825799810f7f08ab2ce8f0d54af4b7ae54e2adb3d4

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

##
# Manages process to create objects
#
# Usage:
#
#   Manager.new(config, logger).load_files(:before).create_objects
#
# or
#
#   Manager.new(config, logger).load_files(:after).create_objects
class PgObjects::Manager
  include Import['db_object_factory', 'config', 'logger']

  ##
  # event: +:before+ or +:after+
  #
  # used to reference configuration settings +before_path+ and +after_path+
  def load_files(event)
    validate_workability

    dir = config.send "#{event}_path"
    Dir[File.join(dir, '**', "*.{#{config.extensions.join(',')}}")].each do |path|
      objects << db_object_factory.create_instance(path)
    end

    self
  end

  def create_objects
    objects.each { create_object(_1) }
  end

  def objects
    @objects ||= []
  end

  private

  def validate_workability
    raise PgObjects::UnsupportedAdapterError if ActiveRecord::Base.connection.adapter_name != 'PostgreSQL'
  end

  def create_object(obj)
    return if obj.status == :done
    raise PgObjects::CyclicDependencyError, obj.name if obj.status == :processing

    obj.status = :processing

    create_dependencies(obj.dependencies)

    logger.write("creating #{obj.name}")
    ActiveRecord::Base.connection.execute(obj.sql_query)

    obj.status = :done
  end

  def create_dependencies(dependencies)
    dependencies.each { |dep_name| create_object(find_object(dep_name)) }
  end

  def find_object(dep_name)
    result = objects.select { |obj| [obj.name, obj.full_name, obj.object_name].compact.include? dep_name }

    raise PgObjects::AmbiguousDependencyError, dep_name if result.size > 1
    raise PgObjects::DependencyNotExistError, dep_name if result.empty?

    result[0]
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pg_objects-1.4.0 lib/pg_objects/manager.rb
pg_objects-1.3.3 lib/pg_objects/manager.rb
pg_objects-1.3.2 lib/pg_objects/manager.rb
pg_objects-1.3.1 lib/pg_objects/manager.rb
pg_objects-1.3.0 lib/pg_objects/manager.rb
pg_objects-1.2.2 lib/pg_objects/manager.rb
pg_objects-1.2.1 lib/pg_objects/manager.rb