Sha256: cc76ec3d8ec8d413a623f23fc8c37ba0887e5d30e58b95725eb9babfd20265b3

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

# encoding: utf-8
module LocalPac
  class GitStorage

    private

    attr_reader :storage_path, :repository, :file_creator, :null_file, :vcs_engine, :compressor_engine

    public

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

      begin
        @repository = vcs_engine.new(storage_path)
      rescue Exceptions::RepositoryDoesNotExist
        @repository = vcs_engine.create(storage_path)
      end
    end

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

    def each_pac_file(&block)
      pac_files.each(&block)
    end

    def each_added_pac_file(old_commit_id, new_commit_id, &block)
      added_pac_files(old_commit_id, new_commit_id).each(&block)
    end

    private

    def data
      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 added_pac_files(old_commit_id, new_commit_id)
      logging_for_repo_action do
        repository.added_files(old_commit_id, new_commit_id).keep_if { |f| f.extension? '.pac' }
      end
    end

    def pac_files
      logging_for_repo_action do
        repository.all_files.keep_if { |f| f.extension? '.pac' }
      end
    end

    def logging_for_repo_action(&block)
      LocalPac.ui_logger.debug "I'm using the following storage path: \"#{storage_path}\"."
      files = block.call
      LocalPac.ui_logger.debug "Found the following files: #{files.collect { |f| "\"#{f.path}\"" }.join(", ")}."

      files
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
local_pac-0.9.0 lib/local_pac/git_storage.rb
local_pac-0.7.0 lib/local_pac/git_storage.rb
local_pac-0.6.3 lib/local_pac/git_storage.rb
local_pac-0.6.2 lib/local_pac/git_storage.rb
local_pac-0.6.1 lib/local_pac/git_storage.rb