Class: Kitchen::Recipe

Inherits:
Object show all
Defined in:
lib/kitchen/recipe.rb

Overview

An object that yields a Document for modification (those modifications are the “recipe”)

Direct Known Subclasses

BookRecipe

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locales_dir: nil) {|doc| ... } ⇒ Recipe

Make a new Recipe

Parameters:

  • locales_dir (String, nil) (defaults to: nil)

    the absolute path to a folder containing recipe-specific I18n translations. If not provided, Kitchen will look for a ‘locales` directory in the same directory as the recipe source. Recipe-specific translations override those in Kitchen. If no recipe-specific locales directory exists, Kitchen will just use its default translations.

Yields:

  • A block for defining the steps of the recipe

Yield Parameters:

  • doc (Document)

    an object representing an XML document

Raises:



46
47
48
49
50
51
52
53
# File 'lib/kitchen/recipe.rb', line 46

def initialize(locales_dir: nil, &block)
  raise(RecipeError, 'Recipes must be initialized with a block') unless block_given?

  @source_location = block.source_location[0]
  @block = block

  load_my_i18n_backend(locales_dir)
end

Instance Attribute Details

#documentDocument

The document the recipe makes available for modification

Returns:



11
12
13
# File 'lib/kitchen/recipe.rb', line 11

def document
  @document
end

#my_i18n_backendI18n::Backend::Simple? (readonly)

An I18n backend specific to this recipe, may be nil

Returns:

  • (I18n::Backend::Simple, nil)


19
20
21
# File 'lib/kitchen/recipe.rb', line 19

def my_i18n_backend
  @my_i18n_backend
end

#source_locationString (readonly)

The file location of the recipe

Returns:



15
16
17
# File 'lib/kitchen/recipe.rb', line 15

def source_location
  @source_location
end

Instance Method Details

#bakeObject

Executes the block given to Recipe.new on the document. Aka, does the baking.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kitchen/recipe.rb', line 57

def bake
  with_my_locales do
    @block.to_proc.call(document)
  end
rescue RecipeError, ElementNotFoundError, Nokogiri::CSS::SyntaxError => e
  print_recipe_error_and_exit(e)
rescue ArgumentError, NoMethodError => e
  raise unless any_stack_file_matches_source_location?(e)

  print_recipe_error_and_exit(e)
rescue NameError => e
  raise unless stack_starts_with_source_location?(e)

  print_recipe_error_and_exit(e)
end