Class: Capistrano::Template::Helpers::Renderer

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/capistrano/template/helpers/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, context, reader: File, locals: {}) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
10
11
12
13
# File 'lib/capistrano/template/helpers/renderer.rb', line 7

def initialize(from, context, reader: File, locals: {})
  super context

  self.from = from
  self.reader = reader
  self.locals = locals
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/capistrano/template/helpers/renderer.rb', line 29

def method_missing(m, *args, &block)
  if locals.key?(m)
    locals[m]
  else
    super
  end
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



5
6
7
# File 'lib/capistrano/template/helpers/renderer.rb', line 5

def from
  @from
end

#localsObject

Returns the value of attribute locals.



5
6
7
# File 'lib/capistrano/template/helpers/renderer.rb', line 5

def locals
  @locals
end

#readerObject

Returns the value of attribute reader.



5
6
7
# File 'lib/capistrano/template/helpers/renderer.rb', line 5

def reader
  @reader
end

Instance Method Details

#as_ioObject



25
26
27
# File 'lib/capistrano/template/helpers/renderer.rb', line 25

def as_io
  StringIO.new(as_str)
end

#as_strObject



21
22
23
# File 'lib/capistrano/template/helpers/renderer.rb', line 21

def as_str
  @rendered_template ||= ERB.new(template_content, nil, '-').result(binding)
end

#indented_content(content, indent) ⇒ Object



44
45
46
# File 'lib/capistrano/template/helpers/renderer.rb', line 44

def indented_content(content, indent)
  content.split("\n").map { |line| "#{' ' * indent}#{line}" }.join("\n")
end

#render(from, indent: 0, locals: {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/capistrano/template/helpers/renderer.rb', line 37

def render(from, indent: 0, locals: {})
  template = template_file(from)
  content  = Renderer.new(template, self, reader: reader, locals: self.locals.merge(locals)).as_str

  indented_content(content, indent)
end

#respond_to_missing?(m, include_private) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/capistrano/template/helpers/renderer.rb', line 48

def respond_to_missing?(m, include_private)
  if locals.key?(m)
    true
  else
    super
  end
end