class CommunityZero::Store

@author Seth Vargo <sethvargo@gmail.com>

Public Instance Methods

add(cookbook) click to toggle source

Add the given cookbook to the cookbook store. This method’s implementation prohibits duplicate cookbooks from entering the store.

@param [CommunityZero::Cookbook] cookbook

the cookbook to add
# File lib/community_zero/store.rb, line 68
def add(cookbook)
  cookbook = cookbook.dup
  cookbook.created_at = Time.now
  cookbook.updated_at = Time.now

  entry = _cookbooks[cookbook.name] ||= {}
  entry[cookbook.version] = cookbook
end
Also aliased as: update
cookbooks() click to toggle source

The full array of cookbooks.

@example

[
  #<CommunityZero::Cookbook apache2>,
  #<CommunityZero::Cookbook apt>
]

@return [Array<CommunityZero::Cookbook>]

the list of cookbooks
# File lib/community_zero/store.rb, line 39
def cookbooks
  _cookbooks.map { |_,v| v[v.keys.first] }
end
destroy_all() click to toggle source

Delete all cookbooks in the store.

# File lib/community_zero/store.rb, line 59
def destroy_all
  @_cookbooks = nil
end
find(name, version = nil) click to toggle source

Determine if the cookbook store contains a cookbook. If the version attribute is nil, this method will return the latest cookbook version by that name that exists. If the version is specified, this method will only return that specific version, or nil if that cookbook at that version exists.

@param [String] name

the name of the cookbook to find

@param [String, nil] version

the version of the cookbook to search

@return [CommunityZero::Cookbook, nil]

the cookbook in the store, or nil if one does not exist
# File lib/community_zero/store.rb, line 107
def find(name, version = nil)
  possibles = _cookbooks[name]
  return nil if possibles.nil?

  version ||= possibles.keys.sort.last
  possibles[version]
end
has_cookbook?(name, version = nil) click to toggle source

Determine if the cookbook store contains a cookbook.

@see {find} for the method signature and parameters

# File lib/community_zero/store.rb, line 90
def has_cookbook?(name, version = nil)
  !find(name, version).nil?
end
latest_version(name) click to toggle source

Return the latest version of the given cookbook.

@param [String, CommunityZero::Cookbook] name

the cookbook or name of the cookbook to get versions for
# File lib/community_zero/store.rb, line 128
def latest_version(name)
  versions(name).last
end
remove(cookbook) click to toggle source

Remove the cookbook from the store.

@param [CommunityZero::Cookbook] cookbook

the cookbook to remove
# File lib/community_zero/store.rb, line 82
def remove(cookbook)
  return unless has_cookbook?(cookbook.name, cookbook.version)
  _cookbooks[cookbook.name].delete(cookbook.version)
end
size() click to toggle source

The number of cookbooks in the store.

@return [Fixnum]

the number of cookbooks in the store
# File lib/community_zero/store.rb, line 25
def size
  _cookbooks.keys.size
end
update(cookbook)
Alias for: add
versions(name) click to toggle source

Return a list of all versions for the given cookbook.

@param [String, CommunityZero::Cookbook] name

the cookbook or name of the cookbook to get versions for
# File lib/community_zero/store.rb, line 119
def versions(name)
  name = name.respond_to?(:name) ? name.name : name
  (_cookbooks[name] && _cookbooks[name].keys.sort) || []
end

Private Instance Methods

_cookbooks() click to toggle source

All the cookbooks in the store.

@example

{
  'apache2' => {
    '1.0.0' => {
      'license' => 'Apache 2.0',
      'version' => '1.0.0',
      'tarball_file_size' => 20949,
      'file' => 'http://s3.amazonaws.com...',
      'cookbook' => 'http://localhost:4000/apache2',
      'average_rating' => nil
    }
  }
}

@return [Hash<String, Hash<String, CommunityZero::Cookbook>>]

# File lib/community_zero/store.rb, line 150
def _cookbooks
  @_cookbooks ||= {}
end