tempfile_for 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,8 +28,7 @@ $ gem install tempfile_for
28
28
 
29
29
  ### for
30
30
 
31
- TempfileFor is a very tiny gem, with only a few lines of code.
32
- However, it can save you lines of ugly code.
31
+ TempfileFor is a very tiny gem, but it can save you lines of ugly code.
33
32
 
34
33
  To get a quick introduction into what TempfileFor does, check this out:
35
34
 
@@ -0,0 +1,28 @@
1
+
2
+ require "tempfile"
3
+
4
+ module TempfileFor
5
+ class Tempfile < ::Tempfile
6
+ def copy(options = {})
7
+ encoding = options[:encoding]
8
+
9
+ tempfile = self.class.open("tempfile", :encoding => encoding)
10
+
11
+ File.open path, :encoding => encoding do |stream|
12
+ tempfile.write_ext stream
13
+ end
14
+
15
+ tempfile.rewind
16
+ tempfile
17
+ end
18
+
19
+ def write_ext(io_or_data)
20
+ if io_or_data.respond_to?(:read)
21
+ write(io_or_data.read(1024)) until io_or_data.eof?
22
+ else
23
+ write io_or_data
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -1,3 +1,3 @@
1
1
  module TempfileFor
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/tempfile_for.rb CHANGED
@@ -1,28 +1,30 @@
1
1
 
2
2
  require "tempfile_for/version"
3
+ require "tempfile_for/tempfile"
3
4
  require "tempfile"
4
5
 
5
6
  class Tempfile
6
- def self.for(data, options = {})
7
- blank :encoding => data.encoding, :read => options[:read] do |tempfile|
8
- tempfile.write data
7
+ def self.for(io_or_data, options = {})
8
+ blank options.merge(:encoding => options[:encoding] || io_or_data.encoding) do |tempfile|
9
+ tempfile.write_ext io_or_data
9
10
  tempfile.flush
10
11
 
11
- yield tempfile
12
+ yield tempfile if block_given?
12
13
  end
13
- end
14
+ end
14
15
 
15
16
  def self.blank(options = {})
16
- tempfile = open("tempfile", :encoding => options[:encoding])
17
+ encoding = options[:encoding]
18
+
19
+ tempfile = TempfileFor::Tempfile.open("tempfile", :encoding => encoding)
17
20
 
18
- yield tempfile
21
+ yield tempfile if block_given?
19
22
 
20
23
  tempfile.flush
21
- tempfile.rewind
22
24
 
23
- options[:read] != false ? tempfile.read : tempfile
25
+ options[:read] != false ? File.read(tempfile.path, :encoding => encoding) : tempfile.copy(:encoding => encoding)
24
26
  ensure
25
- tempfile.close! if options[:read] != false
27
+ tempfile.close!
26
28
  end
27
29
  end
28
30
 
@@ -0,0 +1,31 @@
1
+
2
+ require File.expand_path("../../test_helper", __FILE__)
3
+ require "stringio"
4
+
5
+ class TempfileFor::TemfileTest < Minitest::Test
6
+ def test_copy
7
+ tempfile = TempfileFor::Tempfile.open("tempfile", :encoding => Encoding::BINARY)
8
+ tempfile.write "test"
9
+ tempfile.flush
10
+
11
+ copy = tempfile.copy(:encoding => Encoding::ISO_8859_1).read
12
+
13
+ assert_equal "test", copy
14
+ assert_equal Encoding::ISO_8859_1, copy.encoding
15
+ end
16
+
17
+ def test_write_ext
18
+ tempfile = TempfileFor::Tempfile.open("tempfile")
19
+ tempfile.write_ext StringIO.new("test")
20
+ tempfile.rewind
21
+
22
+ assert_equal "test", tempfile.read
23
+
24
+ tempfile = TempfileFor::Tempfile.open("tempfile")
25
+ tempfile.write_ext "test"
26
+ tempfile.rewind
27
+
28
+ assert_equal "test", tempfile.read
29
+ end
30
+ end
31
+
@@ -1,8 +1,5 @@
1
1
 
2
- require File.expand_path("../../lib/tempfile_for", __FILE__)
3
-
4
- require "minitest"
5
- require "minitest/autorun"
2
+ require File.expand_path("../test_helper", __FILE__)
6
3
 
7
4
  class TempfileForTest < Minitest::Test
8
5
  def test_tempfile_for
@@ -0,0 +1,6 @@
1
+
2
+ require File.expand_path("../../lib/tempfile_for", __FILE__)
3
+
4
+ require "minitest"
5
+ require "minitest/autorun"
6
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tempfile_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -40,9 +40,12 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - lib/tempfile_for.rb
43
+ - lib/tempfile_for/tempfile.rb
43
44
  - lib/tempfile_for/version.rb
44
45
  - tempfile_for.gemspec
46
+ - test/tempfile_for/tempfile_test.rb
45
47
  - test/tempfile_for_test.rb
48
+ - test/test_helper.rb
46
49
  homepage: https://github.com/mrkamel/tempfile_for
47
50
  licenses:
48
51
  - MIT