Class: XCCache::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/xccache/utils/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Template

Returns a new instance of Template.



8
9
10
11
# File 'lib/xccache/utils/template.rb', line 8

def initialize(name)
  @name = name
  @path = ROOT / "lib/xccache/assets/templates/#{name}.template"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/xccache/utils/template.rb', line 6

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/xccache/utils/template.rb', line 6

def path
  @path
end

Instance Method Details

#render(hash = {}, save_to: nil) ⇒ Object

Raises:



13
14
15
16
17
18
19
# File 'lib/xccache/utils/template.rb', line 13

def render(hash = {}, save_to: nil)
  raise GeneralError, "Template not found: #{name}" if path.nil?

  rendered = ERB.new(File.read(@path)).result_with_hash(hash)
  Pathname(save_to).write(rendered) unless save_to.nil?
  rendered
end