Sha256: 4e181ebee884e1352d57b311479a389654e22c1a307f42fc4d58a682030a80ce

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'fileutils'

module Uricp::Strategy
  module CacheCommon
    def validate_cache!
      return if dry_run?

      unless cache_exists?
        raise Uricp::MissingCache,
              "no cache found at #{cache_root}. Expected a 'cache' and 'temp' directory"
      end
      return if cache_name

      raise Uricp::MissingCache,
            'no cache name found'
    end

    def with_active_cache
      if cache_root
        yield
      else
        debug "#{self.class.name}: no cacheing requested"
        false
      end
    end

    def without_active_cache
      unless cache_root
        yield
      else
        debug "#{self.class.name}: cache active - not appropriate"
        false
      end
    end

    def in_cache?
      File.readable?(cache_file) || options['dry-cache'] == :rbd
    end

    def cache_root
      options['cache']
    end

    def cache_name
      options['cache_name']
    end

    def cache_exists?
      cache_root && %w[temp cache].all? do |d|
        File.directory?(File.join(cache_root, d))
      end
    end

    def temp_cache_file
      @temp_cache_file ||= File.join(cache_root, 'temp', cache_name)
    end

    def temp_cache_uri
      URI.join('file:///', temp_cache_file)
    end

    def cache_file
      @cache_file ||= File.join(cache_root, 'cache', cache_name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uricp-0.0.24 lib/uricp/strategy/cache_common.rb
uricp-0.0.23 lib/uricp/strategy/cache_common.rb