module LocalPac class PacManager private attr_reader :cache, :null_file, :paths, :creator, :content_handler, :validator public def initialize(paths = default_paths, creator = PacFile) @paths = Array(paths) @creator = creator @cache = ActiveSupport::Cache::MemoryStore.new @null_file = proc { NullPacFile.new } @content_handler = JavaScriptCompressor.new @validator = PacFileValidator.new end def find(name) name = File.basename(name, '.pac').to_sym cache.fetch(name) do file = valid_pac_files.find(null_file) { |f| f.name == name } file.prepare(content_handler) file 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 valid_pac_files files = [] pac_files.each do |f| files << f if validator.valid?(f) end files 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