tempfile_for 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +19 -5
- data/lib/tempfile_for/version.rb +1 -1
- data/lib/tempfile_for.rb +9 -38
- data/test/tempfile_for_test.rb +8 -17
- metadata +2 -2
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
|
2
2
|
# TempfileFor
|
3
3
|
|
4
|
-
Easily create temporary files for in-memory data, modify the file, and get the
|
4
|
+
Easily create temporary files for in-memory data, modify the file, and get the
|
5
|
+
full content returned.
|
5
6
|
|
6
7
|
## Installation
|
7
8
|
|
@@ -37,8 +38,9 @@ Tempfile.for("string1") { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
|
|
37
38
|
# => "string1, string2"
|
38
39
|
```
|
39
40
|
|
40
|
-
Say, you have some in-memory data, like an image you fetch from an URL - and
|
41
|
-
like e.g., add or remove IPTC tags, or
|
41
|
+
Say, you have some in-memory data, like an image you fetch from an URL - and
|
42
|
+
you want to somehow modify it partially like e.g., add or remove IPTC tags, or
|
43
|
+
scale it, etc. Often, the gems used to modify images or other media files
|
42
44
|
require a path to the file to be able to modify it.
|
43
45
|
|
44
46
|
This is easy thanks to TempfileFor:
|
@@ -60,16 +62,28 @@ If you want to use TempfileFor without initial data, simply use:
|
|
60
62
|
Tempfile.blank { |tempfile| `echo -n data >> #{tempfile.path}` }
|
61
63
|
```
|
62
64
|
|
65
|
+
### Tempfile object
|
66
|
+
|
67
|
+
In case you need the tempfile object itself, because you e.g. don't want to
|
68
|
+
load it into memory, add `:read => false` to either `Tempfile#for` or
|
69
|
+
`Tempfile#blank`
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Tempfile.for("string", :read => false) { |tempfile| ... } # => #<File:/tmp/tempfile...>
|
73
|
+
Tempfile.blank(:read => false) { |tempfile| ... } # => #<File:/tmp/tempfile...>
|
74
|
+
```
|
75
|
+
|
63
76
|
## Encoding
|
64
77
|
|
65
|
-
|
78
|
+
TempfileFor preserves the encoding of the supplied data. Thus, the following
|
79
|
+
code
|
66
80
|
|
67
81
|
```ruby
|
68
82
|
Tempfile.for("string1".encode(Encoding::ISO_8859_1)) { ... }
|
69
83
|
```
|
70
84
|
|
71
85
|
will return a string encoded as ISO-8859-1. If you use the blank method, you
|
72
|
-
can supply the desired encoding
|
86
|
+
can supply the desired encoding via
|
73
87
|
|
74
88
|
```ruby
|
75
89
|
Tempfile.blank(:encoding => Encoding::BINARY) { ... }
|
data/lib/tempfile_for/version.rb
CHANGED
data/lib/tempfile_for.rb
CHANGED
@@ -3,55 +3,26 @@ require "tempfile_for/version"
|
|
3
3
|
require "tempfile"
|
4
4
|
|
5
5
|
class Tempfile
|
6
|
-
def self.for(data)
|
7
|
-
|
8
|
-
|
9
|
-
if RUBY_VERSION < "1.9"
|
10
|
-
tempfile = open("tempfile")
|
11
|
-
else
|
12
|
-
tempfile = open("tempfile", :encoding => data.encoding)
|
13
|
-
end
|
14
|
-
|
15
|
-
begin
|
6
|
+
def self.for(data, options = {})
|
7
|
+
blank :encoding => data.encoding, :read => options[:read] do |tempfile|
|
16
8
|
tempfile.write data
|
17
9
|
tempfile.flush
|
18
10
|
|
19
11
|
yield tempfile
|
20
|
-
|
21
|
-
tempfile.flush
|
22
|
-
|
23
|
-
if RUBY_VERSION < "1.9"
|
24
|
-
File.read tempfile.path
|
25
|
-
else
|
26
|
-
File.read tempfile.path, :encoding => data.encoding
|
27
|
-
end
|
28
|
-
ensure
|
29
|
-
tempfile.close!
|
30
12
|
end
|
31
13
|
end
|
32
14
|
|
33
15
|
def self.blank(options = {})
|
34
|
-
tempfile =
|
16
|
+
tempfile = open("tempfile", :encoding => options[:encoding])
|
35
17
|
|
36
|
-
|
37
|
-
tempfile = open("tempfile")
|
38
|
-
else
|
39
|
-
tempfile = open("tempfile", :encoding => options[:encoding])
|
40
|
-
end
|
18
|
+
yield tempfile
|
41
19
|
|
42
|
-
|
43
|
-
|
20
|
+
tempfile.flush
|
21
|
+
tempfile.rewind
|
44
22
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
File.read tempfile.path
|
49
|
-
else
|
50
|
-
File.read tempfile.path, :encoding => options[:encoding]
|
51
|
-
end
|
52
|
-
ensure
|
53
|
-
tempfile.close!
|
54
|
-
end
|
23
|
+
options[:read] != false ? tempfile.read : tempfile
|
24
|
+
ensure
|
25
|
+
tempfile.close! if options[:read] != false
|
55
26
|
end
|
56
27
|
end
|
57
28
|
|
data/test/tempfile_for_test.rb
CHANGED
@@ -1,29 +1,20 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
|
3
2
|
require File.expand_path("../../lib/tempfile_for", __FILE__)
|
4
|
-
require "test/unit"
|
5
3
|
|
6
|
-
|
4
|
+
require "minitest"
|
5
|
+
require "minitest/autorun"
|
6
|
+
|
7
|
+
class TempfileForTest < Minitest::Test
|
7
8
|
def test_tempfile_for
|
8
9
|
assert_equal "string1, string2", Tempfile.for("string1") { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
|
9
|
-
|
10
|
-
|
11
|
-
string = "string1".force_encoding(Encoding::ISO_8859_1)
|
12
|
-
|
13
|
-
res = Tempfile.for(string) { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
|
14
|
-
|
15
|
-
assert_equal Encoding::ISO_8859_1, res.encoding
|
16
|
-
end
|
10
|
+
assert_equal "string1, string2", Tempfile.for("string1", :read => false) { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }.read
|
11
|
+
assert_equal Encoding::ISO_8859_1, Tempfile.for("string1".force_encoding(Encoding::ISO_8859_1)) { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }.encoding
|
17
12
|
end
|
18
13
|
|
19
14
|
def test_tempfile_blank
|
20
15
|
assert_equal "data", Tempfile.blank { |tempfile| `echo -n data >> #{tempfile.path}` }
|
21
|
-
|
22
|
-
|
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
|
26
|
-
end
|
16
|
+
assert_equal "data", Tempfile.blank(:read => false) { |tempfile| `echo -n data >> #{tempfile.path}` }.read
|
17
|
+
assert_equal Encoding::ISO_8859_1, Tempfile.blank(:encoding => Encoding::ISO_8859_1) { |tempfile| `echo -n data >> #{tempfile.path}` }.encoding
|
27
18
|
end
|
28
19
|
end
|
29
20
|
|
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.
|
4
|
+
version: 0.0.9
|
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:
|
12
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|