Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

lib/libcdb.rb (open): Minor refactoring.

  • Loading branch information...
commit 19e62aa4e1c075dba705d27e2f3bca1d31a10f8e 1 parent de25501
@blackwinter authored
Showing with 19 additions and 11 deletions.
  1. +19 −11 lib/libcdb.rb
View
30 lib/libcdb.rb
@@ -32,20 +32,12 @@ class << self
# <tt>r+</tt>:: CDB (initially opened for reading)
# <tt>w+</tt>:: CDB (initially opened for writing)
def open(path, mode = MODE_READ)
- klass, args = self, []
-
- case mode
- when 'r+' then args = [mode = MODE_READ]
- when 'w+' then args = [ MODE_WRITE]
- when MODE_READ then klass = Reader
- when MODE_WRITE then klass, mode = Writer, 'w+'
- else raise ArgumentError, "illegal access mode #{mode}"
- end
+ klass, args = _open_args(path, mode)
cdb = begin
- klass.new(io = File.open(path, mode), *args)
+ klass.new(*args)
rescue
- io.close if io
+ args.first.close
raise
end
@@ -86,6 +78,22 @@ def dump(path, target = '', separator = $\ || $/)
}
end
+ private
+
+ def _open_args(path, mode)
+ klass, args = self, []
+
+ case mode
+ when 'r+' then args = [mode = MODE_READ]
+ when 'w+' then args = [ MODE_WRITE]
+ when MODE_READ then klass = Reader
+ when MODE_WRITE then klass, mode = Writer, 'w+'
+ else raise ArgumentError, "illegal access mode #{mode.inspect}"
+ end
+
+ [klass, args.unshift(File.open(path, mode))]
+ end
+
end
# call-seq:
Please sign in to comment.
Something went wrong with that request. Please try again.