Module: Slack::Mixins::Proc

Defined in:
lib/slack.rb

Instance Method Summary collapse

Instance Method Details

#check_exception(exception = Exception) ⇒ Object

This method creates a new Speck::Check initialized to pass if the receiver can be executed without exception being raised, or fail if the receiver raises exception when raised.

The passed exception object will be rescued from within the block, but any other exception raised by the block will not be caught.

Raises:

  • (Exception::NoEnvironment)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/slack.rb', line 65

def check_exception exception = Exception
  # TODO: Should we allow specks in the root environment? Could be useful
  #       for quick checks…
  raise Exception::NoEnvironment unless Speck.current
  
  file, line, _ = Kernel::caller.first.split(':')
  source = File.open(file).readlines[line.to_i - 1]
  source.strip!
  source = source.partition(".check_exception").first
  # TODO: Get rid of the "->{…}" around the resulting string.
  
  Speck::Check.new(->(){
    begin
      self.call
    rescue exception
      return true
    end
    return false
  }, source)
    .tap {|check| Speck.current.checks << check }
end