Sha256: aca32677c2527a9badb6b7d4a149df4c99a29d430aa5c6a00273a2f40342d67e

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'yaml'

module Dbcp
  class DatabaseYamlEnvironmentProvider
    def initialize(database_yaml_path)
      @database_yaml_path = database_yaml_path
    end

    # @return [Environment, nil]
    def find(environment_name)
      begin
        environment_hash = read_file[environment_name]
        if environment_hash
          build_environment environment_name, environment_hash
        else
          nil
        end
      rescue Errno::ENOENT
        return nil
      end
    end

   private

    def read_file
      YAML.load_file @database_yaml_path
    end

    def build_environment(environment_name, environment_hash)
      execution_host = ExecutionHost.build(environment_hash)

      begin
        database = Database.build(environment_hash)
      rescue Database::BlankDatabaseDefinition => e
        if execution_host.remote?
          database = execution_host.remote_database @database_yaml_path, environment_name
        else
          raise e
        end
      end

      Environment.new({
        environment_name: environment_name,
        database:         database,
        execution_host:   execution_host
      })
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dbcp-0.2.1 lib/dbcp/environment_providers/database_yaml_environment_provider.rb