try-catch 1.0.0 → 1.2.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +9 -1
- data/VERSION +1 -1
- data/lib/try-catch.rb +3 -117
- data/lib/try-catch/functions.rb +64 -0
- data/lib/try-catch/memory.rb +95 -0
- data/lib/try-catch/syntax.rb +18 -0
- data/test/try_catch.rb +7 -2
- data/try-catch.gemspec +1 -1
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 345103aaa3a3a13455a9efa6c7d8aaab523fdc3b
|
4
|
+
data.tar.gz: d8862c5da29c4fba29311f2cbc7f3ec44df656ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4ef0166e36f785139ff5efbc52c09fff386e0ca83ef83fb1e571381aa25f178cebf005305df66ac9d94ae150548b1933e9ffacf19879e0d0491af8dc4dd6114
|
7
|
+
data.tar.gz: 22dee0fd8c9b6a6b7af555a052c5f7fa015c3d778e32eb8a5664066865f3137a0d4e44004b8e594e21452c98c698b791a9c2e514f71cdc24ce716997df9c293c
|
data/Gemfile.lock
CHANGED
@@ -1,11 +1,18 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
try-catch (1.
|
4
|
+
try-catch (1.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
|
+
columnize (0.3.6)
|
10
|
+
debugger (1.6.6)
|
11
|
+
columnize (>= 0.3.1)
|
12
|
+
debugger-linecache (~> 1.2.0)
|
13
|
+
debugger-ruby_core_source (~> 1.3.2)
|
14
|
+
debugger-linecache (1.2.0)
|
15
|
+
debugger-ruby_core_source (1.3.2)
|
9
16
|
rake (10.2.2)
|
10
17
|
|
11
18
|
PLATFORMS
|
@@ -13,5 +20,6 @@ PLATFORMS
|
|
13
20
|
|
14
21
|
DEPENDENCIES
|
15
22
|
bundler
|
23
|
+
debugger
|
16
24
|
rake
|
17
25
|
try-catch!
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/try-catch.rb
CHANGED
@@ -1,117 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
module Memory
|
5
|
-
@@memoryspace = {}
|
6
|
-
class << self
|
7
|
-
|
8
|
-
def save object_id, exception
|
9
|
-
|
10
|
-
begin
|
11
|
-
|
12
|
-
unless exception.class <= Exception
|
13
|
-
raise ArgumentError, "invalid exception attempt to be saved"
|
14
|
-
end
|
15
|
-
|
16
|
-
@@memoryspace[object_id] ||= []
|
17
|
-
|
18
|
-
if @@memoryspace[object_id].count >= 100
|
19
|
-
@@memoryspace[object_id].shift
|
20
|
-
end
|
21
|
-
|
22
|
-
@@memoryspace[object_id].push exception
|
23
|
-
|
24
|
-
return true
|
25
|
-
rescue ArgumentError
|
26
|
-
return false
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
#loaded_exs= @@memoryspace[object_id]
|
33
|
-
#return nil if loaded_exs.nil?
|
34
|
-
#return loaded_exs.pop unless loaded_exs.class != Array
|
35
|
-
def load object_id
|
36
|
-
( @@memoryspace[object_id] || [] ).last
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
module Function
|
44
|
-
class << self
|
45
|
-
|
46
|
-
def try( self_object, *exception_classes, &block )
|
47
|
-
|
48
|
-
exception_classes.each do |exception_class|
|
49
|
-
unless exception_class <= Exception
|
50
|
-
raise ArgumentError, "invalid Exception: #{exception_class}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
exception_classes.push(Exception) if exception_classes.empty?
|
55
|
-
|
56
|
-
begin
|
57
|
-
return block.call
|
58
|
-
rescue *exception_classes => ex
|
59
|
-
TryCatch::Memory.save( self_object.object_id, ex )
|
60
|
-
return ex
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
def catch( self_object, *exception_classes, &block )
|
66
|
-
|
67
|
-
exception_classes.each do |exception_class|
|
68
|
-
unless exception_class <= Exception
|
69
|
-
raise ArgumentError, "invalid Exception: #{exception_class}"
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
exception_classes.push(Exception) if exception_classes.empty?
|
74
|
-
|
75
|
-
if self_object.class <= Exception
|
76
|
-
exception_obj= self_object
|
77
|
-
else
|
78
|
-
exception_obj= TryCatch::Memory.load( self_object.object_id )
|
79
|
-
end
|
80
|
-
|
81
|
-
return nil if exception_obj.nil?
|
82
|
-
return exception_obj if block.nil?
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
if exception_classes.map{ |exc| exception_obj.class <= exc }.include?(true)
|
87
|
-
|
88
|
-
if block.parameters.empty?
|
89
|
-
return block.call
|
90
|
-
else
|
91
|
-
return block.call exception_obj
|
92
|
-
end
|
93
|
-
|
94
|
-
else
|
95
|
-
return exception_obj
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
module EXT::Object
|
104
|
-
|
105
|
-
def try *exception_classes, &block
|
106
|
-
::TryCatch::Function.try self, *exception_classes, &block
|
107
|
-
end
|
108
|
-
|
109
|
-
def catch *exception_classes, &block
|
110
|
-
::TryCatch::Function.catch self, *exception_classes, &block
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
Object.__send__ :include, TryCatch::EXT::Object
|
1
|
+
require 'try-catch/memory'
|
2
|
+
require 'try-catch/functions'
|
3
|
+
require 'try-catch/syntax'
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module TryCatch
|
2
|
+
|
3
|
+
module Function
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def try( self_object, *exception_classes, &block )
|
7
|
+
|
8
|
+
exception_classes.each do |exception_class|
|
9
|
+
unless exception_class <= Exception
|
10
|
+
raise ArgumentError, "invalid Exception: #{exception_class}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
exception_classes.push(Exception) if exception_classes.empty?
|
15
|
+
|
16
|
+
begin
|
17
|
+
return block.call
|
18
|
+
rescue *exception_classes => ex
|
19
|
+
TryCatch::Memory.save( self_object.object_id, ex )
|
20
|
+
TryCatch::Memory.memory_agent
|
21
|
+
|
22
|
+
return ex
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def catch( self_object, *exception_classes, &block )
|
29
|
+
|
30
|
+
exception_classes.each do |exception_class|
|
31
|
+
unless exception_class <= Exception
|
32
|
+
raise ArgumentError, "invalid Exception: #{exception_class}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
exception_classes.push(Exception) if exception_classes.empty?
|
37
|
+
|
38
|
+
if self_object.class <= Exception
|
39
|
+
exception_obj= self_object
|
40
|
+
else
|
41
|
+
exception_obj= TryCatch::Memory.load( self_object.object_id )
|
42
|
+
end
|
43
|
+
|
44
|
+
return nil if exception_obj.nil?
|
45
|
+
return exception_obj if block.nil?
|
46
|
+
|
47
|
+
if exception_classes.map{ |exc| exception_obj.class <= exc }.include?(true)
|
48
|
+
|
49
|
+
if block.parameters.empty?
|
50
|
+
return block.call
|
51
|
+
else
|
52
|
+
return block.call exception_obj
|
53
|
+
end
|
54
|
+
|
55
|
+
else
|
56
|
+
return exception_obj
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module TryCatch
|
2
|
+
|
3
|
+
module Memory
|
4
|
+
@@memoryspace = {}
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def init_memory_allocation opts={}
|
8
|
+
raise(ArgumentError) unless opts.class <= Hash
|
9
|
+
|
10
|
+
opts[:thread] ||= opts.delete(:t) || Thread.current
|
11
|
+
opts[:object_id] ||= opts.delete(:o) || opts.delete(:object)
|
12
|
+
|
13
|
+
@@memoryspace[opts[:thread]] ||= {}
|
14
|
+
unless opts[:object_id].nil?
|
15
|
+
@@memoryspace[opts[:thread]][opts[:object_id]] ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
return nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def save object_id, exception
|
22
|
+
|
23
|
+
begin
|
24
|
+
|
25
|
+
init_memory_allocation o: object_id
|
26
|
+
@@memoryspace[Thread.current][object_id].push exception
|
27
|
+
|
28
|
+
return true
|
29
|
+
rescue ArgumentError
|
30
|
+
return false
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
#loaded_exs= @@memoryspace[object_id]
|
36
|
+
#return nil if loaded_exs.nil?
|
37
|
+
#return loaded_exs.pop unless loaded_exs.class != Array
|
38
|
+
def load object_id
|
39
|
+
|
40
|
+
return nil if @@memoryspace[Thread.current].nil?
|
41
|
+
return @@memoryspace[Thread.current][object_id].last
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def pop object_id
|
46
|
+
|
47
|
+
return nil if @@memoryspace[Thread.current].nil?
|
48
|
+
|
49
|
+
unless @@memoryspace[Thread.current][object_id].nil?
|
50
|
+
@@memoryspace[Thread.current][object_id].pop
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def clean_memory_by_object_id object_id, thread= Thread.current
|
56
|
+
|
57
|
+
init_memory_allocation t: thread, o: object_id
|
58
|
+
if @@memoryspace[thread][object_id].count >= 20
|
59
|
+
( @@memoryspace[thread][object_id].count - 10 ).times{ @@memoryspace[thread][object_id].shift }
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
def clean_thread_memory thread= Thread.current
|
65
|
+
|
66
|
+
init_memory_allocation t: thread
|
67
|
+
@@memoryspace[thread].keys.each{ |object_id| clean_memory_by_object_id(object_id) }
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
def memory_clean
|
72
|
+
Thread.list.each{ |thr| clean_thread_memory(thr) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def memory_agent
|
76
|
+
|
77
|
+
@@memory_agent ||= nil
|
78
|
+
if @@memory_agent.nil?
|
79
|
+
|
80
|
+
@@memory_agent= ::Thread.new{
|
81
|
+
loop{
|
82
|
+
sleep(60)
|
83
|
+
Memory.memory_clean
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
end
|
88
|
+
return @@memory_agent
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module TryCatch
|
2
|
+
|
3
|
+
EXT= Module.new
|
4
|
+
module EXT::Object
|
5
|
+
|
6
|
+
def try *exception_classes, &block
|
7
|
+
::TryCatch::Function.try self, *exception_classes, &block
|
8
|
+
end
|
9
|
+
|
10
|
+
def catch *exception_classes, &block
|
11
|
+
::TryCatch::Function.catch self, *exception_classes, &block
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
Object.__send__ :include, TryCatch::EXT::Object
|
data/test/try_catch.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
-
require_relative '../lib/try-catch'
|
1
|
+
require_relative '../lib/try-catch/memory'
|
2
|
+
require_relative '../lib/try-catch/functions'
|
3
|
+
require_relative '../lib/try-catch/syntax'
|
2
4
|
|
3
|
-
|
5
|
+
require 'debugger'
|
6
|
+
|
7
|
+
try { puts("hello world".z) }
|
8
|
+
catch(NoMethodError) { |ex| puts ex.to_s.upcase }
|
data/try-catch.gemspec
CHANGED
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
%W[ rake bundler ].each{ |gem_name| spec.add_development_dependency(gem_name) }
|
22
|
+
%W[ rake bundler debugger ].each{ |gem_name| spec.add_development_dependency(gem_name) }
|
23
23
|
|
24
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: try-catch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: debugger
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: " try catch blocks for Ruby! "
|
42
56
|
email:
|
43
57
|
- adamluzsi@gmail.com
|
@@ -53,6 +67,9 @@ files:
|
|
53
67
|
- VERSION
|
54
68
|
- files.rb
|
55
69
|
- lib/try-catch.rb
|
70
|
+
- lib/try-catch/functions.rb
|
71
|
+
- lib/try-catch/memory.rb
|
72
|
+
- lib/try-catch/syntax.rb
|
56
73
|
- test/try_catch.rb
|
57
74
|
- try-catch.gemspec
|
58
75
|
homepage: https://github.com/adamluzsi/try-catch
|