module LocalPac class PacManager private attr_reader :cache, :null_file, :paths, :creator public def initialize(paths = default_paths, creator = PacFile) @paths = Array(paths) @creator = creator @cache = ActiveSupport::Cache::MemoryStore.new @null_file = proc { NullPacFile.new } end def find(name) name = File.basename(name, '.pac').to_sym cache.fetch(name) do pac_files.find(null_file) { |f| f.name == name } end end private def pac_files paths.reduce([]) do |memo, path| memo.concat Dir.glob(File.join(path, '*.pac')).collect { |f| creator.new(f) } end end def default_paths [ File.expand_path(File.join(ENV['HOME'], '.config', 'pacfiles')), File.expand_path(File.join(ENV['HOME'], '.pacfiles')), File.expand_path('../../../files', __FILE__), ] end end end