try_to 1.2 → 1.3

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +104 -72
  3. data/lib/try_to.rb +3 -3
  4. data/lib/try_to/version.rb +1 -1
  5. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a53f923148d5b7feaca7c296d23166c2a3a2ba747345844f84f420c540c40431
4
- data.tar.gz: 163a6c9c3b7e5432749715da57053952ca6ac79fe60b2fe40186597d566dfd3d
3
+ metadata.gz: 9ec1f34ec687f3663ab5210dac26e30f237399f274e1e449c4da8241bbafcf36
4
+ data.tar.gz: 9ac967b5be81ccaacf7736539d409c93b506157dc957016c4cd1426d4d445ee6
5
5
  SHA512:
6
- metadata.gz: 41b63c7643991af44bfe7c44dc716d0b2d35954b827d38fca900243a9258fe93ffee331660ec53bfc69eda4bd266b7b614de7f80573cf25b6feb043384810d69
7
- data.tar.gz: 332ecb1c745af1ff3b26eabf391d3546fae89e9997f0e820cb8a43b4b0c77135afb3e5c770283e9850334b8d1f745288a187b0babdb18aadf13c3f1f45df4440
6
+ metadata.gz: 8293b132f2a40f59c3d636a33c84025f7f177a25227c68602bfb300b69a2c66f3728aa9d7ac628d3e415edbf838e62048635e5d8dd72f83c44a8e3ba02005eba
7
+ data.tar.gz: 6d3516a4dbeb51cf8261bc611a8f347985bdcae00ae912c39a29b71c24de8a3eef519a411ab7af41fc2a386e35252da579a9c4545bf36d423334bd3d9827cf40
data/README.md CHANGED
@@ -12,33 +12,49 @@ but allows for much more sophisticated error handling.
12
12
 
13
13
  Instead of using Rails' `Object#try` like this,
14
14
 
15
- obj1.try(:met1).try(:met2).try(:met3).to_s
15
+ ```ruby
16
+ obj.try(:met1).try(:met2).try(:met3).to_s
17
+ ```
16
18
 
17
19
  you can do this:
18
20
 
19
- try_to { obj.met1.met2.met3.to_s }
21
+ ```ruby
22
+ try_to { obj.met1.met2.met3.to_s }
23
+ ```
20
24
 
21
25
  ### Exceptions
22
26
 
27
+ #### Adding exceptions
28
+
23
29
  It's possible to customize which exceptions to handle with `add_exception`:
24
30
 
25
- TryTo.add_exception(ZeroDivisionError)
26
- #=> [NoMethodError, ZeroDivisionError]
27
- try_to { 1/0 } # will not raise an exception
31
+ ```ruby
32
+ TryTo.add_exception(ZeroDivisionError)
33
+ #=> [NoMethodError, ZeroDivisionError]
34
+ try_to { 1/0 } # will not raise an exception
35
+ ```
36
+
37
+ #### Removing exceptions
28
38
 
29
39
  To remove an exception, use `remove_exception!`:
30
40
 
31
- TryTo.exceptions
32
- #=> [NoMethodError, ZeroDivisionError]
33
- TryTo.remove_exception!(ZeroDivisionError)
34
- #=> [NoMethodError]
41
+ ```ruby
42
+ TryTo.exceptions
43
+ #=> [NoMethodError, ZeroDivisionError]
44
+ TryTo.remove_exception!(ZeroDivisionError)
45
+ #=> [NoMethodError]
46
+ ```
47
+
48
+ #### Resetting exceptions
35
49
 
36
50
  You can also use `reset_exceptions!` to go back to only handle `NoMethodError`s.
37
51
 
38
- TryTo.exceptions
39
- #=> [NoMethodError, RuntimeError, ZeroDivisionError]
40
- TryTo.reset_exceptions!
41
- #=> [NoMethodError]
52
+ ```ruby
53
+ TryTo.exceptions
54
+ #=> [NoMethodError, RuntimeError, ZeroDivisionError]
55
+ TryTo.reset_exceptions!
56
+ #=> [NoMethodError]
57
+ ```
42
58
 
