Class: ChefAPI::Resource::CollectionProxy
- Inherits:
-
Object
- Object
- ChefAPI::Resource::CollectionProxy
- Includes:
- Enumerable
- Defined in:
- lib/chef-api/resources/collection_proxy.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#all ⇒ Array<Resource::Base>
Get the full list of all entries in the collection.
-
#count ⇒ Object
(also: #size)
The total number of items in this collection.
-
#each(&block) ⇒ Object
The custom iterator for looping over each object in this collection.
-
#exists?(id) ⇒ Boolean
Determine if the resource with the given id exists on the remote Chef Server.
-
#fetch(id) ⇒ Resource::Base?
Fetch a specific resource in the collection by id.
-
#initialize(parent, klass, endpoint, prefix = {}) ⇒ CollectionProxy
constructor
Create a new collection proxy from the given parent class and collection information.
-
#inspect ⇒ String
The detailed string representation of this collection proxy.
-
#reload! ⇒ self
Force a reload of this collection proxy and all its elements.
-
#to_s ⇒ String
The string representation of this collection proxy.
Constructor Details
#initialize(parent, klass, endpoint, prefix = {}) ⇒ CollectionProxy
Create a new collection proxy from the given parent class and collection information. The collection proxy aims to make working with nested resource collections a bit easier. The proxy waits until non-existing data is requested before making another HTTP request. In this way, it helps reduce bandwidth and API requets.
Additionally, the collection proxy caches the results of an object request in memory by id
, so additional requests for the same object will hit the cache, again reducing HTTP requests.
25 26 27 28 29 30 31 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 25 def initialize(parent, klass, endpoint, prefix = {}) @parent = parent @klass = klass @endpoint = "#{parent.resource_path}/#{endpoint}" @prefix = prefix @collection = load_collection end |
Instance Method Details
#all ⇒ Array<Resource::Base>
Get the full list of all entries in the collection. This method is incredibly expensive and should only be used when you absolutely need all resources. If you need a specific resource, you should use an iterator such as select
or find
instead, since it will minimize HTTP requests. Once all the objects are requested, they are cached, reducing the number of HTTP requests.
92 93 94 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 92 def all entries end |
#count ⇒ Object Also known as: size
The total number of items in this collection. This method does not make an API request, but rather counts the number of keys in the given collection.
112 113 114 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 112 def count collection.length end |
#each(&block) ⇒ Object
The custom iterator for looping over each object in this collection. For more information, please see the Enumerator
module in Ruby core.
100 101 102 103 104 105 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 100 def each(&block) collection.each do |id, url| object = cached(id) { klass.from_url(url, prefix) } block.call(object) if block end end |
#exists?(id) ⇒ Boolean
Determine if the resource with the given id exists on the remote Chef Server. This method does not actually query the Chef Server, but rather delegates to the cached collection. To guarantee the most fresh set of data, you should call reload!
before exists?
to ensure you have the most up-to-date collection of resources.
78 79 80 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 78 def exists?(id) collection.has_key?(id.to_s) end |
#fetch(id) ⇒ Resource::Base?
Fetch a specific resource in the collection by id.
60 61 62 63 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 60 def fetch(id) return nil unless exists?(id) cached(id) { klass.from_url(get(id), prefix) } end |
#inspect ⇒ String
The detailed string representation of this collection proxy.
131 132 133 134 135 136 137 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 131 def inspect objects = collection .map { |id, _| cached(id) || klass.new(klass.schema.primary_key => id) } .map { |object| object.to_s } "#<#{self.class.name} [#{objects.join(', ')}]>" end |
#reload! ⇒ self
Force a reload of this collection proxy and all its elements. This is useful if you think additional items have been added to the remote collection and need access to them locally. This will also clear any existing cached responses, so use with caution.
41 42 43 44 45 46 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 41 def reload! cache.clear @collection = load_collection self end |
#to_s ⇒ String
The string representation of this collection proxy.
122 123 124 |
# File 'lib/chef-api/resources/collection_proxy.rb', line 122 def to_s "#<#{self.class.name}>" end |