Class: Ufo::DSL::TaskDefinition
- Inherits:
-
Object
- Object
- Ufo::DSL::TaskDefinition
- Defined in:
- lib/ufo/dsl/task_definition.rb
Instance Attribute Summary collapse
-
#task_definition_name ⇒ Object
readonly
Returns the value of attribute task_definition_name.
Instance Method Summary collapse
- #build ⇒ Object
-
#build_error_info(e) ⇒ Object
Provide a slightly better error message to user when the task definition code block is not evaluated successfully.
- #check_source_path(path) ⇒ Object
-
#copy_instance_variables ⇒ Object
Copy the instance variables from TemplateScope to TaskDefinition so that config/variables are available in the task_definition blocks also.
-
#helper ⇒ Object
delegate helper method back up to dsl.
-
#initialize(dsl, task_definition_name, options = {}, &block) ⇒ TaskDefinition
constructor
A new instance of TaskDefinition.
-
#source(name) ⇒ Object
At this point instance_eval has been called and source has been possibly called.
- #source_path ⇒ Object
- #template_scope ⇒ Object
- #template_scope_instance_variable?(var) ⇒ Boolean
- #variables(vars = {}) ⇒ Object
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, ={}, &block) @dsl = dsl @task_definition_name = task_definition_name @block = block @options = end |
Instance Attribute Details
#task_definition_name ⇒ Object (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
#build ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ufo/dsl/task_definition.rb', line 24 def build copy_instance_variables begin instance_eval(&@block) rescue Exception => e build_error_info(e) raise end RenderMePretty.result(source_path, context: template_scope) end |
#build_error_info(e) ⇒ Object
Provide a slightly better error message to user when the task definition code block is not evaluated successfully.
37 38 39 40 41 42 43 44 |
# File 'lib/ufo/dsl/task_definition.rb', line 37 def build_error_info(e) puts "ERROR: evaluating block for task_definition #{@task_definition_name}".color(:red) # The first line of the backtrace has the info of the file name. Example: # ./.ufo/task_definitions.rb:24:in `block in evaluate_template_definitions' info = e.backtrace[0] filename = info.split(':')[0..1].join(':') puts "Filename: #{filename}".color(:red) end |
#check_source_path(path) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ufo/dsl/task_definition.rb', line 96 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 |
#copy_instance_variables ⇒ Object
Copy the instance variables from TemplateScope to TaskDefinition so that config/variables are available in the task_definition blocks also. Example:
task_definition "my-app" do
# make config/variables available here also
end
This allows possible collision but think it is worth it to have variables available.
57 58 59 60 61 62 |
# File 'lib/ufo/dsl/task_definition.rb', line 57 def copy_instance_variables template_scope.instance_variables.each do |var| val = template_scope.instance_variable_get(var) instance_variable_set(var, val) end end |
#helper ⇒ Object
delegate helper method back up to dsl
16 17 18 |
# File 'lib/ufo/dsl/task_definition.rb', line 16 def helper @dsl.helper end |
#source(name) ⇒ Object
At this point instance_eval has been called and source has been possibly called
65 66 67 |
# File 'lib/ufo/dsl/task_definition.rb', line 65 def source(name) @source = name end |
#source_path ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ufo/dsl/task_definition.rb', line 84 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.("../../default/templates/main.json.erb", __FILE__) puts "#{task_definition_name} template definition using default template: #{path}" unless @options[:mute] end path end |
#template_scope ⇒ Object
20 21 22 |
# File 'lib/ufo/dsl/task_definition.rb', line 20 def template_scope @template_scope ||= Ufo::TemplateScope.new(helper, @task_definition_name) end |
#template_scope_instance_variable?(var) ⇒ Boolean
80 81 82 |
# File 'lib/ufo/dsl/task_definition.rb', line 80 def template_scope_instance_variable?(var) template_scope.instance_variables.include?("@#{var}".to_sym) end |
#variables(vars = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ufo/dsl/task_definition.rb', line 69 def variables(vars={}) vars.each do |var,value| # Warn when variable collides with internal variable, but dont warn # template_scope variables collision. if instance_variable_defined?("@#{var}") && !template_scope_instance_variable?(var) puts "WARNING: The instance variable @#{var} is already used internally with ufo. Please name you variable another name!" end template_scope.instance_variable_set("@#{var}", value) end end |