Class: GDK::Templates::ErbRenderer
- Inherits:
-
Object
- Object
- GDK::Templates::ErbRenderer
- 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
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#initialize(source, **locals) ⇒ ErbRenderer
constructor
Initialize the renderer providing source, target and local variables.
-
#render(target) ⇒ Object
Render template into target file.
-
#render_to_string ⇒ String
Render template and return its content.
-
#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.
Constructor Details
#initialize(source, **locals) ⇒ ErbRenderer
Initialize the renderer providing source, target and local variables
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
#context ⇒ Object (readonly)
Returns the value of attribute context.
13 14 15 |
# File 'lib/gdk/templates/erb_renderer.rb', line 13 def context @context end |
#source ⇒ Object (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
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_string ⇒ String
Render template and return its 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.}.", 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
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 |