diff options
| author | mo khan <mo@mokhan.ca> | 2013-12-20 12:38:36 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-12-20 12:38:36 -0700 |
| commit | f8f9daf2c0de476cf91cc1e5f3f9080be1dab7c6 (patch) | |
| tree | 0113747e50d03eb5d9a9155cb78906ba42421b3a | |
| parent | 296cea440ccd9fdefa050b90babbc69ee896df9b (diff) | |
add using to kernel.
| -rw-r--r-- | lib/nasty.rb | 1 | ||||
| -rw-r--r-- | lib/nasty/kernel.rb | 9 | ||||
| -rw-r--r-- | spec/unit/using_spec.rb | 26 |
3 files changed, 36 insertions, 0 deletions
diff --git a/lib/nasty.rb b/lib/nasty.rb index acfdce1..842f05b 100644 --- a/lib/nasty.rb +++ b/lib/nasty.rb @@ -1,5 +1,6 @@ require "nasty/command" require "nasty/composite_command" +require "nasty/kernel" require "nasty/version" module Nasty diff --git a/lib/nasty/kernel.rb b/lib/nasty/kernel.rb new file mode 100644 index 0000000..d858e56 --- /dev/null +++ b/lib/nasty/kernel.rb @@ -0,0 +1,9 @@ +module Kernel + def using(resource, &block) + begin + block.call + ensure + resource.dispose + end + end +end diff --git a/spec/unit/using_spec.rb b/spec/unit/using_spec.rb new file mode 100644 index 0000000..8988fcb --- /dev/null +++ b/spec/unit/using_spec.rb @@ -0,0 +1,26 @@ +require "spec_helper" + +describe "using" do + let(:item) { double("item", dispose: true) } + + it "disposes the item" do + using(item) { } + item.should have_received(:dispose) + end + + it "performs the action" do + ran = false + using(item) do + ran = true + end + ran.should be_true + end + + it "always cleans up the resource" do + begin + using(item) { raise "heck" } + rescue + end + item.should have_received(:dispose) + end +end |
