Class: Berkshelf::API::DependencyCache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/berkshelf/api/dependency_cache.rb

Overview

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" }
  }
}

}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

init, #logger

Constructor Details

#initialize(contents = {}) ⇒ DependencyCache

Returns a new instance of DependencyCache.

Parameters:

  • contents (Hash) (defaults to: {})


41
42
43
44
45
46
47
48
49
# 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

Class Method Details

.from_file(filepath) ⇒ DependencyCache

Read an archived cache and re-instantiate it

Parameters:

  • filepath (#to_s)

    path to an archived cache

Returns:

Raises:



26
27
28
29
30
31
32
33
# 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

Instance Method Details

#add(cookbook, metadata) ⇒ Hash

Parameters:

  • cookbook (RemoteCookbook)
  • metadata (Ridley::Chef::Cookbook::Metadata)

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/berkshelf/api/dependency_cache.rb', line 55

def add(cookbook, )
  platforms    = .platforms || Hash.new
  dependencies = .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

#clearHash

Clear any items added to this instance

Returns:

  • (Hash)


71
72
73
# File 'lib/berkshelf/api/dependency_cache.rb', line 71

def clear
  @cache.clear
end

#cookbooksArray<RemoteCookbook>

Returns:



130
131
132
133
134
135
136
137
138
# 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, |
        remote_cookbooks << RemoteCookbook.new(name, version, [:location_type], [:location_path], [:endpoint_priority])
      end
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/berkshelf/api/dependency_cache.rb', line 102

def empty?
  @cache.empty?
end

#has_cookbook?(name, version) ⇒ Boolean

Check if the cache knows about the given cookbook version

Parameters:

  • name (#to_s)
  • version (#to_s)

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# 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) ⇒ Hash

Parameters:

  • name (String)
  • version (String)

Returns:

  • (Hash)


93
94
95
96
97
98
99
# 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) ⇒ Object



140
141
142
143
# 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_warmedObject



149
150
151
# File 'lib/berkshelf/api/dependency_cache.rb', line 149

def set_warmed
  @warmed = true
end

#to_hashHash

Returns:

  • (Hash)


107
108
109
# File 'lib/berkshelf/api/dependency_cache.rb', line 107

def to_hash
  @cache.to_hash
end

#to_json(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


114
115
116
117
118
# 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 = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


123
124
125
126
127
# 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?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/berkshelf/api/dependency_cache.rb', line 145

def warmed?
  @warmed
end