Class: Berkshelf::CookbookStore

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

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.



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

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



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

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:



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

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:



67
68
69
# File 'lib/berkshelf/cookbook_store.rb', line 67

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:



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

def cookbooks(filter = nil)
  cookbooks = []

  storage_path.each_child.map do |path|
    Celluloid::Future.new do
      cached_cookbook = CachedCookbook.from_store_path(path)

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

      cookbooks << cached_cookbook
    end
  end.each(&:value)

  cookbooks
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:



78
79
80
81
82
83
84
85
86
87
# File 'lib/berkshelf/cookbook_store.rb', line 78

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