Class: CommunityZero::Store
- Inherits:
-
Object
- Object
- CommunityZero::Store
- Defined in:
- lib/community_zero/store.rb
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.
-
#latest_version(name) ⇒ Object
Return the latest version of the given 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.
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 |
#cookbooks ⇒ Array<CommunityZero::Cookbook>
The full array of cookbooks.
39 40 41 |
# File 'lib/community_zero/store.rb', line 39 def cookbooks _cookbooks.map { |_,v| v[v.keys.first] } end |
#destroy_all ⇒ Object
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.
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.
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.
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.
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.
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 |
#size ⇒ 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.
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 |