Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: iij/mruby-tempfile
base: master@{1393927627}
...
head fork: iij/mruby-tempfile
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 16 commits
  • 7 files changed
  • 4 commit comments
  • 3 contributors
View
2  .travis.yml
@@ -0,0 +1,2 @@
+script:
+ - "ruby run_test.rb all test"
View
15 README.md
@@ -5,6 +5,21 @@ Tempfile class for mruby (experimental)
depend on mruby-io(iij/mruby, mruby-support-io branch), mruby-dir, mrub-simple-random, mruby-sprintf, mruby-time
+## Implemented methods
+
+### Tempfile
+ - http://docs.ruby-lang.org/ja/1.9.3/class/Tempfile.html
+
+| method | mruby-tempfile | memo |
+| -------------------------------- | -------------- | ---- |
+| Tempfile.new, Tempfile.open | o | |
+| Tempfile#close | o | |
+| Tempfile#close! | o | |
+| Tempfile#delete, Tempfile#unlink | o | |
+| Tempfile#length, Tempfile#size | o | |
+| Tempfile#open | x | |
+| Tempfile#path | o | |
+
## License
View
9 mrbgem.rake
@@ -1,4 +1,11 @@
MRuby::Gem::Specification.new('mruby-tempfile') do |spec|
spec.license = 'MIT'
- spec.authors = 'Internet Initiative Japan Inc.'
+ spec.author = 'Internet Initiative Japan Inc.'
+
+ spec.add_dependency 'mruby-dir'
+ #spec.add_dependency 'mruby-env' # not mandatory
+ spec.add_dependency 'mruby-io'
+ spec.add_dependency 'mruby-random'
+ spec.add_dependency 'mruby-sprintf'
+ spec.add_dependency 'mruby-time'
end
View
31 mrblib/tempfile.rb
@@ -1,32 +1,15 @@
# -*- coding: utf-8 -*-
class Tempfile < File
- @@tempfiles = []
-
def initialize(basename, tempdir = Dir::tmpdir)
- self._bless
@deleted = false
@basename = basename
@mode = "w+"
@perm = 0600
@path = @path || make_tmpname(basename, tempdir)
+ @entity = TempfilePath.new(@path)
super(@path, @mode, @perm)
-
- @@tempfiles << self
-
- self
- end
-
- def self.delete_all
- while not @@tempfiles.empty?
- @@tempfiles.shift.delete
- end
-
- end
-
- def self.tempfiles
- @@tempfiles
end
def make_tmpname(basename, tempdir, n=nil)
@@ -65,13 +48,6 @@ def make_tmpname(basename, tempdir, n=nil)
path
end
- def open
- close unless closed?
- tempfile = initialize(@basename)
-
- tempfile
- end
-
def close(real=false)
super
delete if real
@@ -81,14 +57,11 @@ def close(real=false)
def close!
close(true)
-
- nil
end
def delete
File.delete(@path)
@deleted = true
- @@tempfiles.delete(self)
self
end
@@ -100,7 +73,7 @@ def deleted?
end
def size
- FileTest.size?(self)
+ File.size(@path)
end
alias :length :size
View
4 run_test.rb
@@ -23,7 +23,9 @@
toolchain :gcc
conf.gembox 'default'
- conf.gem :git => 'https://github.com/iij/mruby-io.git'
+ conf.gem github: 'iij/mruby-dir'
+ conf.gem github: 'iij/mruby-env'
+ conf.gem github: 'iij/mruby-io'
conf.gem File.expand_path(File.dirname(__FILE__))
end
View
54 src/tempfile.c
@@ -5,12 +5,51 @@
*/
#include "mruby.h"
#include "mruby/class.h"
-
+#include "mruby/value.h"
+#include "mruby/variable.h"
+#include "mruby/string.h"
+#include "mruby/data.h"
//#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
+static void mrb_tempfile_path_free(mrb_state *, void *);
+
+struct tempfile_path {
+ char *pathname;
+};
+
+const static struct mrb_data_type mrb_tempfile_path_type = { "TempfilePath", mrb_tempfile_path_free };
+
+static void
+mrb_tempfile_path_free(mrb_state *mrb, void *self)
+{
+ struct tempfile_path *tp = self;
+
+ if ( ! access(tp->pathname, F_OK) ) {
+ unlink(tp->pathname);
+ }
+
+ mrb_free(mrb, self);
+}
+
+mrb_value
+mrb_tempfile_path_init(mrb_state *mrb, mrb_value self)
+{
+ mrb_value path;
+ struct tempfile_path *tp;
+
+ tp = (struct tempfile_path *)mrb_malloc(mrb, sizeof(struct tempfile_path));
+
+ mrb_get_args(mrb, "S", &path);
+ tp->pathname = mrb_string_value_cstr(mrb, &path);
+
+ DATA_TYPE(self) = &mrb_tempfile_path_type;
+ DATA_PTR(self) = tp;
+
+ return self;
+}
mrb_value
mrb_tempfile_getpid(mrb_state *mrb, mrb_value self)
@@ -23,6 +62,17 @@ mrb_tempfile_getpid(mrb_state *mrb, mrb_value self)
}
void
+mrb_init_tempfile_path(mrb_state *mrb)
+{
+ struct RClass *tempfile_path_class;
+
+ tempfile_path_class = mrb_define_class(mrb, "TempfilePath", mrb->object_class);
+ MRB_SET_INSTANCE_TT(tempfile_path_class, MRB_TT_DATA);
+
+ mrb_define_method(mrb, tempfile_path_class, "initialize", mrb_tempfile_path_init, MRB_ARGS_REQ(1));
+}
+
+void
mrb_mruby_tempfile_gem_init(mrb_state *mrb)
{
struct RClass *tempfile_class;
@@ -34,6 +84,8 @@ mrb_mruby_tempfile_gem_init(mrb_state *mrb)
MRB_SET_INSTANCE_TT(tempfile_class, MRB_TT_DATA);
mrb_define_class_method(mrb, tempfile_class, "_getpid", mrb_tempfile_getpid, MRB_ARGS_NONE());
+
+ mrb_init_tempfile_path(mrb);
}
void
View
18 test/test_tempfile.rb
@@ -0,0 +1,18 @@
+assert('Tempfile remove tempfile (when GC run)') do
+ t = Tempfile.new 'test'
+ path = t.path
+ assert_true File.exist?(path)
+ t.close false
+ assert_true File.exist?(path)
+ t = nil
+ GC.start
+ assert_false File.exist?(path)
+end
+
+assert('Tempfile remove tempfile (call Tempfile#close with real = true)') do
+ t = Tempfile.new 'test'
+ path = t.path
+ assert_true File.exist?(path)
+ t.close true
+ assert_false File.exist?(path)
+end

Showing you all comments on commits in this comparison.

@ak-mochi
Owner

@@tempfilesを消す
self.delete_allも削る
self.tempfilesも削る

@ak-mochi
Owner

_set_pathをやめて、インスタンス変数(@entity)に直接入れる

@ak-mochi
Owner

Tempfile#initializeはnew以外にもTempfile#openから呼ばれることがある

@ak-mochi
Owner

削る。newから呼ばれる場合はいらない

Something went wrong with that request. Please try again.