Skip to content

Commit

Permalink
Merge pull request #141 from joshuaflanagan/namespaces
Browse files Browse the repository at this point in the history
Move FileOrHashLoader into Resque::Pool namespace
  • Loading branch information
nevans committed Oct 26, 2015
2 parents 8ad098e + d8db0a3 commit 78e29cb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 53 deletions.
108 changes: 56 additions & 52 deletions lib/resque/pool/file_or_hash_loader.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
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) }
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
2 changes: 1 addition & 1 deletion spec/resque_pool_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ module Rails; end
subject { Resque::Pool.create_configured }

it "created pools use config file and hash loading logic" do
subject.config_loader.should be_instance_of FileOrHashLoader
subject.config_loader.should be_instance_of Resque::Pool::FileOrHashLoader
end
end

Expand Down

0 comments on commit 78e29cb

Please sign in to comment.