Module: Mulligan::Condition

Included in:
Exception
Defined in:
lib/mulligan/condition.rb

Instance Method Summary collapse

Instance Method Details

#has_recovery?(id) ⇒ Boolean

Checks for the presence of a recovery

Parameters:

  • id (String or Symbol)

    the key for the recovery

Returns:

  • (Boolean)

    whether or not a recovery exists for this id



36
37
38
# File 'lib/mulligan/condition.rb', line 36

def has_recovery?(id)
  recoveries.has_key?(id.to_sym)
end

#recover(id, *params) ⇒ Object

Executes the recovery. This actually places the stack back to just after the ‘#raise` that brought us to this `rescue` clause. Then the recovery block is executed and the program continues on.

Parameters:

  • id (String or Symbol)

    the key for the recovery

  • params

    any additional parameters you want to pass to the recovery block

Returns:

  • This doesn’t actually matter because you can’t retrieve it

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mulligan/condition.rb', line 64

def recover(id, *params)
  raise Mulligan::UnsupportedException unless Mulligan.supported?

  Thread.current[:__last_recovery__] = nil
  raise ControlException unless has_recovery?(id.to_sym)
  data = recoveries[id.to_sym]
  if data[:continuation].nil?
    $stderr.puts "Cannot invoke restart #{id}. Must first raise this exception: '#{self.inspect}'"
    return
  end
  Thread.current[:__last_recovery__] = id
  data[:continuation].call(*data[:block].call(*params))
  true
end

#recovery_identifiersEnumerable

Retrieves all the identifiers for available recoveries

Returns:

  • (Enumerable)

    The identifiers for all the recoveries



52
53
54
# File 'lib/mulligan/condition.rb', line 52

def recovery_identifiers
  recoveries.keys
end

#recovery_options(id) ⇒ Hash

Retrieves the options specified when a recovery was made

Parameters:

  • id (String or Symbol)

    the key for the recovery

Returns:

  • (Hash)

    The options set on this recovery



44
45
46
47
# File 'lib/mulligan/condition.rb', line 44

def recovery_options(id)
  return nil unless has_recovery?(id.to_sym)
  recoveries[id.to_sym].dup.reject{|k,v| [:block, :continuation].include? k}
end

#set_recovery(id, options = {}, &block) ⇒ Object



21
22
23
24
25
# File 'lib/mulligan/condition.rb', line 21

def set_recovery(id, options={}, &block)
  return if block.nil?
  recoveries[id.to_sym] = options.merge(:block => block)
  nil
end