try-catch 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84f2bba2b110f675a2f70f1ff631d8c5d180d13a
4
+ data.tar.gz: e1a3891ab124ac5c24096806fc1bb0b4f636c1e5
5
+ SHA512:
6
+ metadata.gz: d816e1e719538e3270a1bddaffe5a673b6c8253e41d03ba623d36994da58d564b89fb9df26dfa97646726d0d687c6aa904fb8245b334a520b2d78a473df958d9
7
+ data.tar.gz: 6203816e7dd020974de6686e9ed63f846f277aa5fde402c7cfee841a7e84ceeaa05a9a37f50ee2fc1381b4d8b730ff28e015213dc5403ca583391d4c9cb457d5
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ try-catch (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ rake (10.2.2)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler
16
+ rake
17
+ try-catch!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Adam Luzsi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,50 @@
1
+ try-catch
2
+ =========
3
+
4
+ try catch blocks for Ruby!
5
+
6
+
7
+ ### Try catch Block Chain use case
8
+
9
+ if you new to ruby and missing the try catch block here is a simple one for you
10
+
11
+
12
+ ```ruby
13
+
14
+ require 'try-catch'
15
+
16
+ try { hello world }.catch{ "not hello world " }
17
+ #> "not hello world"
18
+
19
+ try { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
20
+ #> there is and error, because undefined method `asdaf' for "hello world":String
21
+
22
+ #> you can cain up multiple catch for specific error
23
+ try { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
24
+
25
+ puts try { "hello world".asdaf }.catch(ArgumentError) { "it was and argument error" }.catch( NoMethodError ) { |ex| "bla bla #{ex}" }
26
+ #> "bla bla undefined method `asdaf' for "hello world":String"
27
+
28
+ ```
29
+
30
+ ### The each line style
31
+
32
+ ```ruby
33
+
34
+ try { puts "hello world".asdf }
35
+ catch { |ex| puts ex.to_s.upcase }
36
+ #> UNDEFINED METHOD `ASDF' FOR "HELLO WORLD":STRING
37
+
38
+ ```
39
+
40
+ or in each line version with specific Exception case
41
+
42
+ ```ruby
43
+
44
+ try { puts "hello world".asdf }
45
+ catch(ArgumentError) { |ex| puts ex.to_s.capitalize }
46
+ catch(NoMethodError) { |ex| puts ex.to_s.upcase }
47
+
48
+ ```
49
+
50
+ ## Happy Coding!
@@ -0,0 +1 @@
1
+ require File.join "bundler","gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,24 @@
1
+ ### Get Files from dir
2
+ begin
3
+
4
+ module TryCatch
5
+ @@spec_files = []
6
+ end
7
+
8
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),"**","*"))].sort.uniq.each do |one_file_name|
9
+
10
+ one_file_name = File.expand_path one_file_name
11
+ file_name = one_file_name[(File.expand_path(File.dirname(__FILE__)).to_s.length+1)..(one_file_name.length-1)]
12
+
13
+ if !one_file_name.include?("pkg")
14
+ if !File.directory? file_name
15
+
16
+ TryCatch.class_variable_get("@@spec_files").push file_name
17
+ STDOUT.puts file_name if $DEBUG
18
+
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,117 @@
1
+ module TryCatch
2
+ module EXT; end
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
@@ -0,0 +1,3 @@
1
+ require_relative '../lib/try-catch'
2
+
3
+ catch(NoMethodError) { |ex| puts ex.to_s.upcase }
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__),"files.rb"))
4
+
5
+ ### Specification for the new Gem
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = "try-catch"
9
+ spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
10
+ spec.authors = ["Adam Luzsi"]
11
+ spec.email = ["adamluzsi@gmail.com"]
12
+ spec.description = %q{ try catch blocks for Ruby! }
13
+ spec.summary = %q{ try catch blocks for Ruby! }
14
+ spec.homepage = "https://github.com/adamluzsi/try-catch"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = TryCatch.class_variable_get("@@spec_files")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ %W[ rake bundler ].each{ |gem_name| spec.add_development_dependency(gem_name) }
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: try-catch
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Luzsi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: " try catch blocks for Ruby! "
42
+ email:
43
+ - adamluzsi@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - VERSION
54
+ - files.rb
55
+ - lib/try-catch.rb
56
+ - test/try_catch.rb
57
+ - try-catch.gemspec
58
+ homepage: https://github.com/adamluzsi/try-catch
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: try catch blocks for Ruby!
82
+ test_files:
83
+ - test/try_catch.rb