Module: DslExceptionHandling

Included in:
ChildrenDsl, NodeDsl
Defined in:
lib/geoff/dsl_exception_handling.rb

Instance Method Summary collapse

Instance Method Details

#eval_with_exceptions(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/geoff/dsl_exception_handling.rb', line 10

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

#lines_around(file, number, context = 10) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/geoff/dsl_exception_handling.rb', line 34

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

#write_modeObject

prevent very confusing method_missing calls during debugging by only writing to the object when expected



4
5
6
7
8
# File 'lib/geoff/dsl_exception_handling.rb', line 4

def write_mode
  @write_mode = true
  yield
  @write_mode = false
end