Class: Mumukit::Core::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, variables) ⇒ Template

Returns a new instance of Template.



4
5
6
7
8
9
10
# File 'lib/template.rb', line 4

def initialize(path, variables)
  @path = path
  variables.each do |key, value|
    instance_variable_set "@#{key}", value
    self.singleton_class.class_eval { attr_reader key }
  end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



2
3
4
# File 'lib/template.rb', line 2

def path
  @path
end

Instance Method Details

#renderObject



12
13
14
# File 'lib/template.rb', line 12

def render
  template_file.result binding
end

#with_tempfile!(prefix) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/template.rb', line 20

def with_tempfile!(prefix)
  file = Tempfile.new(prefix)
  write! file
  yield file
ensure
  file.unlink
  file.close
end

#write!(file) ⇒ Object



16
17
18
# File 'lib/template.rb', line 16

def write!(file)
  File.write file, render
end