lib/erb_asterisk.rb in erb_asterisk-0.0.7 vs lib/erb_asterisk.rb in erb_asterisk-0.0.8

- old
+ new

@@ -2,13 +2,12 @@ require 'find' require 'pathname' module ErbAsterisk # Render template - # TODO: render user defined templates def render(template, vars = {}) - tpl = File.read("#{@templates_path}/#{template}.erb") + tpl = read_template(template) e = ERB.new(tpl) b = TOPLEVEL_BINDING vars.each do |name, value| b.local_variable_set(name, value) @@ -54,12 +53,12 @@ def yield_actual(tag) @yields[tag] end - def execute - init_instance + def execute(opts) + init_instance(opts) load_project_file root = asterisk_root @templates_path = "#{root}templates".freeze @@ -71,14 +70,17 @@ ERB_PROJECT_FILE = './erb_asterisk_project.rb'.freeze ERB_ASTERISK_CONF = 'asterisk.conf'.freeze ERB_ASTERISK_DIR = 'asterisk/'.freeze - def init_instance + def init_instance(opts) @exports = {} @templates_path = '' @yields = {} + + user_path = opts[:templates].nil? ? '~/.erb_asterisk' : opts[:templates] + @user_templates = File.expand_path("#{user_path}/templates") end def asterisk_root return './' if File.exist?(ERB_ASTERISK_CONF) return ERB_ASTERISK_DIR if Dir.exist?(ERB_ASTERISK_DIR) @@ -138,7 +140,18 @@ s << "#include \"#{i[:file].sub(root, '')}\"\n" end File.write("#{root}#{include_file}", result) end + end + + def read_template(template) + file_name = "#{template}.erb" + project_template = "#{@templates_path}/#{file_name}" + return File.read(project_template) if File.exist?(project_template) + + user_template = "#{@user_templates}/#{file_name}" + return File.read(user_template) if File.exist?(user_template) + + raise "Template not found: #{template}" end end