tmp 4.0.0 → 4.1.0
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.
- checksums.yaml +4 -4
- data/.gitignore +0 -3
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/tmp/deprecated/core.rb +186 -0
- data/lib/tmp/deprecated/dci.rb +3 -0
- data/lib/tmp/deprecated/dsl.rb +62 -0
- data/lib/tmp/deprecated/init.rb +49 -0
- data/lib/tmp/deprecated/sup.rb +92 -0
- data/lib/tmp/instance.rb +60 -0
- data/spec/tmp/instance_spec.rb +157 -0
- metadata +10 -8
- data/examples/blocks.rb +0 -50
- data/examples/config_exmpl.rb +0 -23
- data/examples/instance.rb +0 -30
- data/examples/path.rb +0 -7
- data/examples/sugar_use.rb +0 -18
- data/examples/test.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b63871da5854ee49651dc7047a3b727bb6f0bb35
|
4
|
+
data.tar.gz: 20264ee3627c2290530ac350b7c1b85e810ddb88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec4c05025a24207e28dd220ca8984d34079268b3eb31445476e020a0fb41a2f99f3e7d14c8b14af8de2b2899f3d09273737e872f30f4d440e4c0c080343a16c1
|
7
|
+
data.tar.gz: a23973ca4932fbd7f7e0d792da7b2c1dac40f180bea277bef4c33675afbedecdd15c1cad6ba45e637566ab6e0a9436cba55bf8f1e64b8118207f9ac2ce172c30
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
4.
|
1
|
+
4.1.0
|
@@ -0,0 +1,186 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module TMP
|
3
|
+
|
4
|
+
module CORE
|
5
|
+
|
6
|
+
def project_name name_string= nil
|
7
|
+
|
8
|
+
unless name_string.nil?
|
9
|
+
|
10
|
+
unless name_string.class <= String
|
11
|
+
raise ArgumentError,"invalid name string"
|
12
|
+
end
|
13
|
+
|
14
|
+
@project_name = name_string
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
@project_name || Dir.pwd.split(File::Separator).last.to_s
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
alias :project_folder :project_name
|
23
|
+
|
24
|
+
def tmpdir
|
25
|
+
::Dir.tmpdir
|
26
|
+
end
|
27
|
+
|
28
|
+
def default_folder_path
|
29
|
+
File.join( tmpdir , project_name )
|
30
|
+
end
|
31
|
+
|
32
|
+
@folder_path= nil
|
33
|
+
def tmp_folder_path path_string= nil
|
34
|
+
|
35
|
+
$stderr.puts('warning this is deprecated, will be removed in the further releases!(tmp gem)')
|
36
|
+
|
37
|
+
unless path_string.nil?
|
38
|
+
|
39
|
+
unless path_string.class <= String
|
40
|
+
raise ArgumentError,"invalid path class, must be string like"
|
41
|
+
end
|
42
|
+
|
43
|
+
@folder_path = File.absolute_path(path_string)
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
@folder_path || default_folder_path
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
alias :folder_path :tmp_folder_path
|
52
|
+
|
53
|
+
def tmp_folder_init
|
54
|
+
|
55
|
+
begin
|
56
|
+
|
57
|
+
Dir.mkdir tmp_folder_path
|
58
|
+
return true
|
59
|
+
|
60
|
+
rescue Errno::EEXIST
|
61
|
+
return false
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
alias :folder_init :tmp_folder_init
|
67
|
+
|
68
|
+
def write variable_name, data_object
|
69
|
+
|
70
|
+
$stderr.puts('warning this is deprecated, will be removed in the further releases!(tmp gem)')
|
71
|
+
|
72
|
+
tmp_folder_init
|
73
|
+
|
74
|
+
File.open( File.join(tmp_folder_path,variable_name.to_s) ,"w") do |file|
|
75
|
+
return file.write ::Marshal.dump(data_object)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
def block variable_name, *args, &block_obj
|
81
|
+
|
82
|
+
$stderr.puts('warning this is deprecated, will be removed in the further releases!(tmp gem)')
|
83
|
+
|
84
|
+
tmp_folder_init
|
85
|
+
|
86
|
+
options = args.select{|e|(e.class <= ::Hash)}.reduce({},:merge!)
|
87
|
+
args.select!{|e|!(e.class <= ::Hash)}
|
88
|
+
|
89
|
+
options[:file_extension] ||= options[:extension] || options[:ext]
|
90
|
+
file_name = variable_name.to_s
|
91
|
+
options[:file_mod] ||= options[:mod] || args[0].class <= ::String ? args[0] : nil
|
92
|
+
|
93
|
+
unless options[:file_extension].nil?
|
94
|
+
|
95
|
+
while options[:file_extension][0] == '.'
|
96
|
+
options[:file_extension][0]= ''
|
97
|
+
end
|
98
|
+
|
99
|
+
file_name += ".#{options[:file_extension]}"
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
if File.exist?(File.join( tmp_folder_path, file_name ))
|
104
|
+
options[:file_mod] ||= "r+"
|
105
|
+
else
|
106
|
+
options[:file_mod] ||= "w+"
|
107
|
+
end
|
108
|
+
|
109
|
+
begin
|
110
|
+
return File.open( File.join( tmp_folder_path, file_name ), options[:file_mod], &block_obj )
|
111
|
+
rescue Errno::ENOENT
|
112
|
+
var_file= File.new( File.join( tmp_folder_path, file_name ), options[:file_mod])
|
113
|
+
block_obj.call(var_file) unless block_obj.nil?
|
114
|
+
var_file.close
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
def read variable_name
|
120
|
+
|
121
|
+
$stderr.puts('warning this is deprecated, will be removed in the further releases!(tmp gem)')
|
122
|
+
|
123
|
+
unless File.exist?(File.join( tmp_folder_path,variable_name.to_s))
|
124
|
+
return nil
|
125
|
+
end
|
126
|
+
|
127
|
+
File.open( File.join( tmp_folder_path,variable_name.to_s ) ,"r+") do |file|
|
128
|
+
|
129
|
+
var= file.read
|
130
|
+
begin
|
131
|
+
return ::Marshal.load var
|
132
|
+
rescue
|
133
|
+
return var
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
def path variable_name,*args
|
141
|
+
|
142
|
+
$stderr.puts('warning this is deprecated, will be removed in the further releases!(tmp gem)')
|
143
|
+
|
144
|
+
options = args.select{|e|(e.class <= ::Hash)}.reduce({},:merge!)
|
145
|
+
args.select!{|e|!(e.class <= ::Hash)}
|
146
|
+
|
147
|
+
variable_name = variable_name.to_s
|
148
|
+
options[:file_extension] ||= options[:extension] || options[:ext]
|
149
|
+
unless options[:file_extension].nil?
|
150
|
+
while options[:file_extension][0] == '.'
|
151
|
+
options[:file_extension][0]= ''
|
152
|
+
end
|
153
|
+
variable_name += ".#{options[:file_extension]}"
|
154
|
+
end
|
155
|
+
|
156
|
+
tmp_folder_init
|
157
|
+
path_to_file= File.join( tmp_folder_path, variable_name )
|
158
|
+
unless File.exist?(path_to_file)
|
159
|
+
File.open( path_to_file ,"w") {|f| f.write(""); f.close }
|
160
|
+
end
|
161
|
+
return path_to_file
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
def purge_files
|
166
|
+
|
167
|
+
Dir.glob( File.join( tmp_folder_path,'**','*' ) ).map do |file_path|
|
168
|
+
|
169
|
+
begin
|
170
|
+
File.delete file_path
|
171
|
+
rescue Errno::ENOENT => ex
|
172
|
+
ex
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
alias :purge! :purge_files
|
180
|
+
alias :purge :purge_files
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
extend CORE
|
185
|
+
|
186
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module TMP
|
3
|
+
|
4
|
+
module DSLCore
|
5
|
+
|
6
|
+
def target_obj obj=nil
|
7
|
+
|
8
|
+
@target_obj= obj unless obj.nil?
|
9
|
+
@target_obj || ::TMP
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing( method, *args, &block )
|
14
|
+
|
15
|
+
if method.to_s.reverse[0] == '='
|
16
|
+
|
17
|
+
target_obj.__send__ :write, method.to_s.reverse.sub('=','').reverse, args.first
|
18
|
+
return args.first
|
19
|
+
|
20
|
+
else
|
21
|
+
|
22
|
+
unless block.nil?
|
23
|
+
|
24
|
+
return target_obj.__send__ :block, method, *args, &block
|
25
|
+
|
26
|
+
else
|
27
|
+
|
28
|
+
if method =~ /^\w+__path__$/
|
29
|
+
return target_obj.__send__ :path, method.to_s.sub!( /__path__$/,"" ),*args
|
30
|
+
else
|
31
|
+
return target_obj.__send__ :read, method
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
module DSL
|
43
|
+
|
44
|
+
extend ObjectExt
|
45
|
+
extend DSLCore
|
46
|
+
privatize
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
module SyntaxSugar
|
51
|
+
|
52
|
+
def __tmp__
|
53
|
+
::TMP::DSL
|
54
|
+
end
|
55
|
+
|
56
|
+
alias :__TMP__ :__tmp__
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
Object.__send__ :include, ::TMP::SyntaxSugar
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module TMP
|
3
|
+
|
4
|
+
class InstanceCore
|
5
|
+
|
6
|
+
include CORE
|
7
|
+
|
8
|
+
def initialize path_string
|
9
|
+
|
10
|
+
unless path_string.class <= String
|
11
|
+
raise ArgumentError, "path must be a string like type"
|
12
|
+
end
|
13
|
+
|
14
|
+
folder_path path_string
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
class InstanceDSL
|
21
|
+
|
22
|
+
extend ObjectExt
|
23
|
+
include DSLCore
|
24
|
+
|
25
|
+
def initialize path_string
|
26
|
+
target_obj InstanceCore.new( path_string )
|
27
|
+
end
|
28
|
+
|
29
|
+
privatize t: 'instance'
|
30
|
+
|
31
|
+
public
|
32
|
+
|
33
|
+
|
34
|
+
def tmp_class_instance_object
|
35
|
+
target_obj
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
class << self
|
41
|
+
|
42
|
+
def new *args
|
43
|
+
InstanceDSL.new *args
|
44
|
+
end
|
45
|
+
alias :init :new
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module TMP
|
3
|
+
|
4
|
+
module ObjectExt
|
5
|
+
|
6
|
+
def privatize opts= {}
|
7
|
+
|
8
|
+
unless opts.class <= Hash
|
9
|
+
raise ArgumentError,"invalid input for options"
|
10
|
+
end
|
11
|
+
|
12
|
+
%W[ e ex exc ].each do |str_sym|
|
13
|
+
opts[:exclude] ||= opts[str_sym.to_sym]
|
14
|
+
end
|
15
|
+
|
16
|
+
%W[ i in inc only methods ].each do |str_sym|
|
17
|
+
opts[:include] ||= opts[str_sym.to_sym]
|
18
|
+
end
|
19
|
+
|
20
|
+
%W[ t target ].each do |str_sym|
|
21
|
+
opts[:target] ||= opts[str_sym.to_sym]
|
22
|
+
end
|
23
|
+
|
24
|
+
opts[:target] ||= 's'
|
25
|
+
opts[:target]= opts[:target].to_s.downcase
|
26
|
+
|
27
|
+
unless opts[:target][0] == "s" || opts[:target][0] == "i"
|
28
|
+
%W[ singleton instance ].include?(opts[:target].to_s)
|
29
|
+
raise ArgumentError, [
|
30
|
+
"invalid options for target, you should use the following: ",
|
31
|
+
"\n\tsingleton for targeting the singleton class what is de",
|
32
|
+
"fault\n\tinstance for targeting the object instance methods."
|
33
|
+
].join
|
34
|
+
end
|
35
|
+
|
36
|
+
opts[:exclude] ||= []
|
37
|
+
|
38
|
+
if opts[:target][0] == 'i' && self.class <= Module
|
39
|
+
opts[:include] ||= self.instance_methods.map{|e| e.to_s }
|
40
|
+
else
|
41
|
+
opts[:target]= 's'
|
42
|
+
opts[:include] ||= self.methods.map{|e| e.to_s }
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
[:include,:exclude].each do |array_name|
|
47
|
+
|
48
|
+
unless opts[array_name].class <= Array
|
49
|
+
opts[array_name]= [ opts[array_name] ]
|
50
|
+
end
|
51
|
+
|
52
|
+
opts[array_name].map!{ |element| ( element.class == String ? element : element.to_s ) }
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
opts[:exclude].push('__send__').push('object_id')
|
57
|
+
|
58
|
+
if opts[:target][0] == 's'
|
59
|
+
|
60
|
+
self.instance_eval do
|
61
|
+
|
62
|
+
opts[:include].each do |sym|
|
63
|
+
|
64
|
+
unless opts[:exclude].include?(sym)
|
65
|
+
(class << self; self; end).__send__ :private, sym
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
elsif opts[:target][0] == 'i'
|
72
|
+
|
73
|
+
opts[:include].each do |sym|
|
74
|
+
|
75
|
+
unless opts[:exclude].include?(sym)
|
76
|
+
self.__send__ :private, sym
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
else
|
82
|
+
STDERR.puts "invalid target definition"
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
end
|
data/lib/tmp/instance.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
class TMP::Instance
|
2
|
+
|
3
|
+
attr_reader :tmpdir
|
4
|
+
|
5
|
+
def initialize(tmp_folder=nil)
|
6
|
+
@tmpdir = tmp_folder || get_system_tmpdir
|
7
|
+
end
|
8
|
+
|
9
|
+
def []=(file_name, dump_object)
|
10
|
+
open(file_name, 'w+') do |file|
|
11
|
+
file.write(Marshal.dump(dump_object))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](file_name)
|
16
|
+
open(file_name, 'r') do |f|
|
17
|
+
begin
|
18
|
+
Marshal.load(f.read)
|
19
|
+
rescue ArgumentError => ex
|
20
|
+
ex.message.to_s.include?('marshal') ? nil : raise(ex)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def open(file_name, *args)
|
26
|
+
|
27
|
+
file_path = path_for(file_name)
|
28
|
+
|
29
|
+
File.open(file_path, *args) do |f|
|
30
|
+
begin
|
31
|
+
f.flock(File::LOCK_EX)
|
32
|
+
yield(f)
|
33
|
+
ensure
|
34
|
+
f.flock(File::LOCK_UN)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
rescue Errno::ENOENT
|
39
|
+
File.open(file_path, 'a') {}
|
40
|
+
retry
|
41
|
+
end
|
42
|
+
|
43
|
+
def path_for(file_name)
|
44
|
+
File.join(tmpdir, file_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
def get_system_tmpdir
|
50
|
+
|
51
|
+
require 'tmpdir'
|
52
|
+
require 'securerandom'
|
53
|
+
tmp_dir_to_use = File.join(Dir.tmpdir,SecureRandom.uuid.to_s)
|
54
|
+
Dir.mkdir(tmp_dir_to_use) unless File.exist?(tmp_dir_to_use)
|
55
|
+
|
56
|
+
return tmp_dir_to_use
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
describe TMP::Instance do
|
3
|
+
|
4
|
+
let(:tmp_folder_path){'/tmp/folder/path'}
|
5
|
+
subject{ self.described_class.new(tmp_folder_path)}
|
6
|
+
|
7
|
+
before{ allow(Dir).to receive(:mkdir).and_return(true) }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
|
11
|
+
context 'when tmp folder path not given' do
|
12
|
+
|
13
|
+
subject{ self.described_class.new }
|
14
|
+
it 'should initialize, and set the default tmp folder by the system' do
|
15
|
+
|
16
|
+
require 'securerandom'
|
17
|
+
require 'tmpdir'
|
18
|
+
|
19
|
+
expect(SecureRandom).to receive(:uuid).and_return('uuid')
|
20
|
+
expect(subject.instance_variable_get(:@tmpdir)).to eq File.join(Dir.tmpdir,'uuid')
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when folder path passed to the initialization' do
|
27
|
+
|
28
|
+
it 'should return the path that passed on initialization' do
|
29
|
+
expect(subject.instance_variable_get(:@tmpdir)).to eq tmp_folder_path
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#tmpdir' do
|
37
|
+
|
38
|
+
it 'should return the @tmpdir content' do
|
39
|
+
expect( subject.tmpdir ).to eq subject.instance_variable_get(:@tmpdir)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'File Accessors' do
|
45
|
+
|
46
|
+
let(:file_mock){double('file mock')}
|
47
|
+
let(:sample_object){{name: 'test',content: 'Sample Object'}}
|
48
|
+
|
49
|
+
describe '#[]=' do
|
50
|
+
|
51
|
+
context 'when block not given' do
|
52
|
+
|
53
|
+
it 'should marshal dump to a temp file by the given name' do
|
54
|
+
|
55
|
+
expect( subject ).to receive(:open).with( sample_object[:name], 'w+' ).and_yield(file_mock)
|
56
|
+
expect( file_mock ).to receive(:write).with(Marshal.dump(sample_object[:content]))
|
57
|
+
expect( subject[sample_object[:name]]= sample_object[:content] ).to eq sample_object[:content]
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#[]' do
|
66
|
+
|
67
|
+
context 'when valid marshal data in the file' do
|
68
|
+
|
69
|
+
it 'should read a tmp file content by unserialize and returning object' do
|
70
|
+
|
71
|
+
expect( subject ).to receive(:open).with(sample_object[:name],'r').and_yield(file_mock)
|
72
|
+
expect( file_mock ).to receive(:read).and_return(Marshal.dump(sample_object[:content]))
|
73
|
+
expect( subject[sample_object[:name]] ).to eq sample_object[:content]
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'when marshal data too short' do
|
80
|
+
|
81
|
+
it 'should return a nil' do
|
82
|
+
|
83
|
+
expect( subject ).to receive(:open).with(sample_object[:name],'r').and_yield(file_mock)
|
84
|
+
expect( file_mock ).to receive(:read).and_return('')
|
85
|
+
expect( subject[sample_object[:name]] ).to eq nil
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#open' do
|
94
|
+
|
95
|
+
let(:file_open_mode){'r+'}
|
96
|
+
|
97
|
+
before do
|
98
|
+
allow(file_mock).to receive(:flock)
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'when file exist' do
|
102
|
+
it 'should send lock signal thought the os for the file for a given block' do
|
103
|
+
|
104
|
+
expect(File).to receive(:open).with(File.join(tmp_folder_path,sample_object[:name]),file_open_mode).and_yield(file_mock)
|
105
|
+
|
106
|
+
expect(file_mock).to receive(:flock).with(File::LOCK_EX)
|
107
|
+
expect(file_mock).to receive(:flock).with(File::LOCK_UN)
|
108
|
+
|
109
|
+
block_ran = false
|
110
|
+
subject.open( sample_object[:name], file_open_mode ) do |f|
|
111
|
+
expect(f).to eq file_mock
|
112
|
+
block_ran = true
|
113
|
+
end
|
114
|
+
|
115
|
+
expect(block_ran).to be_truthy
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'when file not exist' do
|
121
|
+
|
122
|
+
it 'should rescue ENOENT exception, create an empty file and retry' do
|
123
|
+
|
124
|
+
expect(File).to receive(:open).with(File.join(tmp_folder_path,sample_object[:name]),file_open_mode).once.and_raise(Errno::ENOENT)
|
125
|
+
expect(File).to receive(:open).with(File.join(tmp_folder_path,sample_object[:name]),'a')
|
126
|
+
|
127
|
+
expect(File).to receive(:open).with(File.join(tmp_folder_path,sample_object[:name]),file_open_mode).and_yield(file_mock)
|
128
|
+
|
129
|
+
expect(file_mock).to receive(:flock).with(File::LOCK_EX)
|
130
|
+
expect(file_mock).to receive(:flock).with(File::LOCK_UN)
|
131
|
+
|
132
|
+
block_ran = false
|
133
|
+
subject.open( sample_object[:name], file_open_mode ) do |f|
|
134
|
+
expect(f).to eq file_mock
|
135
|
+
block_ran = true
|
136
|
+
end
|
137
|
+
|
138
|
+
expect(block_ran).to be_truthy
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#path_for' do
|
148
|
+
|
149
|
+
it 'should return a full path for a given name that is located in the current tmp directory' do
|
150
|
+
expect(subject.path_for(sample_object[:name])).to eq File.join(tmp_folder_path,sample_object[:name])
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tmp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -75,15 +75,16 @@ files:
|
|
75
75
|
- Rakefile
|
76
76
|
- VERSION
|
77
77
|
- bin/console
|
78
|
-
- examples/blocks.rb
|
79
|
-
- examples/config_exmpl.rb
|
80
|
-
- examples/instance.rb
|
81
|
-
- examples/path.rb
|
82
|
-
- examples/sugar_use.rb
|
83
|
-
- examples/test.rb
|
84
78
|
- files.rb
|
85
79
|
- lib/tmp.rb
|
80
|
+
- lib/tmp/deprecated/core.rb
|
81
|
+
- lib/tmp/deprecated/dci.rb
|
82
|
+
- lib/tmp/deprecated/dsl.rb
|
83
|
+
- lib/tmp/deprecated/init.rb
|
84
|
+
- lib/tmp/deprecated/sup.rb
|
85
|
+
- lib/tmp/instance.rb
|
86
86
|
- spec/spec_helper.rb
|
87
|
+
- spec/tmp/instance_spec.rb
|
87
88
|
- tmp.gemspec
|
88
89
|
homepage: https://github.com/adamluzsi/tmp
|
89
90
|
licenses:
|
@@ -111,3 +112,4 @@ specification_version: 4
|
|
111
112
|
summary: DSL for temporally files read/write in the object oriented way (system tmp)
|
112
113
|
test_files:
|
113
114
|
- spec/spec_helper.rb
|
115
|
+
- spec/tmp/instance_spec.rb
|
data/examples/blocks.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'tmp'
|
2
|
-
|
3
|
-
TMP.purge!
|
4
|
-
# using blocks is simply as this
|
5
|
-
__tmp__.some_file_name "w+" do |file|
|
6
|
-
file.write Random.rand(1000...9999)
|
7
|
-
end
|
8
|
-
|
9
|
-
__tmp__.some_file_name do |file|
|
10
|
-
puts file.readline #> some random number
|
11
|
-
end
|
12
|
-
|
13
|
-
# reopen the new file is also simple
|
14
|
-
__tmp__.some_file_name do |file|
|
15
|
-
|
16
|
-
while line = file.gets
|
17
|
-
#> some random number same as above
|
18
|
-
puts line
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
# you can set the file mod if you like, by default it's based on file e
|
24
|
-
__tmp__.some_file_name "w+" do |file|
|
25
|
-
|
26
|
-
puts file.readlines.inspect #> [] empty array because the file got w+ command
|
27
|
-
|
28
|
-
file.write "Hello world!"
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
__tmp__.some_file_name do |file|
|
33
|
-
puts file.gets.inspect #> "Hello world!"
|
34
|
-
end
|
35
|
-
|
36
|
-
puts __tmp__.some_file_name__path__
|
37
|
-
|
38
|
-
puts __tmp__.some_file_name.inspect #> it's a string from the file we made
|
39
|
-
|
40
|
-
|
41
|
-
TMP.purge!.inspect
|
42
|
-
|
43
|
-
#> file_with_extension.rb
|
44
|
-
__tmp__.file_with_extension ext: 'rb' do |file|
|
45
|
-
file.write "hello world!"
|
46
|
-
end
|
47
|
-
|
48
|
-
__tmp__.file_with_extension ext: 'rb' do |file|
|
49
|
-
puts file.read
|
50
|
-
end
|
data/examples/config_exmpl.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'tmp'
|
2
|
-
|
3
|
-
TMP.folder_path File.expand_path(File.join(File.dirname(__FILE__),'tmp_folder'))
|
4
|
-
|
5
|
-
# except the default folder path, everything is opt able
|
6
|
-
puts "","default tmp folder:",
|
7
|
-
TMP.default_folder_path,"",
|
8
|
-
|
9
|
-
"new tmp folder for use:",
|
10
|
-
TMP.folder_path,"",
|
11
|
-
|
12
|
-
"the project name what is used:",
|
13
|
-
TMP.project_name,""
|
14
|
-
|
15
|
-
# you can trigger the tmp folder generating by the following
|
16
|
-
TMP.folder_init #> || tmp_folder_init
|
17
|
-
|
18
|
-
# or you can use syntax sugar!
|
19
|
-
__tmp__.hello = { hello: 'world'}
|
20
|
-
|
21
|
-
# defined variable
|
22
|
-
puts __tmp__.hello #> { hello: 'world'}
|
23
|
-
|
data/examples/instance.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'tmp'
|
2
|
-
|
3
|
-
|
4
|
-
tmp_instance= TMP.new( File.expand_path(File.join(File.dirname(__FILE__),'tmp_folder')) )
|
5
|
-
|
6
|
-
# use the same as the TMP::DSL aka tmp method
|
7
|
-
|
8
|
-
# let's set a string for this one
|
9
|
-
tmp_instance.test= "hello"
|
10
|
-
|
11
|
-
# get the string what we just set now
|
12
|
-
tmp_instance.test #> "hello"
|
13
|
-
|
14
|
-
# you can get the tmp class instance obj like this:
|
15
|
-
tmp_instance.tmp_class_instance_object #> return the tmp_instance
|
16
|
-
|
17
|
-
# return the tmp folder path
|
18
|
-
tmp_instance.tmp_class_instance_object.folder_path
|
19
|
-
|
20
|
-
# Remember this instance use different folder thant the main TMP::DSL
|
21
|
-
__tmp__.test.inspect #> nil, because it was never used before
|
22
|
-
#> the tmp method is same like invoke the TMP::DSL module
|
23
|
-
|
24
|
-
tmp_instance.test_file do |file|
|
25
|
-
file.write "hello world!"
|
26
|
-
end
|
27
|
-
|
28
|
-
tmp_instance.test_file do |file|
|
29
|
-
file.read
|
30
|
-
end
|
data/examples/path.rb
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
require 'tmp'
|
2
|
-
|
3
|
-
puts __tmp__.random #> nil or empty string because never used or just fresh inited file
|
4
|
-
puts __tmp__.random__path__ #> path to random named file,
|
5
|
-
# if not exist, make an empty one
|
6
|
-
|
7
|
-
puts __tmp__.random__path__ ext: 'rb' #> path to random named file,
|
data/examples/sugar_use.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'tmp'
|
2
|
-
|
3
|
-
TMP.write :test2, {hello: 'world'}
|
4
|
-
puts TMP.read(:test2)
|
5
|
-
|
6
|
-
#or you can use syntax sugar!
|
7
|
-
__tmp__.hello= { hello: 'world'}
|
8
|
-
|
9
|
-
# defined variable
|
10
|
-
puts __tmp__.hello #> { hello: 'world'}
|
11
|
-
|
12
|
-
# undefined variable
|
13
|
-
puts __tmp__.sup #> nil
|
14
|
-
|
15
|
-
TMP.purge!
|
16
|
-
|
17
|
-
# call after tmp is purged
|
18
|
-
puts __tmp__.hello #> nil
|