Class: CommunityZero::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/community_zero/store.rb

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:



68
69
70
71
72
73
74
75
# 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

#cookbooksArray<CommunityZero::Cookbook>

The full array of cookbooks.

Examples:

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

Returns:



39
40
41
# File 'lib/community_zero/store.rb', line 39

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

#destroy_allObject

Delete all cookbooks in the store.



59
60
61
# File 'lib/community_zero/store.rb', line 59

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:



107
108
109
110
111
112
113
# 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) ⇒ Boolean

Determine if the cookbook store contains a cookbook.

Returns:

  • (Boolean)

See Also:

  • for the method signature and parameters


90
91
92
# File 'lib/community_zero/store.rb', line 90

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

#latest_version(name) ⇒ Object

Return the latest version of the given cookbook.

Parameters:



128
129
130
# File 'lib/community_zero/store.rb', line 128

def latest_version(name)
  versions(name).last
end

#remove(cookbook) ⇒ Object

Remove the cookbook from the store.

Parameters:



82
83
84
85
# 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

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



51
52
53
54
55
56
# File 'lib/community_zero/store.rb', line 51

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



25
26
27
# File 'lib/community_zero/store.rb', line 25

def size
  _cookbooks.keys.size
end

#versions(name) ⇒ Object

Return a list of all versions for the given cookbook.

Parameters:



119
120
121
122
# 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