tmp 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75c96b37c641f435b2d757b2edfeae5ae655eedf
4
- data.tar.gz: e6fd985a0f4b3c9a3478210e184294a71abdc690
3
+ metadata.gz: c1bda2dbca58011f1df3c3a20b42576dfec912cf
4
+ data.tar.gz: 77dd1029623db6de2a853f3da7b111db0177bd19
5
5
  SHA512:
6
- metadata.gz: 072c71935cc8f913c8e6ecc5ca6a31feaaa6c5b1c4d3c24066cf54de7c0f0f9cb2d258e08d8f16e8e3eb0017a61ecdc902bd2103cae17ce36f203d3bba337326
7
- data.tar.gz: 46476e5f722f24c911f7e77e09848cc3d09424c6625380e781241a5e53701dbf54edd89604a1750e533c7f546b2344db4f1c1136d7f4616a29a4b270e5d23109
6
+ metadata.gz: 49f7c44baa417d6db5b256089996c71889c8244c881fde8828beaa7aa305ad94c224ff5558facf2c70dd1e5193ee4b7dbc6fc2a8c1a59ee868e8c772017168a4
7
+ data.tar.gz: 67b8dfa1432a925ac6c22e4eb04e69c7bf4848ddfb283e5758c49958172c8e084a5f8299c609708b0bcc00662dc14694e00afc34915f6c09c020b5dfcebb04aa
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tmp (3.0.0)
4
+ tmp (3.1.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -23,7 +23,11 @@ The real object(module) being called all the time is the TMP::DSL
23
23
  The DSL will make easy the job on you but you can use the default methods alike:
24
24
 
25
25
  * TMP.write( file_name, object )
26
- * TMP.read( file_name ) #> object
26
+ * object
27
+ * TMP.read( file_name )
28
+ * object
29
+ * TMP.path( file_name )
30
+ * string
27
31
 
28
32
  ```ruby
29
33
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.1.0
@@ -1,17 +1,17 @@
1
1
  require 'tmp'
2
2
 
3
- tmp.purge!
3
+ TMP.purge!
4
4
  # using blocks is simply as this
5
- tmp.some_file_name "w+" do |file|
5
+ __tmp__.some_file_name "w+" do |file|
6
6
  file.write Random.rand(1000...9999)
7
7
  end
8
8
 
9
- tmp.some_file_name do |file|
9
+ __tmp__.some_file_name do |file|
10
10
  puts file.readline #> some random number
11
11
  end
12
12
 
13
13
  # reopen the new file is also simple
14
- tmp.some_file_name do |file|
14
+ __tmp__.some_file_name do |file|
15
15
 
16
16
  while line = file.gets
17
17
  #> some random number same as above
@@ -21,7 +21,7 @@ tmp.some_file_name do |file|
21
21
  end
22
22
 
23
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|
24
+ __tmp__.some_file_name "w+" do |file|
25
25
 
26
26
  puts file.readlines.inspect #> [] empty array because the file got w+ command
27
27
 
@@ -29,10 +29,22 @@ tmp.some_file_name "w+" do |file|
29
29
 
30
30
  end
31
31
 
32
- tmp.some_file_name do |file|
32
+ __tmp__.some_file_name do |file|
33
33
  puts file.gets.inspect #> "Hello world!"
34
34
  end
35
35
 
36
- puts tmp.some_file_name__path__
36
+ puts __tmp__.some_file_name__path__
37
37
 
38
- puts tmp.some_file_name.inspect #> it's a string from the file we made
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
@@ -16,8 +16,8 @@ puts "","default tmp folder:",
16
16
  TMP.folder_init #> || tmp_folder_init
17
17
 
18
18
  # or you can use syntax sugar!
19
- tmp.hello = { hello: 'world'}
19
+ __tmp__.hello = { hello: 'world'}
20
20
 
21
21
  # defined variable
22
- puts tmp.hello #> { hello: 'world'}
22
+ puts __tmp__.hello #> { hello: 'world'}
23
23
 
@@ -18,5 +18,13 @@ tmp_instance.tmp_class_instance_object #> return the tmp_instance
18
18
  tmp_instance.tmp_class_instance_object.folder_path
19
19
 
20
20
  # Remember this instance use different folder thant the main TMP::DSL
21
- tmp.test.inspect #> nil, because it was never used before
21
+ __tmp__.test.inspect #> nil, because it was never used before
22
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
@@ -1,5 +1,7 @@
1
1
  require 'tmp'
2
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,
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
5
  # if not exist, make an empty one
6
+
7
+ puts __tmp__.random__path__ ext: 'rb' #> path to random named file,
@@ -4,15 +4,15 @@ TMP.write :test2, {hello: 'world'}
4
4
  puts TMP.read(:test2)
5
5
 
6
6
  #or you can use syntax sugar!
7
- tmp.hello= { hello: 'world'}
7
+ __tmp__.hello= { hello: 'world'}
8
8
 
9
9
  # defined variable
10
- puts tmp.hello #> { hello: 'world'}
10
+ puts __tmp__.hello #> { hello: 'world'}
11
11
 
12
12
  # undefined variable
13
- puts tmp.sup #> nil
13
+ puts __tmp__.sup #> nil
14
14
 
15
15
  TMP.purge!
16
16
 
17
17
  # call after tmp is purged
18
- puts tmp.hello #> nil
18
+ puts __tmp__.hello #> nil
@@ -0,0 +1 @@
1
+ hello world!
@@ -75,13 +75,38 @@ module TMP
75
75
 
76
76
  def block variable_name, *args, &block_obj
77
77
 
78
- if File.exist? File.join( tmp_folder_path,variable_name.to_s )
79
- args[0] ||= "r+"
78
+ tmp_folder_init
79
+
80
+ options = args.select{|e|(e.class <= ::Hash)}.reduce({},:merge!)
81
+ args.select!{|e|!(e.class <= ::Hash)}
82
+
83
+ options[:file_extension] ||= options[:extension] || options[:ext]
84
+ file_name = variable_name.to_s
85
+ options[:file_mod] ||= options[:mod] || args[0].class <= ::String ? args[0] : nil
86
+
87
+ unless options[:file_extension].nil?
88
+
89
+ while options[:file_extension][0] == '.'
90
+ options[:file_extension][0]= ''
91
+ end
92
+
93
+ file_name += ".#{options[:file_extension]}"
94
+
95
+ end
96
+
97
+ if File.exist?(File.join( tmp_folder_path, file_name ))
98
+ options[:file_mod] ||= "r+"
80
99
  else
81
- args[0] ||= "w+"
100
+ options[:file_mod] ||= "w+"
82
101
  end
83
102
 
84
- return File.open( File.join( tmp_folder_path,variable_name.to_s ), args[0], &block_obj)
103
+ begin
104
+ return File.open( File.join( tmp_folder_path, file_name ), options[:file_mod], &block_obj )
105
+ rescue Errno::ENOENT
106
+ var_file= File.new( File.join( tmp_folder_path, file_name ), options[:file_mod])
107
+ block_obj.call(var_file) unless block_obj.nil?
108
+ var_file.close
109
+ end
85
110
 
86
111
  end
87
112
 
@@ -104,10 +129,22 @@ module TMP
104
129
 
105
130
  end
106
131
 
107
- def path variable_name
132
+ def path variable_name,*args
133
+
134
+ options = args.select{|e|(e.class <= ::Hash)}.reduce({},:merge!)
135
+ args.select!{|e|!(e.class <= ::Hash)}
136
+
137
+ variable_name = variable_name.to_s
138
+ options[:file_extension] ||= options[:extension] || options[:ext]
139
+ unless options[:file_extension].nil?
140
+ while options[:file_extension][0] == '.'
141
+ options[:file_extension][0]= ''
142
+ end
143
+ variable_name += ".#{options[:file_extension]}"
144
+ end
108
145
 
109
146
  tmp_folder_init
110
- path_to_file= File.join(tmp_folder_path,variable_name.to_s)
147
+ path_to_file= File.join( tmp_folder_path, variable_name )
111
148
  unless File.exist?(path_to_file)
112
149
  File.open( path_to_file ,"w") {|f| f.write(""); f.close }
113
150
  end
@@ -117,11 +154,12 @@ module TMP
117
154
 
118
155
  def purge_files
119
156
 
120
- Dir.glob( File.join( tmp_folder_path,'**','*' ) ).each do |file_path|
157
+ Dir.glob( File.join( tmp_folder_path,'**','*' ) ).map do |file_path|
121
158
 
122
159
  begin
123
160
  File.delete file_path
124
- rescue Errno::ENOENT
161
+ rescue Errno::ENOENT => ex
162
+ ex
125
163
  end
126
164
 
127
165
  end
@@ -26,7 +26,7 @@ module TMP
26
26
  else
27
27
 
28
28
  if method =~ /^\w+__path__$/
29
- return target_obj.__send__ :path, method.to_s.sub!( /__path__$/,"" )
29
+ return target_obj.__send__ :path, method.to_s.sub!( /__path__$/,"" ),*args
30
30
  else
31
31
  return target_obj.__send__ :read, method
32
32
  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: 3.0.0
4
+ version: 3.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: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -66,6 +66,7 @@ files:
66
66
  - examples/test.rb
67
67
  - examples/tmp_folder/hello
68
68
  - examples/tmp_folder/test
69
+ - examples/tmp_folder/test_file
69
70
  - files.rb
70
71
  - lib/tmp.rb
71
72
  - lib/tmp/core.rb