Class: Task

Inherits:
Object
  • Object
show all
Includes:
TaskInterface
Defined in:
lib/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, template, template_name) ⇒ Task

Returns a new instance of Task.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/task.rb', line 34

def initialize(src, template, template_name)
  @src = src
  @template = template
  @template_name = template_name
  if @template.nil?
    raise ArgumentError, "template_name or template must be given" if @template_name.nil?
    begin
      @template = File.read(@template_name)
    rescue Errno::ENOENT
      raise StandardError, "Could not load #{@template_name}"
    rescue StandardError => e
      raise StandardError, "#{e}\nFailed to read #{@template_name}"
    end
  end
  @name = nil
  @executable = false
  @discard = false
  @x = nil
end

Instance Attribute Details

#discardObject

Returns the value of attribute discard.



32
33
34
# File 'lib/task.rb', line 32

def discard
  @discard
end

#executableObject

Returns the value of attribute executable.



32
33
34
# File 'lib/task.rb', line 32

def executable
  @executable
end

#nameObject

Returns the value of attribute name.



32
33
34
# File 'lib/task.rb', line 32

def name
  @name
end

#srcObject (readonly)

Returns the value of attribute src.



31
32
33
# File 'lib/task.rb', line 31

def src
  @src
end

#templateObject (readonly)

Returns the value of attribute template.



31
32
33
# File 'lib/task.rb', line 31

def template
  @template
end

#template_nameObject (readonly)

Returns the value of attribute template_name.



31
32
33
# File 'lib/task.rb', line 31

def template_name
  @template_name
end

#xObject

Returns the value of attribute x.



32
33
34
# File 'lib/task.rb', line 32

def x
  @x
end

Instance Method Details

#generate(context_binding) ⇒ Object

You can override this instead of internal_generate if you do not need the exception handling.



63
64
65
66
67
68
69
70
# File 'lib/task.rb', line 63

def generate(context_binding)
  n = @template_name.nil? ? '' : "#{@template_name} "
  internal_generate(context_binding)
rescue SyntaxError => e
  aargh("Template #{n}syntax error: #{e.full_message}", 5)
rescue Exception => e
  aargh("Template #{n}error: #{e.full_message}", 6)
end

#internal_generate(context_binding) ⇒ Object

If this is overridden to perform some processing but not to produce output, set @discard = true and return value will be ignored. No other methods are called in that case.



57
58
59
# File 'lib/task.rb', line 57

def internal_generate(context_binding)
  ERB.new(@template).result(context_binding)
end

#output_nameObject

This is only called when generate produced output that is not discarded.



73
74
75
76
77
78
79
# File 'lib/task.rb', line 73

def output_name
  return @name unless @name.nil?
  # Using template name may show where name assignment is missing.
  # Name assignment may also be missing in the task creation stage.
  return File.basename(@template_name) unless @template_name.nil?
  nil
end