Sha256: 3876c54f8eb5561ae0b3f2317dae58e2aa7c62daf6a7594c13539234870ea386

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
local_pac-0.0.7 lib/local_pac/pac_manager.rb