Class: Berkshelf::CookbookStore

Inherits:
Object
  • Object
show all
Defined in:
lib/berkshelf/cookbook_store.rb

Overview

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_path) ⇒ CookbookStore

Create a new instance of CookbookStore with the given storage_path.

Parameters:

  • storage_path (String)

    local filesystem path to the location to be initialized as a CookbookStore.



16
17
18
19
# File 'lib/berkshelf/cookbook_store.rb', line 16

def initialize(storage_path)
  @storage_path = Pathname.new(storage_path)
  initialize_filesystem
end

Instance Attribute Details

#storage_pathString (readonly)

Returns filepath to where cookbooks are stored.

Returns:

  • (String)

    filepath to where cookbooks are stored



8
9
10
# File 'lib/berkshelf/cookbook_store.rb', line 8

def storage_path
  @storage_path
end

Instance Method Details

#cookbook(name, version) ⇒ Berkshelf::CachedCookbook?

Returns an instance of CachedCookbook representing the Cookbook of your given name and version.

Parameters:

  • name (String)

    name of the Cookbook you want to retrieve

  • version (String)

    version of the Cookbook you want to retrieve

Returns:



30
31
32
33
34
35
# File 'lib/berkshelf/cookbook_store.rb', line 30

def cookbook(name, version)
  path = cookbook_path(name, version)
  return nil unless path.cookbook?

  CachedCookbook.from_store_path(path)
end

#cookbook_path(name, version) ⇒ Pathname

Returns an expanded path to the location on disk where the Cookbook of the given name and version is located.

Parameters:

Returns:



64
65
66
# File 'lib/berkshelf/cookbook_store.rb', line 64

def cookbook_path(name, version)
  storage_path.join("#{name}-#{version}")
end

#cookbooks(filter = nil) ⇒ Array<Berkshelf::CachedCookbook>

Returns an array of the Cookbooks that have been cached to the storage_path of this instance of CookbookStore.

Parameters:

  • filter (String) (defaults to: nil)

    return only the CachedCookbooks whose name match the given filter

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/berkshelf/cookbook_store.rb', line 44

def cookbooks(filter = nil)
  [].tap do |cookbooks|
    storage_path.each_child do |p|
      cached_cookbook = CachedCookbook.from_store_path(p)

      next unless cached_cookbook
      next if filter && cached_cookbook.cookbook_name != filter

      cookbooks << cached_cookbook
    end
  end
end

#satisfy(name, constraint) ⇒ Berkshelf::CachedCookbook?

Return a CachedCookbook matching the best solution for the given name and constraint. Nil is returned if no matching CachedCookbook is found.

Parameters:

  • name (#to_s)
  • constraint (Solve::Constraint)

Returns:



75
76
77
78
79
80
81
82
83
84
# File 'lib/berkshelf/cookbook_store.rb', line 75

def satisfy(name, constraint)
  graph = Solve::Graph.new
  cookbooks(name).each { |cookbook| graph.artifacts(name, cookbook.version) }

  name, version = Solve.it!(graph, [[name, constraint]]).first

  cookbook(name, version)
rescue Solve::Errors::NoSolutionError
  nil
end