43
59
  ### Handlers
44
60
 
@@ -47,87 +63,103 @@ ways in which you can customize this behavior. All handlers can either be simple
47
63
  values or an object responding to `#call`, which should take one argument, the
48
64
  exception object.
49
65
 
50
- Specifying a handler with the call for one time use:
51
-
52
- # use a handler function
53
- try_to(-> e { puts e.class }) { 1.foo }
54
- # prints "NoMethodError"
55
- # or provide a simple value:
56
- try_to(42) { 1.foo }
57
- #=> 42
58
-
59
- Registering handlers so they always are used:
60
-
61
- TryTo.handlers
62
- #=> {}
63
- TryTo.add_handler(ZeroDivisionError, -> _ { puts "Ouch" })
64
- try_to { 1/0 }
65
- # prints "Ouch"
66
- TryTo.add_handler(NoMethodError, -> _ { 23 })
67
- try_to { 1.foo }
68
- #=> 23
69
- # or simply
70
- TryTo.add_handler(NoMethodError, 42)
71
- try_to { 1.foo } #=> 42
72
-
73
- Removing handler:
66
+ #### Specifying a handler inline:
67
+
68
+ ```ruby
69
+ try_to(-> e { puts e.class }) { 1.foo }
70
+ # prints "NoMethodError"
71
+ # or provide a simple value:
72
+ try_to(42) { 1.foo }
73
+ #=> 42
74
+ ```
75
+
76
+ #### Registering handlers for exception classes:
77
+
78
+ ```ruby
79
+ TryTo.handlers
80
+ #=> {}
81
+ TryTo.add_handler(ZeroDivisionError, -> _ { puts "Ouch" })
82
+ try_to { 1/0 }
83
+ # prints "Ouch"
84
+ TryTo.add_handler(NoMethodError, -> _ { 23 })
85
+ try_to { 1.foo }
86
+ #=> 23
87
+ # or simply
88
+ TryTo.add_handler(NoMethodError, 42)
89
+ try_to { 1.foo } #=> 42
90
+ ```
91
+
92
+ #### Removing handlers:
74
93
 
75
94
  TryTo.handlers
76
95
  #=> {ZeroDivisionError=>#<Proc:0x0000000108921d60@(irb):1 (lambda)>}
77
96
  TryTo.remove_handler!(ZeroDivisionError)
78
97
  #=> {}
79
98
 
80
- Last but not least you can define a default handler for all the exceptions
81
- listed in `TryTo.exceptions`.
82
-
83
- TryTo.default_handler = 42
84
- try_to { 1.foo } #=> 42
85
- # or
86
- TryTo.default_handler = lambda { |_| puts "Something went wrong!" }
87
- try_to { 1.foo }
88
- # Outputs: Something went wrong!
99
+ #### Default handler
89
100
 
90
- Here's a complete example in the form of an IRB transcript:
91
-
92
- # default behavior
93
- try_to #=> nil
94
- try_to {} #=> nil
95
- class Foo ; end
96
- try_to { Foo.new.foo } #=> nil
101
+ This will be called for all the exceptions listed in `TryTo.exceptions`.
97
102
 
98
- # this will raise an exception
99
- try_to { 1 / 0 }
100
- ZeroDivisionError: divided by 0
101
- # let's fix that
102
- TryTo.exceptions << ZeroDivisionError #=> [NoMethodError, ZeroDivisionError]
103
- try_to { 1 / 0 } #=> nil
103
+ ```ruby
104
+ TryTo.default_handler = 42
105
+ try_to { 1.foo } #=> 42
106
+ # or
107
+ TryTo.default_handler = -> _e { puts "Something went wrong!" }
108
+ try_to { 1.foo }
109
+ # Outputs: Something went wrong!
110
+ ```
104
111
 
