Class: Cond::CondPrivate::Defaults

Inherits:
Object
  • Object
show all
Defined in:
lib/cond/cond_private/defaults.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefaults

Returns a new instance of Defaults.



8
9
10
11
# File 'lib/cond/cond_private/defaults.rb', line 8

def initialize
  @stream_in = STDIN
  @stream_out = STDERR
end

Instance Attribute Details

#stream_inObject

Returns the value of attribute stream_in.



13
14
15
# File 'lib/cond/cond_private/defaults.rb', line 13

def stream_in
  @stream_in
end

#stream_outObject

Returns the value of attribute stream_out.



13
14
15
# File 'lib/cond/cond_private/defaults.rb', line 13

def stream_out
  @stream_out
end

Instance Method Details

#handler(exception) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cond/cond_private/defaults.rb', line 21

def handler(exception)
  stream_out.puts exception.backtrace.last

  if exception.respond_to? :message
    stream_out.puts exception.message, ""
  end
    
  #
  # Show restarts in the order they appear on the stack (via
  # partial differences).
  #
  # grr:
  #
  #   % ruby186 -ve 'p({:x => 33}.eql?({:x => 33}))'
  #   ruby 1.8.6 (2009-03-10 patchlevel 362) [i686-darwin9.6.0]
  #   false
  #
  #   % ruby187 -ve 'p({:x => 33}.eql?({:x => 33}))'
  #   ruby 1.8.7 (2009-03-09 patchlevel 150) [i686-darwin9.6.0]
  #   true
  #
  stack_arrays = Cond.restarts_stack.map { |level|
    level.to_a.sort_by { |t| t.first.to_s }
  }
  restart_names = stack_arrays.to_enum(:each_with_index).map {
    |level, index|
    if index == 0
      level
    else
      level - stack_arrays[index - 1]
    end
  }.map { |level| level.map { |t| t.first } }.flatten

  restart_index = loop {
    restart_names.each_with_index { |name, index|
      func = Cond.available_restarts[name]
      message = (
        if func.respond_to?(:message) and func.message != ""
          func.message + " "
        else
          ""
        end
      )
      stream_out.printf("%3d: %s(%s)\n", index, message, name.inspect)
    }
    stream_out.print "Choose number: "
    stream_out.flush
    input = stream_in.readline.strip
    if input =~ %r!\A\d+\Z! and
        (0...restart_names.size).include?(input.to_i)
      break input.to_i
    end
  }
  Cond.invoke_restart(restart_names[restart_index])
end

#handlersObject



15
16
17
18
19
# File 'lib/cond/cond_private/defaults.rb', line 15

def handlers
  {
    Exception => method(:handler)
  }
end