diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-05-13 17:57:10 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-05-13 17:57:10 -0700 |
commit | 605765ded31c784727077dfca573092ba725f717 (patch) | |
tree | 1768f51e990f1eb8d66f2ea886d216a140c02d7d | |
parent | c8bd876fb5086e5b79299869b4c29f1f7f020b4d (diff) | |
download | kgio-605765ded31c784727077dfca573092ba725f717.tar.gz |
Kgio.tryopen => Kgio::File.tryopen
This will allow users to subclass Kgio::File and override certain behavior (e.g. overriding "#each").
-rw-r--r-- | ext/kgio/tryopen.c | 6 | ||||
-rw-r--r-- | test/test_tryopen.rb | 16 |
2 files changed, 11 insertions, 11 deletions
diff --git a/ext/kgio/tryopen.c b/ext/kgio/tryopen.c index 6ba9618..d6789f4 100644 --- a/ext/kgio/tryopen.c +++ b/ext/kgio/tryopen.c @@ -18,7 +18,6 @@ static ID id_for_fd, id_to_path, id_path; static st_table *errno2sym; -static VALUE cFile; struct open_args { const char *pathname; @@ -98,7 +97,7 @@ retry: return rv; } } - rv = rb_funcall(cFile, id_for_fd, 1, INT2FIX(fd)); + rv = rb_funcall(klass, id_for_fd, 1, INT2FIX(fd)); set_file_path(rv, pathname); return rv; } @@ -106,6 +105,7 @@ retry: void init_kgio_tryopen(void) { VALUE mKgio = rb_define_module("Kgio"); + VALUE cFile; VALUE tmp; VALUE *ptr; long len; @@ -114,8 +114,8 @@ void init_kgio_tryopen(void) id_for_fd = rb_intern("for_fd"); id_to_path = rb_intern("to_path"); - rb_define_singleton_method(mKgio, "tryopen", s_tryopen, -1); cFile = rb_define_class_under(mKgio, "File", rb_cFile); + rb_define_singleton_method(cFile, "tryopen", s_tryopen, -1); if (!rb_funcall(cFile, rb_intern("method_defined?"), 1, ID2SYM(id_to_path))) diff --git a/test/test_tryopen.rb b/test/test_tryopen.rb index ca80460..e60cb27 100644 --- a/test/test_tryopen.rb +++ b/test/test_tryopen.rb @@ -6,7 +6,7 @@ require 'kgio' class TestTryopen < Test::Unit::TestCase def test_tryopen_success - tmp = Kgio.tryopen(__FILE__) + tmp = Kgio::File.tryopen(__FILE__) assert_kind_of File, tmp assert_equal File.read(__FILE__), tmp.read assert_equal __FILE__, tmp.path @@ -18,20 +18,20 @@ class TestTryopen < Test::Unit::TestCase tmp = Tempfile.new "tryopen" path = tmp.path tmp.close! - tmp = Kgio.tryopen(path) + tmp = Kgio::File.tryopen(path) assert_equal :ENOENT, tmp end def test_tryopen_EPERM tmp = Tempfile.new "tryopen" File.chmod 0000, tmp.path - tmp = Kgio.tryopen(tmp.path) + tmp = Kgio::File.tryopen(tmp.path) assert_equal :EACCES, tmp end def test_tryopen_readwrite tmp = Tempfile.new "tryopen" - file = Kgio.tryopen(tmp.path, IO::RDWR) + file = Kgio::File.tryopen(tmp.path, IO::RDWR) file.syswrite "FOO" assert_equal "FOO", tmp.sysread(3) end @@ -40,7 +40,7 @@ class TestTryopen < Test::Unit::TestCase tmp = Tempfile.new "tryopen" path = tmp.path tmp.close! - file = Kgio.tryopen(path, IO::RDWR|IO::CREAT, 0000) + file = Kgio::File.tryopen(path, IO::RDWR|IO::CREAT, 0000) assert_equal 0100000, File.stat(path).mode ensure File.unlink path @@ -53,17 +53,17 @@ class TestTryopen < Test::Unit::TestCase file = tmp.path Benchmark.bmbm do |x| x.report("tryopen (OK)") do - nr.times { Kgio.tryopen(file).close } + nr.times { Kgio::File.tryopen(file).close } end x.report("open (OK)") do nr.times { File.readable?(file) && File.open(file).close } end end tmp.close! - assert_equal :ENOENT, Kgio.tryopen(file) + assert_equal :ENOENT, Kgio::File.tryopen(file) Benchmark.bmbm do |x| x.report("tryopen (ENOENT)") do - nr.times { Kgio.tryopen(file) } + nr.times { Kgio::File.tryopen(file) } end x.report("open (ENOENT)") do nr.times { File.readable?(file) && File.open(file) } |