Class: ChefAPI::Resource::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chef-api/resources/collection_proxy.rb

Direct Known Subclasses

DataBagItemCollectionProxy

Instance Method Summary collapse

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.

Parameters:

  • parent (Resource::Base)

    the parent resource that created the collection

  • klass (Class)

    the class the resulting objects should be

  • endpoint (String)

    the relative path for the RESTful endpoint



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

#allArray<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.

Returns:



92
93
94
# File 'lib/chef-api/resources/collection_proxy.rb', line 92

def all
  entries
end

#countObject 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.

Parameters:

  • id (String, Symbol)

    the unique id of the resource to find.

Returns:

  • (Boolean)

    true if the resource exists, false otherwise



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.

Examples:

Fetch a resource

Bacon.first.items.fetch('crispy')

Parameters:

  • id (String, Symbol)

    the id of the resource to fetch

Returns:

  • (Resource::Base, nil)

    the fetched class, or nil if it does not exists



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

#inspectString

The detailed string representation of this collection proxy.

Returns:

  • (String)


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.

Returns:

  • (self)


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_sString

The string representation of this collection proxy.

Returns:

  • (String)


122
123
124
# File 'lib/chef-api/resources/collection_proxy.rb', line 122

def to_s
  "#<#{self.class.name}>"
end