godefer 0.1.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89b8e1803e4f34cb2dcffdc84ddf89db4d1587b0
4
- data.tar.gz: 99d1dd2aa28736d522eb0d13d84c0bf665d66cb3
3
+ metadata.gz: 98e03532245eb8835e7fa878912698cd2e48c600
4
+ data.tar.gz: 3190101b1dead8f35b0461524712056703cdfa28
5
5
  SHA512:
6
- metadata.gz: 73892dfef51ca21a66e5c1c9b49c688068f0903bed21cc7dadb1c11b5a3d577dcfacbdb19af528db2ba8209edc93f3ac9b1285ede19aa612d195d0b02d10fdb2
7
- data.tar.gz: 2b20512630f15afe18d0497f45b192aaeef588038ee7fb998a724c5540a401b31409ddbd807ee2a2178bd426ef802c15f293b6f592df7b4ca729ac46449fa433
6
+ metadata.gz: e26e349e62a721008367279a838f5f8d6f8915fc314cfa60b095c2985585649939022ba6b30a7dd8f4942f92c4e29bc3bd61e0cbed7f6d9b5150e38bb9151412
7
+ data.tar.gz: 9be5041a4b66623d01cf89e6896d89bff6aa85d0eb92fac0c6fc1072a810106fcc355aca9ffcb3edb412b1c7e16573b54d414901eb8dabbacdc93462072366d3
data/.travis.yml CHANGED
@@ -1,4 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0-p647
4
+ - 2.1.7
3
5
  - 2.2.3
6
+ - jruby-1.7.22
7
+ - jruby-9.0.3.0
8
+ - rbx-2.4.1
9
+ - rbx-2.5.8
4
10
  before_install: gem install bundler -v 1.10.6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- godefer (0.1.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
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/godefer`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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/[USERNAME]/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.
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
@@ -8,8 +8,3 @@ Rake::TestTask.new(:test) do |t|
8
8
  end
9
9
 
10
10
  task default: :test
11
-
12
- desc "Open irb session with library loaded"
13
- task :console do
14
- sh "irb -rubygems -Ilib -rgodefer"
15
- end
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
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
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
@@ -1,3 +1,3 @@
1
1
  module Defer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-30 00:00:00.000000000 Z
11
+ date: 2015-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler