Sha256: a70feb18e6fe08a4d0f504a4e21f71c2c14353ae2418ad83459ea567cd85e0da
Contents?: true
Size: 1.28 KB
Versions: 15
Compression:
Stored size: 1.28 KB
Contents
module DslExceptionHandling #prevent very confusing method_missing calls during debugging by #only writing to the object when expected def write_mode @write_mode = true yield @write_mode = false end def eval_with_exceptions &block write_mode do begin instance_eval(&block) rescue Exception => e #don't swallow exceptions further down the tree raise e if e.is_a? Geoff::DslSyntaxError lines = caller.select{|c| (c !~ /geoff\/lib\/geoff/)and (c !~ /ruby-debug/) } file, number, method = lines.first.split ":" message = "Syntax Error in #{method} in Geoff DSL block for:\n#{file}:#{number}\n" message << lines_around(file, number) do |m| "\n\n#{e.message}\n\n" end raise Geoff::DslSyntaxError, message end end end def lines_around file, number, context = 10 number = number.to_i range = (number - context) .. (number + context) lines = File.read(file).split("\n") filtered_lines = range.inject({}) do |l, i| l[i] = lines[i] l end filtered_lines.map do |line_number, line| if line_number == number yield + ">#{line_number} #{line}" else " #{line_number} #{line}" end end.join "\n" end end
Version data entries
15 entries across 15 versions & 1 rubygems