Sha256: 5d46542073a3ddd50ddadccc07514195982356953e3dc7d3d5f3a3569d134bc4

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

module Slaver
  class ConfigHandler
    include Singleton

    attr_reader :block, :saved_block, :saved_config, :current_config

    def run_with(klass, config_name, pools_handler)
      config_name = prepare(config_name)

      pools_handler.for_config(klass, config_name)

      with_config(config_name) { yield }
    end

    private

    def with_config(config_name)
      last_config = @current_config
      @current_config = config_name

      begin
        yield
      ensure
        @current_config = last_config
      end
    end

    def prepare(config_name)
      config_name = config_name.to_s

      return config_name if ::ActiveRecord::Base.configurations.key?(config_name)

      config_name = "#{Rails.env}_#{config_name}"

      unless ::ActiveRecord::Base.configurations.key?(config_name)
        if Rails.env.production?
          raise ArgumentError, "Can't find #{config_name} on database configurations"
        else
          config_name = Rails.env
        end
      end

      config_name
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slaver-0.2.3 lib/slaver/config_handler.rb
slaver-0.2.2 lib/slaver/config_handler.rb
slaver-0.2.0 lib/slaver/config_handler.rb
slaver-0.0.2 lib/slaver/config_handler.rb
slaver-0.0.1 lib/slaver/config_handler.rb