105
- # change the default handler
106
- TryTo.default_handler = -> e { puts e.class }
107
- try_to { 1 / 0 } # prints "ZeroDivisionError"
108
- try_to { Foo.new.foo } # prints "ZeroDivisionError"
112
+ #### Example
109
113
 
110
- # new behavior for ZeroDivisionError
111
- TryTo.add_handler(ZeroDivisionError, -> _ { puts "You shouldn't divide by 0!"})
112
- try_to { 1 / 0 } # prints: "You shouldn't divide by 0!"
113
- try_to { Foo.new.foo} # still prints "NoMethodError"
114
+ Here's a complete example in the form of an IRB transcript:
114
115
 
115
- # change handler at call site
116
- try_to(-> _ {puts "Ouch!"}) { Foo.new.foo } # prints "Ouch!"
116
+ ```ruby
117
+ # default behavior
118
+ try_to #=> nil
119
+ try_to {} #=> nil
120
+ Foo = Class.new
121
+ try_to { Foo.new.foo } #=> nil (instead of NoMethodError)
122
+
123
+ # this will raise an exception
124
+ try_to { 1 / 0 }
125
+ ZeroDivisionError: divided by 0
126
+ # let's fix that
127
+ TryTo.exceptions << ZeroDivisionError #=> [NoMethodError, ZeroDivisionError]
128
+ try_to { 1 / 0 } #=> nil
129
+
130
+ # change the default handler
131
+ TryTo.default_handler = -> e { puts e.class }
132
+ try_to { 1 / 0 } # prints "ZeroDivisionError"
133
+ try_to { Foo.new.foo } # prints "ZeroDivisionError"
134
+
135
+ # new behavior for ZeroDivisionError
136
+ TryTo.add_handler(ZeroDivisionError, -> _ { puts "You shouldn't divide by 0!"})
137
+ try_to { 1 / 0 } # prints: "You shouldn't divide by 0!"
138
+ try_to { Foo.new.foo} # still prints "NoMethodError"
139
+
140
+ # change handler at call site
141
+ try_to(-> _ {puts "Ouch!"}) { Foo.new.foo } # prints "Ouch!"
142
+ ```
117
143
 
118
144
  ## Installation
119
145
 
120
146
  Add this line to your application's Gemfile:
121
147
 
122
- gem 'try_to'
148
+ ```ruby
149
+ gem 'try_to'
150
+ ```
123
151
 
124
152
  And then execute:
125
153
 
126
- $ bundle
154
+ ```shell
155
+ $ bundle
156
+ ```
127
157
 
128
158
  Or install it yourself:
129
159
 
130
- $ gem install try_to
160
+ ```shell
161
+ $ gem install try_to
162
+ ```
131
163
 
132
164
  ## Authors
133
165
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TryTo
2
4
  @handlers = {}
3
5
  @exceptions = [NoMethodError]
@@ -34,9 +36,7 @@ module Kernel
34
36
  private def try_to(handler = nil)
35
37
  yield if block_given?
36
38
  rescue *(TryTo.exceptions | TryTo.handlers.keys) => e
37
- handler = [handler,
38
- TryTo.handlers[e.class],
39
- TryTo.default_handler].compact.first
39
+ handler = handler || TryTo.handlers[e.class] || TryTo.default_handler
40
40
  handler.respond_to?(:call) ? handler.call(e) : handler
41
41
  end
42
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TryTo
4
- VERSION = '1.2'.freeze
4
+ VERSION = '1.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: try_to
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Kohl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-08 00:00:00.000000000 Z
11
+ date: 2019-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,8 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubyforge_project:
88
- rubygems_version: 2.7.6
87
+ rubygems_version: 3.0.3
89
88
  signing_key:
90
89
  specification_version: 4
91
90
  summary: An alternative approach to Rails' Object#try