Skip to content

Commit

Permalink
Merge pull request #806 from kostya/dir
Browse files Browse the repository at this point in the history
Dir#open,foreach,path and refactor coercion
  • Loading branch information
alex committed Jul 14, 2013
2 parents 5593987 + e176aac commit a66c520
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 37 deletions.
40 changes: 32 additions & 8 deletions lib-topaz/dir.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 3 additions & 9 deletions lib-topaz/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ def self.size(filename)
end

def self.zero?(filename)
if filename.respond_to?(:to_path)
filename = filename.to_path
end

begin
File.size(filename) == 0
rescue Errno::ENOENT
false
end
File.size(filename) == 0
rescue Errno::ENOENT
false
end

def size
Expand Down
1 change: 0 additions & 1 deletion spec/tags/core/dir/close_tags.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 0 additions & 5 deletions spec/tags/core/dir/foreach_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/core/dir/initialize_tags.txt

This file was deleted.

8 changes: 0 additions & 8 deletions spec/tags/core/dir/open_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/tags/core/dir/path_tags.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions spec/tags/core/dir/read_tags.txt

This file was deleted.

6 changes: 5 additions & 1 deletion topaz/objects/dirobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def ensure_open(self, space):
if not self.open:
raise space.error(space.w_IOError, "closed directory")

@classdef.method("initialize", path="str")
@classdef.method("initialize", path="path")
def method_initialize(self, space, path):
self.path = path
try:
Expand Down Expand Up @@ -127,6 +127,10 @@ def method_close(self, space):
self.open = False
return space.w_nil

@classdef.method("path")
def method_path(self, space):
return space.newstr_fromstr(self.path)

@classdef.singleton_method("mkdir", path="path", mode="int")
def method_mkdir(self, space, path, mode=0777):
try:
Expand Down

0 comments on commit a66c520

Please sign in to comment.