Class: Amp::Support::FileTemplate

Inherits:
Template show all
Defined in:
lib/amp/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(name, file, renderer = nil) ⇒ FileTemplate

Initializes a new FileTemplate with different

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
142
# File 'lib/amp/templates/template.rb', line 135

def initialize(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(name, renderer, File.read(file))
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



131
132
133
# File 'lib/amp/templates/template.rb', line 131

def file
  @file
end

Instance Method Details

#save!Object



144
145
146
# File 'lib/amp/templates/template.rb', line 144

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