godefer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/Gemfile.lock +2 -2
- data/README.md +49 -5
- data/Rakefile +0 -5
- data/bin/console +11 -9
- data/lib/defer/block.rb +9 -0
- data/lib/defer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98e03532245eb8835e7fa878912698cd2e48c600
|
4
|
+
data.tar.gz: 3190101b1dead8f35b0461524712056703cdfa28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e26e349e62a721008367279a838f5f8d6f8915fc314cfa60b095c2985585649939022ba6b30a7dd8f4942f92c4e29bc3bd61e0cbed7f6d9b5150e38bb9151412
|
7
|
+
data.tar.gz: 9be5041a4b66623d01cf89e6896d89bff6aa85d0eb92fac0c6fc1072a810106fcc355aca9ffcb3edb412b1c7e16573b54d414901eb8dabbacdc93462072366d3
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
godefer (0.
|
4
|
+
godefer (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -25,7 +25,7 @@ DEPENDENCIES
|
|
25
25
|
bundler (~> 1.10)
|
26
26
|
codeclimate-test-reporter
|
27
27
|
godefer!
|
28
|
-
minitest
|
28
|
+
minitest (~> 5.8)
|
29
29
|
rake (~> 10.0)
|
30
30
|
|
31
31
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -4,9 +4,7 @@
|
|
4
4
|
[![Code Climate](https://codeclimate.com/github/zacheryph/godefer/badges/gpa.svg)](https://codeclimate.com/github/zacheryph/godefer)
|
5
5
|
[![Test Coverage](https://codeclimate.com/github/zacheryph/godefer/badges/coverage.svg)](https://codeclimate.com/github/zacheryph/godefer/coverage)
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
TODO: Delete this and the text above, and describe your gem
|
7
|
+
Godefer implements the `defer` / `recover` mechanism from [Go](https://golang.org). It allows grouping operations together that require a closing of some sort after opening. I don't really know the usefulness of this gem, though I will take issues and pull requests.
|
10
8
|
|
11
9
|
## Installation
|
12
10
|
|
@@ -26,7 +24,53 @@ Or install it yourself as:
|
|
26
24
|
|
27
25
|
## Usage
|
28
26
|
|
29
|
-
|
27
|
+
Godefer allows two different uses. The first using a general block, which would mostly be useful probably writing one off scripts of some sort. The other is to include the `Defer` module into your own class and running code within a `with_defer` block.
|
28
|
+
|
29
|
+
### Block Usage
|
30
|
+
|
31
|
+
Example of writing to an output file. The `defer {}` block gets called at the end of the run block.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Defer.run do
|
35
|
+
f = File.open('output.log')
|
36
|
+
defer { f.close }
|
37
|
+
|
38
|
+
# do stuff that uses f
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Module Usage
|
43
|
+
|
44
|
+
Similar example where we are doing a bunch of stuff within a class.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Audit
|
48
|
+
include Defer
|
49
|
+
attr_accessor :file
|
50
|
+
|
51
|
+
def large_operation
|
52
|
+
with_defer do
|
53
|
+
opening
|
54
|
+
defer { closing }
|
55
|
+
|
56
|
+
helper
|
57
|
+
# ...
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def opening
|
62
|
+
# ...
|
63
|
+
end
|
64
|
+
|
65
|
+
def helper
|
66
|
+
# ...
|
67
|
+
end
|
68
|
+
|
69
|
+
def closing
|
70
|
+
# ...
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
30
74
|
|
31
75
|
## Development
|
32
76
|
|
@@ -36,4 +80,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
36
80
|
|
37
81
|
## Contributing
|
38
82
|
|
39
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zacheryph/godefer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
begin
|
4
|
+
require "pry"
|
5
|
+
rescue LoadError
|
6
|
+
end
|
7
|
+
|
3
8
|
require "bundler/setup"
|
4
9
|
require "godefer"
|
5
10
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
11
|
+
begin
|
12
|
+
Pry.start
|
13
|
+
rescue
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
16
|
+
end
|
data/lib/defer/block.rb
CHANGED
@@ -8,7 +8,11 @@ module Defer
|
|
8
8
|
|
9
9
|
def run(&block)
|
10
10
|
ret = instance_eval(&block)
|
11
|
+
rescue Exception => e
|
12
|
+
@_exception = e
|
13
|
+
ensure
|
11
14
|
_cleanup
|
15
|
+
raise @_exception if @_exception && !@_recovered
|
12
16
|
ret
|
13
17
|
end
|
14
18
|
|
@@ -16,6 +20,11 @@ module Defer
|
|
16
20
|
_stack.unshift(block)
|
17
21
|
end
|
18
22
|
|
23
|
+
def recover
|
24
|
+
@_recovered = true
|
25
|
+
@_exception
|
26
|
+
end
|
27
|
+
|
19
28
|
private
|
20
29
|
|
21
30
|
def _stack
|
data/lib/defer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: godefer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zachery Hostens
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|