Class: Berkshelf::API::DependencyCache
- Inherits:
-
Object
- Object
- Berkshelf::API::DependencyCache
- 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
-
.from_file(filepath) ⇒ DependencyCache
Read an archived cache and re-instantiate it.
Instance Method Summary collapse
- #add(cookbook, metadata) ⇒ Hash
-
#clear ⇒ Hash
Clear any items added to this instance.
- #cookbooks ⇒ Array<RemoteCookbook>
- #empty? ⇒ Boolean
-
#has_cookbook?(name, version) ⇒ Boolean
Check if the cache knows about the given cookbook version.
-
#initialize(contents = {}) ⇒ DependencyCache
constructor
A new instance of DependencyCache.
- #remove(name, version) ⇒ Hash
- #save(path) ⇒ Object
- #set_warmed ⇒ Object
- #to_hash ⇒ Hash
- #to_json(options = {}) ⇒ String
- #to_msgpack(options = {}) ⇒ String
- #warmed? ⇒ Boolean
Methods included from Logging
Constructor Details
#initialize(contents = {}) ⇒ DependencyCache
Returns a new instance of DependencyCache.
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
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
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 |
#clear ⇒ Hash
Clear any items added to this instance
71 72 73 |
# File 'lib/berkshelf/api/dependency_cache.rb', line 71 def clear @cache.clear end |
#cookbooks ⇒ Array<RemoteCookbook>
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
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
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
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_warmed ⇒ Object
149 150 151 |
# File 'lib/berkshelf/api/dependency_cache.rb', line 149 def set_warmed @warmed = true end |
#to_hash ⇒ Hash
107 108 109 |
# File 'lib/berkshelf/api/dependency_cache.rb', line 107 def to_hash @cache.to_hash end |
#to_json(options = {}) ⇒ String
114 115 116 117 118 |
# File 'lib/berkshelf/api/dependency_cache.rb', line 114 def to_json( = {}) output = to_hash output['endpoints_checksum'] = Application.config.endpoints_checksum if [:saving] JSON.generate(output, ) end |
#to_msgpack(options = {}) ⇒ String
123 124 125 126 127 |
# File 'lib/berkshelf/api/dependency_cache.rb', line 123 def to_msgpack( = {}) output = to_hash output['endpoints_checksum'] = Application.config.endpoints_checksum if [:saving] MessagePack.pack(output) end |
#warmed? ⇒ Boolean
145 146 147 |
# File 'lib/berkshelf/api/dependency_cache.rb', line 145 def warmed? @warmed end |