Class: GDK::Templates::ErbRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/gdk/templates/erb_renderer.rb

Overview

ErbRenderer is responsible for rendering templates and providing them access to configuration data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, **locals) ⇒ ErbRenderer

Initialize the renderer providing source, target and local variables

Parameters:

  • source (Pathname)
  • **locals (Hash)

    variables available inside the template



19
20
21
22
# File 'lib/gdk/templates/erb_renderer.rb', line 19

def initialize(source, **locals)
  @source = ensure_pathname(source)
  @context = ::GDK::Templates::Context.new(**locals)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/gdk/templates/erb_renderer.rb', line 13

def context
  @context
end

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/gdk/templates/erb_renderer.rb', line 13

def source
  @source
end

Instance Method Details

#render(target) ⇒ Object

Render template into target file

Parameters:

  • target (Pathname)


57
58
59
60
61
62
63
64
# File 'lib/gdk/templates/erb_renderer.rb', line 57

def render(target)
  target = ensure_pathname(target)

  return unless should_render?(target)

  FileUtils.mkdir_p(target.dirname) # Ensure target's directory exists
  File.write(target, render_to_string)
end

#render_to_stringString

Render template and return its content

Returns:

  • (String)

    Rendered content



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gdk/templates/erb_renderer.rb', line 69

def render_to_string
  raise ArgumentError, "file not found in: #{source}" unless File.exist?(source)

  template = File.read(source)

  erb = ERB.new(template, trim_mode: '-') # A trim_mode of '-' allows omitting empty lines with <%- -%>
  erb.location = source.to_s # define the file location so errors can point to the right file
  erb.result(@context.context_bindings)
rescue GDK::ConfigSettings::UnsupportedConfiguration => e
  GDK::Output.abort("#{e.message}.", e)
end

#safe_render!(target) ⇒ Object

The safe render take extra steps to avoid unrecoverable changes:

  • Render the new content to a temporary file

  • Display a diff of the changes

  • Make a timestamped backup of the target file

  • Provide instructions on how to restore previous changes

  • Move the temporary file to replace the old one

Parameters:

  • target (Pathname)


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

def safe_render!(target)
  target = ensure_pathname(target)

  return unless should_render?(target)

  temp_file = Tempfile.create(target.to_s)
  File.write(temp_file.path, render_to_string)

  if target.exist?
    return if FileUtils.identical?(target, temp_file.path)

    display_changes!(temp_file.path, target)
    backup = perform_backup!(target)
    warn_overwritten!(backup)
  end

  FileUtils.mkdir_p(target.dirname) # Ensure target's directory exists
  FileUtils.mv(temp_file.path, target)
ensure
  temp_file&.close
end