Sha256: 4fa4e3d70ae5f2aedf74a48fccc340dc1d75e24fd897ff30dbbf9defd76053b7

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

module Resque
  class Pool
    class FileOrHashLoader
      def initialize(filename_or_hash=nil)
        case filename_or_hash
        when String, nil
          @filename = filename_or_hash
        when Hash
          @static_config = filename_or_hash.dup
        else
          raise "#{self.class} cannot be initialized with #{filename_or_hash.inspect}"
        end
      end

      def call(environment)
        @config ||= load_config_from_file(environment)
      end

      def reset!
        @config = nil
      end

      private

      def load_config_from_file(environment)
        if @static_config
          new_config = @static_config
        else
          filename = config_filename
          new_config = load_config filename
        end
        apply_environment new_config, environment
      end

      def apply_environment(config, environment)
        environment and config[environment] and config.merge!(config[environment])
        config.delete_if {|key, value| value.is_a? Hash }
      end

      def config_filename
        @filename || choose_config_file
      end

      def load_config(filename)
        return {} unless filename
        YAML.load(ERB.new(IO.read(filename)).result)
      end

      CONFIG_FILES = ["resque-pool.yml", "config/resque-pool.yml"]
      def choose_config_file
        if ENV["RESQUE_POOL_CONFIG"]
          ENV["RESQUE_POOL_CONFIG"]
        else
          CONFIG_FILES.detect { |f| File.exist?(f) }
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
heroku-resque-pool-0.0.3 lib/resque/pool/file_or_hash_loader.rb
heroku-resque-pool-0.0.2 lib/resque/pool/file_or_hash_loader.rb
heroku-resque-pool-0.0.1 lib/resque/pool/file_or_hash_loader.rb
heroku-resque-pool-0.0.0 lib/resque/pool/file_or_hash_loader.rb