Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #806 from kostya/dir
Dir#open,foreach,path and refactor coercion
- Loading branch information
Showing
with
40 additions
and 37 deletions.
- +32 −8 lib-topaz/dir.rb
- +3 −9 lib-topaz/file.rb
- +0 −1 spec/tags/core/dir/close_tags.txt
- +0 −5 spec/tags/core/dir/foreach_tags.txt
- +0 −1 spec/tags/core/dir/initialize_tags.txt
- +0 −8 spec/tags/core/dir/open_tags.txt
- +0 −2 spec/tags/core/dir/path_tags.txt
- +0 −2 spec/tags/core/dir/read_tags.txt
- +5 −1 topaz/objects/dirobject.py
@@ -1,16 +1,40 @@ | ||
class Dir | ||
def self.exists?(dirname) | ||
if dirname.respond_to?(:to_path) | ||
dirname = dirname.to_path | ||
end | ||
begin | ||
!!new(dirname) | ||
rescue Errno::ENOENT, Errno::ENOTDIR | ||
false | ||
end | ||
!!new(dirname) | ||
rescue Errno::ENOENT, Errno::ENOTDIR | ||
false | ||
end | ||
|
||
class << self | ||
alias :exist? :exists? | ||
end | ||
|
||
def self.open(path, opts = nil, &block) | ||
dir = new(path) | ||
if block | ||
value = nil | ||
|
||
begin | ||
value = yield dir | ||
ensure | ||
dir.close | ||
end | ||
|
||
value | ||
else | ||
dir | ||
end | ||
end | ||
|
||
def self.foreach(path, &block) | ||
return self.enum_for(:foreach, path) unless block | ||
|
||
open(path) do |dir| | ||
while s = dir.read | ||
yield s | ||
end | ||
end | ||
|
||
nil | ||
end | ||
end |
@@ -1,2 +1 @@ | ||
fails:Dir#close closes the stream and fd and returns nil | ||
fails:Dir#close raises an IOError when called on a closed Dir instance |
@@ -1,3 +1 @@ | ||
fails:Dir#path returns the path that was supplied to .new or .open | ||
fails:Dir#path returns the path even when called on a closed Dir instance | ||
fails:Dir#path returns a String with the same encoding as the argument to .open |