Module: CommunityZero::Store
Overview
Instance Method Summary collapse
-
#add(cookbook) ⇒ Object
(also: #update)
Add the given cookbook to the cookbook store.
-
#cookbooks ⇒ Array<CommunityZero::Cookbook>
The full array of cookbooks.
-
#destroy_all ⇒ Object
Delete all cookbooks in the store.
-
#find(name, version = nil) ⇒ CommunityZero::Cookbook?
Determine if the cookbook store contains a cookbook.
-
#has_cookbook?(name, version = nil) ⇒ Boolean
Determine if the cookbook store contains a cookbook.
-
#remove(cookbook) ⇒ Object
Remove the cookbook from the store.
-
#search(query) ⇒ Array<CommunityZero::Cookbook>
Query the installed cookbooks, returning those who’s name matches the given query.
-
#size ⇒ Fixnum
The number of cookbooks in the store.
-
#versions(name) ⇒ Object
Return a list of all versions for the given cookbook.
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.
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 |
#cookbooks ⇒ Array<CommunityZero::Cookbook>
The full array of cookbooks.
41 42 43 |
# File 'lib/community_zero/store.rb', line 41 def cookbooks _cookbooks.map { |_,v| v[v.keys.first] } end |
#destroy_all ⇒ Object
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.
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.
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.
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.
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 |
#size ⇒ 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.
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 |