Class: Itamae::Recipe::EvalContext

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

Instance Method Summary collapse

Constructor Details

#initialize(recipe, vars) ⇒ EvalContext

Returns a new instance of EvalContext.



89
90
91
92
93
94
95
# File 'lib/itamae/recipe.rb', line 89

def initialize(recipe, vars)
  @recipe = recipe

  vars.each do |k, v|
    define_singleton_method(k) { v }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/itamae/recipe.rb', line 104

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)
  @recipe.children << resource
end

Instance Method Details

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



118
119
120
# File 'lib/itamae/recipe.rb', line 118

def define(name, params = {}, &block)
  Resource.define_resource(name, Definition.create_class(name, params, @recipe, &block))
end

#include_recipe(target) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/itamae/recipe.rb', line 122

def include_recipe(target)
  expanded_path = ::File.expand_path(target, File.dirname(@recipe.path))
  expanded_path = ::File.join(expanded_path, 'default.rb') if ::Dir.exists?(expanded_path)
  expanded_path.concat('.rb') unless expanded_path.end_with?('.rb')
  candidate_paths = [expanded_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)
    Itamae.logger.debug "Recipe, #{path}, is skipped because it is already included"
    return
  end

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

#nodeObject



143
144
145
# File 'lib/itamae/recipe.rb', line 143

def node
  runner.node
end

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

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/itamae/recipe.rb', line 97

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

#run_command(*args) ⇒ Object



151
152
153
# File 'lib/itamae/recipe.rb', line 151

def run_command(*args)
  runner.backend.run_command(*args)
end

#runnerObject



147
148
149
# File 'lib/itamae/recipe.rb', line 147

def runner
  @recipe.runner
end