Class: Ufo::DSL::TaskDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl, task_definition_name, options = {}, &block) ⇒ TaskDefinition

Returns a new instance of TaskDefinition.



8
9
10
11
12
13
# File 'lib/ufo/dsl/task_definition.rb', line 8

def initialize(dsl, task_definition_name, options={}, &block)
  @dsl = dsl
  @task_definition_name = task_definition_name
  @block = block
  @options = options
end

Instance Attribute Details

#task_definition_nameObject (readonly)

Returns the value of attribute task_definition_name.



7
8
9
# File 'lib/ufo/dsl/task_definition.rb', line 7

def task_definition_name
  @task_definition_name
end

Instance Method Details

#assign_instance_variablesObject



28
29
30
31
32
33
34
35
36
# File 'lib/ufo/dsl/task_definition.rb', line 28

def assign_instance_variables
  # copy over the instance variables from TaskDefinition scope to 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

#buildObject



20
21
22
23
24
25
26
# File 'lib/ufo/dsl/task_definition.rb', line 20

def build
  load_variables
  instance_eval(&@block)

  hash = assign_instance_variables
  RenderMePretty.result(source_path, hash)
end

#check_source_path(path) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/ufo/dsl/task_definition.rb', line 94

def check_source_path(path)
  unless File.exist?(path)
    friendly_path = path.sub("#{Ufo.root}/", '')
    puts "ERROR: Could not find the #{friendly_path} template.  Are sure it exists?  Check where you called source in ufo/task_definitions.rb"
    exit 1
  else
    puts "#{task_definition_name} template definition using project template: #{path}" unless @options[:mute]
  end
  path
end

#helperObject

delegate helper method back up to dsl



16
17
18
# File 'lib/ufo/dsl/task_definition.rb', line 16

def helper
  @dsl.helper
end

#load_variablesObject



38
39
40
41
# File 'lib/ufo/dsl/task_definition.rb', line 38

def load_variables
  load_variables_file("base")
  load_variables_file(Ufo.env)
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.


63
64
65
66
# File 'lib/ufo/dsl/task_definition.rb', line 63

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

#source(name) ⇒ Object

at this point instance_eval has been called and source has possibly been called



69
70
71
# File 'lib/ufo/dsl/task_definition.rb', line 69

def source(name)
  @source = name
end

#source_pathObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ufo/dsl/task_definition.rb', line 82

def source_path
  if @source # this means that source has been called
    path = "#{Ufo.root}/.ufo/templates/#{@source}.json.erb"
    check_source_path(path)
  else
    # default source path
    path = File.expand_path("../../templates/default.json.erb", __FILE__)
    puts "#{task_definition_name} template definition using default template: #{path}" unless @options[:mute]
  end
  path
end

#variables(vars = {}) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/ufo/dsl/task_definition.rb', line 73

def variables(vars={})
  vars.each do |var,value|
    if instance_variable_defined?("@#{var}")
      puts "WARNING: The instance variable @#{var} is already used internally with ufo.  Please name you variable another name!"
    end
    instance_variable_set("@#{var}", value)
  end
end