Sha256: 3d515f83d5104da4b826e3061697c4a8d4d545bf1e5375f0e94df45a0a1b12fd

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8
module LocalPac
  class GitStorage
    private

    attr_reader :storage_path, :data, :repository, :file_creator, :null_file

    public

    def initialize(storage_path, compressor_engine = JavaScriptCompressor, file_creator = LocalPac::File, null_file = LocalPac::NullFile.new)
      @storage_path = ::File.expand_path(storage_path)
      @file_creator = file_creator
      @null_file    = null_file

      begin
        @repository = Rugged::Repository.new(storage_path)
      rescue Rugged::RepositoryError
        GitRepository.create(storage_path)
        @repository = Rugged::Repository.new(storage_path)
      end

      mutex = Mutex.new
      mutex.synchronize do
        @data = pac_files.reduce({}) do |memo, file| 
          compressor_engine.new.prepare(file)
          memo[file.name.to_sym] = file

          memo
        end
      end
    end

    def [](key)
      data.fetch(key, null_file)
    end

    private

    def pac_files
      LocalPac.ui_logger.debug "I'm using the following storage path: \"#{storage_path}\"."
      files = files_in_repository.keep_if { |f| f.extension? '.pac' }
      LocalPac.ui_logger.debug "Found the following files: \"#{files.join(", ")}\"."

      files
    end

    def files_in_repository
      head_commit = repository.lookup(repository.head.target)

      files = []
      head_commit.tree.walk_blobs(:postorder) do |root, entry| 
        files << file_creator.new("#{root}#{entry[:name]}", repository.lookup(entry[:oid]).content ) 
      end

      files
    rescue Rugged::ReferenceError
      []
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
local_pac-0.3.0 lib/local_pac/git_storage.rb
local_pac-0.2.3 lib/local_pac/git_storage.rb
local_pac-0.2.2 lib/local_pac/git_storage.rb
local_pac-0.2.1 lib/local_pac/git_storage.rb
local_pac-0.2.0 lib/local_pac/git_storage.rb