tempfile_for 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,6 +25,8 @@ $ gem install tempfile_for
25
25
 
26
26
  ## Usage
27
27
 
28
+ ### for
29
+
28
30
  TempfileFor is a very tiny gem, with only a few lines of code.
29
31
  However, it can save you lines of ugly code.
30
32
 
@@ -50,6 +52,31 @@ image = Tempfile.for(data) do |tempfile|
50
52
  end
51
53
  ```
52
54
 
55
+ ### blank
56
+
57
+ If you want to use TempfileFor without initial data, simply use:
58
+
59
+ ```ruby
60
+ Tempfile.blank { |tempfile| `echo -n data >> #{tempfile.path}` }
61
+ ```
62
+
63
+ ## Encoding
64
+
65
+ If you're using ruby 1.9+, TempfileFor preserves the encoding of the supplied data. Thus, the following code
66
+
67
+ ```ruby
68
+ Tempfile.for("string1".encode(Encoding::ISO_8859_1)) { ... }
69
+ ```
70
+
71
+ will return a string encoded as ISO-8859-1. If you use the blank method, you
72
+ can supply the desired encoding, such that
73
+
74
+ ```ruby
75
+ Tempfile.blank(:encoding => Encoding::BINARY) { ... }
76
+ ```
77
+
78
+ will return a string encoded as binary.
79
+
53
80
  ## Contributing
54
81
 
55
82
  1. Fork it
data/lib/tempfile_for.rb CHANGED
@@ -29,5 +29,27 @@ class Tempfile
29
29
  tempfile.close!
30
30
  end
31
31
  end
32
+
33
+ def self.blank(options = {})
34
+ tempfile = nil
35
+
36
+ if RUBY_VERSION < "1.9" || options[:encoding].nil?
37
+ tempfile = open("tempfile")
38
+ else
39
+ tempfile = open("tempfile", :encoding => options[:encoding])
40
+ end
41
+
42
+ begin
43
+ yield tempfile
44
+
45
+ if RUBY_VERSION < "1.9" || options[:encoding].nil?
46
+ File.read tempfile.path
47
+ else
48
+ File.read tempfile.path, :encoding => options[:encoding]
49
+ end
50
+ ensure
51
+ tempfile.close!
52
+ end
53
+ end
32
54
  end
33
55
 
@@ -1,3 +1,3 @@
1
1
  module TempfileFor
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -8,9 +8,21 @@ class TempfileForTest < Test::Unit::TestCase
8
8
  assert_equal "string1, string2", Tempfile.for("string1") { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
9
9
 
10
10
  if RUBY_VERSION >= "1.9"
11
- string = "string1".force_encoding("ISO-8859-1")
11
+ string = "string1".force_encoding(Encoding::ISO_8859_1)
12
+
12
13
  res = Tempfile.for(string) { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
13
- assert_equal "ISO-8859-1", res.encoding.name
14
+
15
+ assert_equal Encoding::ISO_8859_1, res.encoding
16
+ end
17
+ end
18
+
19
+ def test_tempfile_blank
20
+ assert_equal "data", Tempfile.blank { |tempfile| `echo -n data >> #{tempfile.path}` }
21
+
22
+ if RUBY_VERSION >= "1.9"
23
+ res = Tempfile.blank(:encoding => Encoding::ISO_8859_1) { |tempfile| `echo -n data >> #{tempfile.path}` }
24
+
25
+ assert_equal Encoding::ISO_8859_1, res.encoding
14
26
  end
15
27
  end
16
28
  end
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.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-02 00:00:00.000000000 Z
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake