Class: OpenAPISourceTools::Task

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

Overview

Loads template or takes it as argument. Renders template using ERB.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TaskInterface

#system

Constructor Details

#initialize(src, template, template_name) ⇒ Task

Returns a new instance of Task.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openapi/sourcetools/task.rb', line 42

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.



40
41
42
# File 'lib/openapi/sourcetools/task.rb', line 40

def discard
  @discard
end

#executableObject

Returns the value of attribute executable.



40
41
42
# File 'lib/openapi/sourcetools/task.rb', line 40

def executable
  @executable
end

#nameObject

Returns the value of attribute name.



40
41
42
# File 'lib/openapi/sourcetools/task.rb', line 40

def name
  @name
end

#srcObject (readonly)

Returns the value of attribute src.



39
40
41
# File 'lib/openapi/sourcetools/task.rb', line 39

def src
  @src
end

#templateObject (readonly)

Returns the value of attribute template.



39
40
41
# File 'lib/openapi/sourcetools/task.rb', line 39

def template
  @template
end

#template_nameObject (readonly)

Returns the value of attribute template_name.



39
40
41
# File 'lib/openapi/sourcetools/task.rb', line 39

def template_name
  @template_name
end

#xObject

Returns the value of attribute x.



40
41
42
# File 'lib/openapi/sourcetools/task.rb', line 40

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.



71
72
73
74
75
76
77
78
# File 'lib/openapi/sourcetools/task.rb', line 71

def generate(context_binding)
  n = @template_name.nil? ? '' : "#{@template_name} "
  internal_generate(context_binding)
rescue SyntaxError => e
  OpenAPISourceTools::Common.aargh("Template #{n}syntax error: #{e.full_message}", 5)
rescue Exception => e # Some unexpected error.
  OpenAPISourceTools::Common.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.



65
66
67
# File 'lib/openapi/sourcetools/task.rb', line 65

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.



81
82
83
84
85
86
87
# File 'lib/openapi/sourcetools/task.rb', line 81

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