module Poise::Helpers::IncludeRecipe

A provider mixin to add include_recipe that can be called from action methods.

@since 2.0.0

Public Instance Methods

include_recipe(*recipes) click to toggle source
# File lib/poise/helpers/include_recipe.rb, line 30
def include_recipe(*recipes)
  loaded_recipes = []
  subcontext = subcontext_block do
    recipes.flatten.each do |recipe|
      case recipe
      when String
        # Process normally
        Chef::Log.debug("Loading recipe #{recipe} via include_recipe (poise)")
        loaded_recipes += run_context.include_recipe(recipe)
      when Proc
        # Pretend its a block of recipe code
        fake_recipe = Chef::Recipe.new(cookbook_name, new_resource.recipe_name, run_context)
        fake_recipe.instance_eval(&recipe)
        loaded_recipes << fake_recipe
      end
    end
  end
  # Converge the new context.
  Poise::Subcontext::Runner.new(new_resource, subcontext).converge
  collection = global_resource_collection
  subcontext.resource_collection.each do |r|
    Chef::Log.debug("Poise::IncludeRecipe: Adding #{r} to global collection #{collection.object_id}")
    # Insert the local resource into the global context
    collection.insert(r)
    # Skip the iterator forward so we don't double-execute the inserted resource
    # If running at compile time, the iterator is nil
    collection.iterator.skip_forward if collection.iterator
  end
  loaded_recipes
end