Commit f8f9daf
Changed files (3)
spec
unit
lib/nasty/kernel.rb
@@ -0,0 +1,9 @@
+module Kernel
+ def using(resource, &block)
+ begin
+ block.call
+ ensure
+ resource.dispose
+ end
+ end
+end
lib/nasty.rb
@@ -1,5 +1,6 @@
require "nasty/command"
require "nasty/composite_command"
+require "nasty/kernel"
require "nasty/version"
module Nasty
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