Class: Itamae::Recipe::EvalContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe) ⇒ EvalContext

Returns a new instance of EvalContext.



54
55
56
57
58
59
# File 'lib/itamae/recipe.rb', line 54

def initialize(recipe)
  @recipe = recipe
  @children = RecipeChildren.new

  instance_eval(File.read(@recipe.path), @recipe.path, 1)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/itamae/recipe.rb', line 68

def method_missing(*args, &block)
  super unless args.size == 2

  method, name = args
  begin
    klass = Resource.get_resource_class(method)
  rescue NameError
    super
  end

  resource = klass.new(@recipe, name, &block)
  @children << resource
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



52
53
54
# File 'lib/itamae/recipe.rb', line 52

def children
  @children
end

Instance Method Details

#define(name, params = {}, &block) ⇒ Object



82
83
84
85
86
87
# File 'lib/itamae/recipe.rb', line 82

def define(name, params = {}, &block)
  Resource.const_set(
    Resource.get_resource_class_name(name),
    Definition.create_class(name, params, &block)
  )
end

#include_recipe(target) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/itamae/recipe.rb', line 89

def include_recipe(target)
  candidate_paths = [
    ::File.expand_path(target, File.dirname(@recipe.path)),
    Recipe.find_recipe_in_gem(target),
  ].compact
  path = candidate_paths.find {|path| File.exist?(path) }

  unless path
    raise NotFoundError, "Recipe not found. (#{target})"
  end

  if runner.children.find_recipe_by_path(path)
    Logger.debug "Recipe, #{target}, is skipped because it is already included"
    return
  end

  recipe = Recipe.new(runner, path)
  @children << recipe
end

#nodeObject



109
110
111
# File 'lib/itamae/recipe.rb', line 109

def node
  runner.node
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/itamae/recipe.rb', line 61

def respond_to_missing?(method, include_private = false)
  Resource.get_resource_class(method)
  true
rescue NameError
  false
end

#runnerObject



113
114
115
# File 'lib/itamae/recipe.rb', line 113

def runner
  @recipe.runner
end