lib/erb_asterisk.rb in erb_asterisk-0.0.14 vs lib/erb_asterisk.rb in erb_asterisk-0.0.15
- old
+ new
@@ -6,18 +6,20 @@
require 'erb_asterisk/inclusion'
require 'erb_asterisk/yields'
require 'erb_asterisk/utils'
require 'erb_asterisk/log'
require 'erb_asterisk/file_cache'
+require 'erb_asterisk/soft_write'
module ErbAsterisk
include Render
include Inclusion
include Yields
include Utils
include Log
include FileCache
+ include SoftWrite
def execute(opts)
init_instance(opts)
load_project_file
@@ -68,11 +70,11 @@
# Second round replaces yield_actual with accumulated apply_line_to.
log_debug('SECOND ROUND:')
render_erbs(erbs)
log_debug('')
- save_erbs(erbs)
+ save_configs(erbs)
export_includes(root)
end
def load_erbs(root)
erbs = {}
@@ -92,33 +94,38 @@
def render_erbs(erbs)
erbs.each do |file, value|
# Skip on second round all erbs without yield_here method
next if value[:skip]
- # Declare global variable with current erb file name for include_to method:
- TOPLEVEL_BINDING.local_variable_set(:current_conf_file, value[:config])
+ # Declare variable with current erb file name for include_to method:
+ @current_conf_file = value[:config]
log_debug("ERB: #{file}", 1)
@yield_here_occured = false
value[:content] = new_erb(value[:content]).result
value[:skip] = true unless @yield_here_occured
end
end
- def save_erbs(erbs)
- erbs.each { |_, value| File.write(value[:config], value[:content]) }
+ def save_configs(erbs)
+ erbs.each do |_, value|
+ config = value[:config]
+ next unless soft_write(config, value[:content])
+ log_debug("save_configs: #{config}")
+ end
end
def export_includes(root)
@exports.each do |include_file, content|
content = content.sort_by { |i| -i[:priority] }
result = content.reduce('') do |s, i|
s << "; priority: #{i[:priority]}\n" if i[:priority] != 0
s << "#include \"#{i[:file].sub(root, '')}\"\n"
end
- File.write("#{root}#{include_file}", result)
- log_debug("export_includes: #{include_file}")
+ f = "#{root}#{include_file}"
+ next unless soft_write(f, result)
+ log_debug("export_includes: #{f}")
end
end
def read_template(template)
file_name = "#{template}.erb"