about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-04-05 06:25:43 +0000
committerEric Wong <normalperson@yhbt.net>2013-04-05 06:25:43 +0000
commitbefa6dade6158a86be88c7580a6259059e20fb25 (patch)
treeda8b451d72196c63a0409dcbab4fe4f6db2dbcbd
parentf5774348b6fe5bb7e86717629fdb8c4d8c30731f (diff)
downloadmahoro-befa6dade6158a86be88c7580a6259059e20fb25.tar.gz
Mahoro#file supports objects with #to_path support
Pathname (and some other classes) are implemented this way and
Ruby 1.9+ respects #to_path on all File.open calls.
-rw-r--r--mahoro.c7
-rwxr-xr-xtest.rb7
2 files changed, 14 insertions, 0 deletions
diff --git a/mahoro.c b/mahoro.c
index 1548a76..1c0b992 100644
--- a/mahoro.c
+++ b/mahoro.c
@@ -18,6 +18,7 @@
 
 static VALUE cMahoro;
 static VALUE eMahoroError;
+static ID id_to_path;
 
 struct nogvl_args {
         magic_t cookie;
@@ -178,6 +179,11 @@ mahoro_file(self, path)
         struct nogvl_args args;
 
         args.cookie = (magic_t)DATA_PTR(self);
+
+        /* Pathname objects may be transformed via #to_path */
+        if (rb_respond_to(path, id_to_path))
+                path = rb_funcall(path, id_to_path, 0);
+
         args.as.path = StringValueCStr(path);
 
         if(!(msg = NOGVL(nogvl_file, &args, RUBY_UBF_IO, NULL))) {
@@ -598,6 +604,7 @@ void Init_mahoro(void)
         rb_define_singleton_method(cMahoro, "compile", mahoro_s_compile, 1);
         rb_define_method(cMahoro, "compile", mahoro_compile, 1);
         rb_define_method(cMahoro, "load", mahoro_load, 1);
+        id_to_path = rb_intern("to_path");
 }
 
 /* arch-tag: mahoro */
diff --git a/test.rb b/test.rb
index fe00b36..75e46d6 100755
--- a/test.rb
+++ b/test.rb
@@ -2,6 +2,7 @@
 
 require 'test/unit'
 require 'mahoro'
+require 'pathname'
 
 class MahoroTestCase < Test::Unit::TestCase
 
@@ -19,6 +20,12 @@ class MahoroTestCase < Test::Unit::TestCase
                 assert_match(/(?:source|text)/, buf, "is source or text")
         end
 
+        def test_pathname
+                @m.flags = Mahoro::NONE
+                pn = Pathname.new('mahoro.c')
+                assert_c_text(@m.file(pn))
+        end
+
         def test_file
                 @m.flags = Mahoro::NONE
                 assert_c_text(@m.file('mahoro.c'))