Class: Sambot::Template
- Inherits:
-
Object
- Object
- Sambot::Template
- Defined in:
- lib/sambot/template.rb
Instance Method Summary collapse
- #evaluate(context = {}, opts = {}) ⇒ Object
-
#initialize(filename) ⇒ Template
constructor
A new instance of Template.
- #path ⇒ Object
- #process(opts) ⇒ Object
- #write(ctx, dest) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Template
Returns a new instance of Template.
6 7 8 |
# File 'lib/sambot/template.rb', line 6 def initialize(filename) @filename = filename end |
Instance Method Details
#evaluate(context = {}, opts = {}) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/sambot/template.rb', line 14 def evaluate(context = {}, opts = {}) input = File.read(path) input = yield(input) if block_given? eruby = Erubis::Eruby.new(input, opts) eruby.evaluate(context) end |
#path ⇒ Object
10 11 12 |
# File 'lib/sambot/template.rb', line 10 def path File.(File.join(File.dirname(__FILE__), 'templates', @filename)) end |
#process(opts) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sambot/template.rb', line 21 def process(opts) File.delete(opts[:dest]) if File.exist?(opts[:dest]) if opts[:eruby] UI.debug("Parsing #{path} using Erubis") write(opts, opts[:dest]) else FileUtils.cp(path, opts[:dest].to_s) end if File.executable?(path) UI.debug("Making #{opts[:dest]} executable") make_executable(opts[:dest]) end end |
#write(ctx, dest) ⇒ Object
35 36 37 38 |
# File 'lib/sambot/template.rb', line 35 def write(ctx, dest) contents = evaluate(ctx) File.write(dest, contents) end |