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

Version Path
geoff-0.2.6 lib/geoff/dsl_exception_handling.rb
geoff-0.2.5 lib/geoff/dsl_exception_handling.rb
geoff-0.2.3 lib/geoff/dsl_exception_handling.rb
geoff-0.2.2 lib/geoff/dsl_exception_handling.rb
geoff-0.2.1 lib/geoff/dsl_exception_handling.rb
geoff-0.2.0 lib/geoff/dsl_exception_handling.rb
geoff-0.1.2 lib/geoff/dsl_exception_handling.rb
geoff-0.1.1 lib/geoff/dsl_exception_handling.rb
geoff-0.1.0 lib/geoff/dsl_exception_handling.rb
geoff-0.0.8 lib/geoff/dsl_exception_handling.rb
geoff-0.0.7 lib/geoff/dsl_exception_handling.rb
geoff-0.0.6 lib/geoff/dsl_exception_handling.rb
geoff-0.0.5 lib/geoff/dsl_exception_handling.rb
geoff-0.0.4 lib/geoff/dsl_exception_handling.rb
geoff-0.0.3.beta lib/geoff/dsl_exception_handling.rb