Class: SafeBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/safeblock/version.rb,
lib/safeblock/safeblock.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.configurationObject



29
30
31
# File 'lib/safeblock/safeblock.rb', line 29

def self.configuration
  @configuration
end

.setup(&block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/safeblock/safeblock.rb', line 33

def self.setup(&block)
  @configuration = SafeBlock::Configuration.new

  block.call(@configuration)

  @configuration = @configuration.to_hash
end

.start(options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/safeblock/safeblock.rb', line 4

def self.start(options = {}, &block)

  setup{} if @configuration.nil?

  options = @configuration.merge(options)

  proc = Proc.new do |options|
    Timeout::timeout(options[:timeout]) do
      begin
        block.call
      rescue Exception => e
        raise e if options[:ignore_exception]
        options[:rescue_block].call(e) if options[:rescue_block].is_a? Proc
      end
    end
  end

  if options[:threaded]
    Thread.new{ proc.call(options) }
  else
    proc.call(options)
  end

end