Class: Ufo::TemplateScope

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/template_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(helper = nil) ⇒ TemplateScope

Returns a new instance of TemplateScope.



4
5
6
7
8
# File 'lib/ufo/template_scope.rb', line 4

def initialize(helper=nil)
  @helper = helper
  load_variables_file("base")
  load_variables_file(Ufo.env)
end

Instance Attribute Details

#helperObject (readonly)

Returns the value of attribute helper.



3
4
5
# File 'lib/ufo/template_scope.rb', line 3

def helper
  @helper
end

Instance Method Details

#assign_instance_variablesObject



35
36
37
38
39
40
41
42
43
# File 'lib/ufo/template_scope.rb', line 35

def assign_instance_variables
  # copy over the instance variables to make available in RenderMePretty's scope
  hash = {}
  instance_variables.each do |var|
    key = var.to_s.sub('@','') # rid of the leading @
    hash[key.to_sym] = instance_variable_get(var)
  end
  hash
end

#load_variables_file(filename) ⇒ Object

Load the variables defined in ufo/variables/* to make available in the template blocks in ufo/templates/*.

Example:

`ufo/variables/base.rb`:
  @name = "docker-process-name"
  @image = "docker-image-name"

`ufo/templates/main.json.erb`:
{
  "containerDefinitions": [
    {
       "name": "<%= @name %>",
       "image": "<%= @image %>",
   ....
}

NOTE: Only able to make instance variables avaialble with instance_eval

Wasnt able to make local variables available.


30
31
32
33
# File 'lib/ufo/template_scope.rb', line 30

def load_variables_file(filename)
  path = "#{Ufo.root}/.ufo/variables/#{filename}.rb"
  instance_eval(IO.read(path)) if File.exist?(path)
end