Module: CommunityZero::Store

Extended by:
Store
Included in:
Store
Defined in:
lib/community_zero/store.rb

Overview

Author:

Instance Method Summary collapse

Instance Method Details

#add(cookbook) ⇒ Object Also known as: update

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

Parameters:



70
71
72
73
74
75
76
77
# File 'lib/community_zero/store.rb', line 70

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

#cookbooksArray<CommunityZero::Cookbook>

The full array of cookbooks.

Examples:

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

Returns:



41
42
43
# File 'lib/community_zero/store.rb', line 41

def cookbooks
  _cookbooks.map { |_,v| v[v.keys.first] }
end

#destroy_allObject

Delete all cookbooks in the store.



61
62
63
# File 'lib/community_zero/store.rb', line 61

def destroy_all
  @_cookbooks = nil
end

#find(name, version = nil) ⇒ CommunityZero::Cookbook?

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.

Parameters:

  • name (String)

    the name of the cookbook to find

  • version (String, nil) (defaults to: nil)

    the version of the cookbook to search

Returns:



109
110
111
112
113
114
115
# File 'lib/community_zero/store.rb', line 109

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) ⇒ Boolean

Determine if the cookbook store contains a cookbook.

Returns:

  • (Boolean)

See Also:

  • for the method signature and parameters


92
93
94
# File 'lib/community_zero/store.rb', line 92

def has_cookbook?(name, version = nil)
  !find(name, version).nil?
end

#remove(cookbook) ⇒ Object

Remove the cookbook from the store.

Parameters:



84
85
86
87
# File 'lib/community_zero/store.rb', line 84

def remove(cookbook)
  return unless has_cookbook?(cookbook.name, cookbook.version)
  _cookbooks[cookbook.name].delete(cookbook.version)
end

#search(query) ⇒ Array<CommunityZero::Cookbook>

Query the installed cookbooks, returning those who’s name matches the given query.

Parameters:

  • query (String)

    the query parameter

Returns:



53
54
55
56
57
58
# File 'lib/community_zero/store.rb', line 53

def search(query)
  regex = Regexp.new(query, 'i')
  _cookbooks.collect do |_, v|
    v[v.keys.first] if regex.match(v[v.keys.first].name)
  end.compact
end

#sizeFixnum

The number of cookbooks in the store.

Returns:

  • (Fixnum)

    the number of cookbooks in the store



27
28
29
# File 'lib/community_zero/store.rb', line 27

def size
  _cookbooks.keys.size
end

#versions(name) ⇒ Object

Return a list of all versions for the given cookbook.

Parameters:



121
122
123
124
# File 'lib/community_zero/store.rb', line 121

def versions(name)
  name = name.respond_to?(:name) ? name.name : name
  (_cookbooks[name] && _cookbooks[name].keys.sort) || []
end