try 0.3.0 → 0.4.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/try.rb +44 -2
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 709e480aa70f8f56410a63ec46009b96803e116b
4
- data.tar.gz: b16217ce61a1bdc4eb589e632297ac4a29d414ba
3
+ metadata.gz: 59d052acdfe93469250ccd32f028f0909d22ebc3
4
+ data.tar.gz: 8af0c816b218d044e8f50fff90195b1c77e0189d
5
5
  SHA512:
6
- metadata.gz: c4225e05e4cbf52002cfc805cf6ad792366a748d0ca627f72b6cd49a41324ab371146d553f783d4cc80a23bc45de60487f7cc2e3336b1aee4861e3dc62def597
7
- data.tar.gz: c3d7923acfc692c72dde29c0e250d1b9f5eb838530c2a23783ebc10e9286a66c5e42d72614f30d3b95a0e6c813853b145b04eb13cf81b3e233290b843c37da43
6
+ metadata.gz: b87930d9e447137c7f0b324f063369ba6e2b5d06b499e31dac4a1550945e5edbc2617b8c920b1f6890adbc3530ca54e4df7c5b025efdfaba19cef6847351b605
7
+ data.tar.gz: a6058c19afd7279d899d7274fff0574da95c301488fd0b629a66d246058ca84f84c718f750f399a4534b56e930e22eeb52524363c7572ce111b6b2d48b18dc03
data/lib/try.rb CHANGED
@@ -1,4 +1,20 @@
1
+ =begin
2
+ A module that attempts to provide some of the expressive power offered
3
+ by MOO Code through the following language constructs:
1
4
 
5
+ `unsafe_expression ! ANY'
6
+ `unsafe_expression ! ANY => fallback_expression'
7
+ `unsafe_expression ! E_FOO, E_BAR'
8
+ `unsafe_expression ! E_FOO, E_BAR => fallback_expression'
9
+
10
+ In Ruby these become:
11
+
12
+ Try.try { unsafe_expression }
13
+ Try.trap(Exception=>fallback) { unsafe_expression } # OR: unsafe_expression rescue fallback
14
+ Try.trap(FooError, BarError) { unsafe_expression}
15
+ Try.trap(FooError=>fallback, BarError=>fallback) { unsafe_expression }
16
+
17
+ =end
2
18
  module Try
3
19
  #
4
20
  # Evaluates the given block and returns its value.
@@ -17,7 +33,9 @@ module Try
17
33
  # parameters.
18
34
  #
19
35
  # Additionally, specific exception types can default to a fallback
20
- # value if passed as (({Type => value})) pairs.
36
+ # value if passed as (({Type => value})) pairs. If the +value+ is
37
+ # a Proc, it is called and the exception object is passed as a
38
+ # parameter.
21
39
  #
22
40
  # Any un-trapped exception is raised normally.
23
41
  #
@@ -33,7 +51,7 @@ module Try
33
51
  return ex if klass.instance_of?(Module) ? ex.kind_of?(klass) : ex.is_a?(klass)
34
52
  end
35
53
  hash.each_pair do |klass,value|
36
- return value if klass.instance_of?(Module) ? ex.kind_of?(klass) : ex.is_a?(klass)
54
+ return value.is_a?(Proc) ? value.call(ex) : value if klass.instance_of?(Module) ? ex.kind_of?(klass) : ex.is_a?(klass)
37
55
  end
38
56
  raise
39
57
  end
@@ -60,3 +78,27 @@ module Try
60
78
  extend self
61
79
  end
62
80
 
81
+ =begin
82
+ Copyright (c) 2013, Matthew Kerwin
83
+ All rights reserved.
84
+
85
+ Redistribution and use in source and binary forms, with or without
86
+ modification, are permitted provided that the following conditions are met:
87
+
88
+ 1. Redistributions of source code must retain the above copyright notice, this
89
+ list of conditions and the following disclaimer.
90
+ 2. Redistributions in binary form must reproduce the above copyright notice,
91
+ this list of conditions and the following disclaimer in the documentation
92
+ and/or other materials provided with the distribution.
93
+
94
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
95
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
96
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
97
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
98
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
99
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
100
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
101
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
102
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
103
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
104
+ =end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: try
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-11 00:00:00.000000000 Z
11
+ date: 2013-03-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Lazy mechanisms to capture exceptions on-the-fly.
14
14
  email: matthew@kerwin.net.au