Class: Sfp::Template
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Sfp::Template
- Defined in:
- lib/sfpagent/template.rb
Class Method Summary collapse
-
.render(template, map) ⇒ Object
Render given template string, and then return the result.
-
.render_file(file, map) ⇒ Object
Render given file, and then save the result back to the file.
Instance Method Summary collapse
-
#render(template) ⇒ Object
Render given template string, and then return the result.
-
#render_file(file) ⇒ Object
Render given file, and then save the result back to the file.
Class Method Details
.render(template, map) ⇒ Object
Render given template string, and then return the result
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sfpagent/template.rb', line 30 def self.render(template, map) if map.is_a?(Hash) renderer = ::Sfp::Template.new(map) renderer.render(template) elsif map.is_a?(OpenStruct) ERB.new(template).result(map.instance_eval { binding }) else raise Exception, 'A Hash or OpenStruct is required!' end end |
Instance Method Details
#render(template) ⇒ Object
Render given template string, and then return the result
8 9 10 |
# File 'lib/sfpagent/template.rb', line 8 def render(template) ERB.new(template).result(binding) end |
#render_file(file) ⇒ Object
Render given file, and then save the result back to the file
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sfpagent/template.rb', line 15 def render_file(file) File.open(file, File::RDWR|File::CREAT) do |f| f.flock(File::LOCK_EX) result = render(f.read) f.rewind f.write(result) f.flush f.truncate(f.pos) end end |