# class Chef::ResourceCollection

### Constants

MULTIPLE_RESOURCE_MATCH

  Matches a multiple resource lookup specification, e.g., “service”

SINGLE_RESOURCE_MATCH

  Matches a single resource lookup specification, e.g., “service”

### Attributes

iterator[R]

### Public Class Methods

json_create(o) click to toggle source

    # File lib/chef/resource_collection.rb, line 204
    def self.json_create(o)
      collection = self.new()
      o["instance_vars"].each do |k,v|
        collection.instance_variable_set(k.to_sym, v)
      end
      collection
    end

new() click to toggle source

    # File lib/chef/resource_collection.rb, line 37
    def initialize
      @resources = Array.new
      @resources_by_name = Hash.new
      @insert_after_idx = nil
    end

### Public Instance Methods

<<(*args) click to toggle source

    # File lib/chef/resource_collection.rb, line 57
    def <<(*args)
      args.flatten.each do |a|
        is_chef_resource(a)
        @resources << a
        @resources_by_name[a.to_s] = @resources.length - 1
      end
      self
    end

Also aliased as: push
[](index) click to toggle source

    # File lib/chef/resource_collection.rb, line 47
    def [](index)
      @resources[index]
    end

[]=(index, arg) click to toggle source

    # File lib/chef/resource_collection.rb, line 51
    def []=(index, arg)
      is_chef_resource(arg)
      @resources[index] = arg
      @resources_by_name[arg.to_s] = index
    end

all_resources() click to toggle source

    # File lib/chef/resource_collection.rb, line 43
    def all_resources
      @resources
    end

each() { |resource| ... } click to toggle source

    # File lib/chef/resource_collection.rb, line 88
    def each
      @resources.each do |resource|
        yield resource
      end
    end

each_index() { |i| ... } click to toggle source

    # File lib/chef/resource_collection.rb, line 102
    def each_index
      @resources.each_index do |i|
        yield i
      end
    end

empty?() click to toggle source

    # File lib/chef/resource_collection.rb, line 108
    def empty?
      @resources.empty?
    end

execute_each_resource() { |resource| ... } click to toggle source

    # File lib/chef/resource_collection.rb, line 94
    def execute_each_resource(&resource_exec_block)
      @iterator = StepableIterator.for_collection(@resources)
      @iterator.each_with_index do |resource, idx|
        @insert_after_idx = idx
        yield resource
      end
    end

find(*args) click to toggle source

Find existing resources by searching the list of existing resources. Possible forms are:

find(:file => “foobar”) find(:file => [ “foobar”, “baz” ]) find(“file”, “file”) find(“file”)

Returns the matching resource, or an Array of matching resources.

Raises an ArgumentError if you feed it bad lookup information Raises a Runtime Error if it can’t find the resources you are looking for.

    # File lib/chef/resource_collection.rb, line 140
    def find(*args)
      results = Array.new
      args.each do |arg|
        case arg
        when Hash
          results << find_resource_by_hash(arg)
        when String
          results << find_resource_by_string(arg)
        else
          msg = "arguments to #{self.class.name}#find should be of the form :resource => 'name' or resource[name]"
          raise Chef::Exceptions::InvalidResourceSpecification, msg
        end
      end
      flat_results = results.flatten
      flat_results.length == 1 ? flat_results[0] : flat_results
    end

Also aliased as: resources
insert(resource) click to toggle source

    # File lib/chef/resource_collection.rb, line 69
    def insert(resource)
      is_chef_resource(resource)
      if @insert_after_idx
        # in the middle of executing a run, so any resources inserted now should
        # be placed after the most recent addition done by the currently executing
        # resource
        @resources.insert(@insert_after_idx + 1, resource)
        # update name -> location mappings and register new resource
        @resources_by_name.each_key do |key|
          @resources_by_name[key] += 1 if @resources_by_name[key] > @insert_after_idx
        end
        @resources_by_name[resource.to_s] = @insert_after_idx + 1
        @insert_after_idx += 1
      else
        @resources << resource
        @resources_by_name[resource.to_s] = @resources.length - 1
      end
    end

lookup(resource) click to toggle source

    # File lib/chef/resource_collection.rb, line 112
    def lookup(resource)
      lookup_by = nil
      if resource.kind_of?(Chef::Resource)
        lookup_by = resource.to_s
      elsif resource.kind_of?(String)
        lookup_by = resource
      else
        raise ArgumentError, "Must pass a Chef::Resource or String to lookup"
      end
      res = @resources_by_name[lookup_by]
      unless res
        raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{lookup_by} (did you define it first?)"
      end
      @resources[res]
    end

push(*args)

‘push’ is an alias method to <<

Alias for: <<
resources(*args)

resources is a poorly named, but we have to maintain it for back compat.

Alias for: find
to_json(*a) click to toggle source

Serialize this object as a hash

    # File lib/chef/resource_collection.rb, line 192
    def to_json(*a)
      instance_vars = Hash.new
      self.instance_variables.each do |iv|
        instance_vars[iv] = self.instance_variable_get(iv)
      end
      results = {
        'json_class' => self.class.name,
        'instance_vars' => instance_vars
      }
      results.to_json(*a)
    end

validate_lookup_spec!(query_object) click to toggle source

Returns true if `query_object` is a valid string for looking up a resource, or raises InvalidResourceSpecification if not.

### Arguments¶ ↑

- query_object should be a string of the form

“resource_type”, a single element Hash (e.g., :service => “apache2”), or a Chef::Resource (this is the happy path). Other arguments will raise an exception.

### Returns¶ ↑

- true returns true for all valid input.

### Raises¶ ↑

- Chef::Exceptions::InvalidResourceSpecification for all invalid input.

    # File lib/chef/resource_collection.rb, line 173
    def validate_lookup_spec!(query_object)
      case query_object
      when Chef::Resource
        true
      when SINGLE_RESOURCE_MATCH, MULTIPLE_RESOURCE_MATCH
        true
      when Hash
        true
      when String
        raise Chef::Exceptions::InvalidResourceSpecification,
          "The string `#{query_object}' is not valid for resource collection lookup. Correct syntax is `resource_type[resource_name]'"
      else
        raise Chef::Exceptions::InvalidResourceSpecification,
          "The object `#{query_object.inspect}' is not valid for resource collection lookup. " +
          "Use a String like `resource_type[resource_name]' or a Chef::Resource object"
      end
    end