path-temp 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +72 -0
  3. data/lib/file/path-temp.rb +32 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eb460b98c7f3a39822d32e4a256179976cecbd7d4b43844897da4d613f1ddabc
4
+ data.tar.gz: 226618b575360076071adbb961505944e3f7def34be813ade3c0cd56a3695ca3
5
+ SHA512:
6
+ metadata.gz: c3cd2ca78ec833d3e4cdf43fd65f2a26e1bb9e5eca4d72dfdf64805f5f2641b6c7a3652c30e2b2eb62dfdc64b2d478bc410d5d2d833866438147614540f43053
7
+ data.tar.gz: 4be9c4fcfd8eef741d4706a49a54776fd711bbea7e088205979d7aed0ad7b77a9bac5bdc8a2d96861506b58e8ae29f31ef18d7bc670177642405f80e5637e52c
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # path-temp
2
+
3
+ `File.path_temp` defines a temporary file path. It does not create the file. If,
4
+ after the do block, a file exists at that path then the file is deleted.
5
+
6
+ ```ruby
7
+ #!/usr/bin/ruby -w
8
+ require 'file/path-temp'
9
+
10
+ # call File.path_temp
11
+ File.path_temp() do |path|
12
+ # defines a path in current directory with a bunch of random numbers as
13
+ # the name e.g. ./18168068873296828
14
+ puts path
15
+
16
+ # file is *not* automatically created
17
+ puts File.exist?(path) # => false
18
+
19
+ # write to the path
20
+ File.write path, 'whatever'
21
+
22
+ # file now exists
23
+ puts File.exist?(path) # => true
24
+ end
25
+
26
+ # at this point, the file no longer exists
27
+ ```
28
+
29
+ ## root
30
+
31
+ To put the path somewhere besides the current directory, use the `root` option:
32
+
33
+ ```ruby
34
+ File.path_temp('root'=>'/tmp') do |path|
35
+ puts path # => e,g, /tmp/458577463617317
36
+ end
37
+ ```
38
+
39
+ ## extension
40
+
41
+ To give the path an extension, use the `ext` option:
42
+
43
+ ```ruby
44
+ File.path_temp('ext'=>'txt') do |path|
45
+ puts path # => e,g, ./5314921893963471.txt
46
+ end
47
+ ```
48
+
49
+ # Installation
50
+
51
+ The usual:
52
+
53
+ ```bash
54
+ sudo gem install path-temp
55
+ ```
56
+ Or however you like to install gems. It's just a single file.
57
+
58
+
59
+ ## License
60
+
61
+ Open source, MIT license.
62
+
63
+ ## Author
64
+
65
+ Mike O'Sullivan
66
+ mike@idocs.com
67
+
68
+ ## History
69
+
70
+ | version | date | notes |
71
+ |---------|--------------|-------------------------------|
72
+ | 1.0 | Jun 29, 2023 | Initial upload. |
@@ -0,0 +1,32 @@
1
+ class File
2
+ def self.path_temp(opts={})
3
+ opts = {'delete'=>true}.merge(opts)
4
+
5
+ # root
6
+ if opts['root']
7
+ root = opts['root'].sub(/\/*\z/mu, '/')
8
+ else
9
+ root = './'
10
+ end
11
+
12
+ # full path
13
+ path = rand.to_s
14
+ path = path.sub(/\A0\./mu, '')
15
+ path = root + path
16
+
17
+ # add extension
18
+ if opts['ext']
19
+ ext = opts['ext']
20
+ ext = ext.sub(/\A\.*/mu, '.')
21
+ path += ext
22
+ end
23
+
24
+ begin
25
+ yield path
26
+ ensure
27
+ if opts['delete'] and File.exist?(path)
28
+ File.delete path
29
+ end
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: path-temp
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Mike O'Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Utility for defining a path that, if the file exists, is deleted at the
14
+ end of the block.
15
+ email: mike@idocs.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/file/path-temp.rb
22
+ homepage: https://github.com/mikosullivan/path-temp
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.3.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: path-temp
45
+ test_files: []