Skip to content

Commit

Permalink
Slightly more flexible search for ehcache.yml file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoegele committed Sep 24, 2010
1 parent d1390a4 commit fdac3b8
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions lib/ehcache/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

module Ehcache

class MissingConfigurationException < StandardError
def initialize(search_dirs)
super("Could not find Ehcache configuration file in any of: #{search_dirs.inspect}")
end
end

# default configuration and a cache named "cache"
# manager = Ehcache::CacheManager.new
# cache = manager.cache
Expand All @@ -11,26 +17,38 @@ module Ehcache
# manager = Ehcache::CacheManager.new({"cache" => {"name" => "steve"}})
# cache = manager.cache("steve")
class Config
CONFIGURATION_FILE = EHCACHE_HOME + "/config/ehcache.yml"
CONFIG_FILE_NAME = 'ehcache.yml'

# rails specific configuration
if defined?(RAILS_ROOT)
if File.exists?(RAILS_ROOT + "/config/ehcache.yml")
CONFIGURATION_FILE = (RAILS_ROOT + "/config/ehcache.yml")
end
end
RAILS_CONFIG_DIR =
if defined?(::Rails)
File.join(::Rails.root.to_s, 'config')
elsif defined?(RAILS_ROOT)
File.join(RAILS_ROOT, 'config')
end

SEARCH_DIRS = [RAILS_CONFIG_DIR,
File.join(ENV['HOME'], 'lib', 'config'),
File.join(EHCACHE_HOME, 'config')].compact

class << self
def generate(options={})
unless File.exists?(CONFIGURATION_FILE)
raise CONFIGURATION_FILE + " must exist"
unless config_file = find_config_file
raise MissingConfigurationException.new(SEARCH_DIRS)
end
config = ERB.new(File.open(CONFIGURATION_FILE) {|f| f.read})
config = ERB.new(File.open(config_file) {|f| f.read})
config = YAML.load(config.result(binding))
initialize_factory_proxies
process(config, options)
end

private

def find_config_file
SEARCH_DIRS.map {|dir| File.join(dir, CONFIG_FILE_NAME)}.find { |f|
File.readable?(f)
}
end

def process(config, options)
# merge in new defaults if any
config["default"].deep_merge!(options["default"]) if options["default"]
Expand Down

0 comments on commit fdac3b8

Please sign in to comment.