Class: Amp::Support::FileTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/amp-core/templates/template.rb

Overview

FileTemplate

Class for loading a template from a file and registering it in the system. Is smart enough that if the filename ends in a known extension, the appropriate renderer will be used. Otherwise, you will have to specify the renderer.

Constant Summary collapse

KNOWN_EXTENSIONS =
["erb", "haml"]

Instance Attribute Summary collapse

Attributes inherited from Template

#name, #renderer, #text

Instance Method Summary collapse

Methods inherited from Template

[], ensure_templates_loaded, load_default_templates, register, #render, templates_loaded?, unregister

Constructor Details

#initialize(type, name, file, renderer = nil) ⇒ FileTemplate

Initializes a new FileTemplate with a file used as input.

The renderer is inferred from the file’s extension.

Raises:

  • (ArgumentError)


151
152
153
154
155
156
157
158
# File 'lib/amp-core/templates/template.rb', line 151

def initialize(type, name, file, renderer = nil)
  if renderer.nil?
    renderer = KNOWN_EXTENSIONS.select {|ext| file.end_with? ext}.first
  end
  raise ArgumentError.new("No renderer specified for #{file.inspect}") if renderer.nil?
  @file = file
  super(type, name, renderer, File.read(file))
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



145
146
147
# File 'lib/amp-core/templates/template.rb', line 145

def file
  @file
end

Instance Method Details

#save!Object



160
161
162
# File 'lib/amp-core/templates/template.rb', line 160

def save!
  File.open(file, "w") { |out| out.write text }
end