class Berkshelf::API::DependencyCache

@note

DependencyCache stores its data internally as a Mash
The structure is as follows

{
  "cookbook_name" => {
    "x.y.z" => {
      :endpoint_priority => 1,
      :dependencies => { "cookbook_name" => "constraint" },
      :platforms => { "platform" => "constraint" }
    }
  }
}

Public Class Methods

from_file(filepath) click to toggle source

Read an archived cache and re-instantiate it

@param [#to_s] filepath

path to an archived cache

@raise [Berkshelf::API::SaveNotFoundError] @raise [Berkshelf::API::InvalidSaveError]

@return [DependencyCache]

# File lib/berkshelf/api/dependency_cache.rb, line 26
def from_file(filepath)
  contents = JSON.parse(File.read(filepath.to_s))
  new(contents)
rescue Errno::ENOENT => ex
  raise SaveNotFoundError.new(ex)
rescue JSON::ParserError => ex
  raise InvalidSaveError.new(ex)
end
new(contents = {}) click to toggle source

@param [Hash] contents

# File lib/berkshelf/api/dependency_cache.rb, line 41
def initialize(contents = {})
  @warmed = false
  @cache  = Hash[contents].with_indifferent_access
  if @cache['endpoints_checksum'] && (@cache['endpoints_checksum'] != Application.config.endpoints_checksum)
    log.warn "Endpoints in config have changed - invalidating cache"
    @cache.clear
  end
  @cache.delete('endpoints_checksum')
end

Public Instance Methods

add(cookbook, metadata) click to toggle source

@param [RemoteCookbook] cookbook @param [Ridley::Chef::Cookbook::Metadata] metadata

@return [Hash]

# File lib/berkshelf/api/dependency_cache.rb, line 55
def add(cookbook, metadata)
  platforms    = metadata.platforms || Hash.new
  dependencies = metadata.dependencies || Hash.new
  @cache[cookbook.name.to_s] ||= Hash.new
  @cache[cookbook.name.to_s][cookbook.version.to_s] = {
    endpoint_priority: cookbook.priority,
    platforms: platforms,
    dependencies: dependencies,
    location_type: cookbook.location_type,
    location_path: cookbook.location_path
  }
end
clear() click to toggle source

Clear any items added to this instance

@return [Hash]

# File lib/berkshelf/api/dependency_cache.rb, line 71
def clear
  @cache.clear
end
cookbooks() click to toggle source

@return [Array<RemoteCookbook>]

# File lib/berkshelf/api/dependency_cache.rb, line 130
def cookbooks
  [].tap do |remote_cookbooks|
    @cache.each_pair do |name, versions|
      versions.each do |version, metadata|
        remote_cookbooks << RemoteCookbook.new(name, version, metadata[:location_type], metadata[:location_path], metadata[:endpoint_priority])
      end
    end
  end
end
empty?() click to toggle source

@return [Boolean]

# File lib/berkshelf/api/dependency_cache.rb, line 102
def empty?
  @cache.empty?
end
has_cookbook?(name, version) click to toggle source

Check if the cache knows about the given cookbook version

@param [#to_s] name @param [#to_s] version

@return [Boolean]

# File lib/berkshelf/api/dependency_cache.rb, line 81
def has_cookbook?(name, version)
  unless cookbook = @cache[name.to_s]
    return false
  end

  cookbook.has_key?(version.to_s)
end
remove(name, version) click to toggle source

@param [String] name @param [String] version

@return [Hash]

# File lib/berkshelf/api/dependency_cache.rb, line 93
def remove(name, version)
  @cache[name.to_s].delete(version.to_s)
  if @cache[name.to_s].empty?
    @cache.delete(name.to_s)
  end
  @cache
end
save(path) click to toggle source
# File lib/berkshelf/api/dependency_cache.rb, line 140
def save(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w+') { |f| f.write(self.to_json(saving: true)) }
end
set_warmed() click to toggle source
# File lib/berkshelf/api/dependency_cache.rb, line 149
def set_warmed
  @warmed = true
end
to_hash() click to toggle source

@return [Hash]

# File lib/berkshelf/api/dependency_cache.rb, line 107
def to_hash
  @cache.to_hash
end
to_json(options = {}) click to toggle source

@param [Hash] options

@return [String]

# File lib/berkshelf/api/dependency_cache.rb, line 114
def to_json(options = {})
  output = to_hash
  output['endpoints_checksum'] = Application.config.endpoints_checksum if options[:saving]
  JSON.generate(output, options)
end
to_msgpack(options = {}) click to toggle source

@param [Hash] options

@return [String]

# File lib/berkshelf/api/dependency_cache.rb, line 123
def to_msgpack(options = {})
  output = to_hash
  output['endpoints_checksum'] = Application.config.endpoints_checksum if options[:saving]
  MessagePack.pack(output)
end
warmed?() click to toggle source
# File lib/berkshelf/api/dependency_cache.rb, line 145
def warmed?
  @warmed
end