safeblock 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ require 'safeblock/safeblock'
2
+ require 'safeblock/safeblock_module'
3
+
4
+ require 'rubygems'
5
+ require 'ruby-debug'
@@ -0,0 +1,61 @@
1
+ require 'timeout'
2
+
3
+ class SafeBlock
4
+ def self.start(options = {}, &block)
5
+
6
+ setup{} if @configuration.nil?
7
+
8
+ options = @configuration.merge(options)
9
+
10
+ proc = Proc.new do |options|
11
+ Timeout::timeout(options[:timeout]) do
12
+ begin
13
+ block.call
14
+ rescue Exception => e
15
+ raise e if options[:ignore_exception]
16
+ options[:rescue_block].call(e) if options[:rescue_block].is_a? Proc
17
+ end
18
+ end
19
+ end
20
+
21
+ if options[:threaded]
22
+ Thread.new{ proc.call(options) }
23
+ else
24
+ proc.call(options)
25
+ end
26
+
27
+ end
28
+
29
+ def self.configuration
30
+ @configuration
31
+ end
32
+
33
+ def self.setup(&block)
34
+ @configuration = SafeBlock::Configuration.new
35
+
36
+ block.call(@configuration)
37
+
38
+ @configuration = @configuration.to_hash
39
+ end
40
+
41
+ class Configuration
42
+
43
+ VALUES = :timeout, :threaded, :rescue_block, :ignore_exception
44
+ attr_accessor *VALUES
45
+
46
+ def initialize
47
+ @timeout = 0
48
+ @threaded = false
49
+ @ignore_exception = false
50
+ end
51
+
52
+ def to_hash
53
+ hash = {}
54
+ VALUES.each do |value|
55
+ hash[value] = self.send(value)
56
+ end
57
+ hash
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,26 @@
1
+
2
+ module SafeblockModule
3
+
4
+ #add macro to use inside classes
5
+ #the macro must accept a method name and optionally a options hash
6
+ #
7
+ #example of use:
8
+ #
9
+ # rescue_method :my_method, {:threaded => true}
10
+ # rescue_method :my_method, {:timeout => 3}
11
+ # rescue_method :my_method
12
+
13
+ def rescue_method(method_name, options = {})
14
+ alias_method :"old_safeblock_#{method_name}", :"#{method_name}"
15
+
16
+ define_method :"#{method_name}" do |*args,&block|
17
+ SafeBlock.start(options) do
18
+ instance_eval do
19
+ send :"old_safeblock_#{method_name}", *args, &block
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ class SafeBlock
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safeblock
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Alvaro Duran Tovar
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-09 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: The purpose of this gem is to it inside classes to protect methods, or protect some concrete piece of code!
23
+ email: hermesdt@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/safeblock/safeblock.rb
32
+ - lib/safeblock/safeblock_module.rb
33
+ - lib/safeblock/version.rb
34
+ - lib/safeblock.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/hermesdt/SafeBlock
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.6.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Protect your methods or your code blocks!
69
+ test_files: []
70
+