fun_with_files 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,47 @@
1
1
  = fun_with_files
2
2
 
3
- Description goes here.
3
+ FunWith::Files adds a bit of whimsy to your file manipulations, if that's what you're looking for.
4
+
5
+ To the code!
6
+
7
+ require 'fun_with_files'
8
+
9
+ include FunWith::Files
10
+
11
+ class Project; end
12
+
13
+ RootPath.rootify( Project, "/home/user/path/to/project" )
14
+
15
+ Project.root # => <FunWith::Files::FilePath:/home/user/path/to/project>
16
+ Project.root("hello", "subdir") # => <FunWith::Files::FilePath:/home/user/path/to/project/hello/subdir>
17
+
18
+ home = FilePath.home
19
+ music = FilePath.home( "Music" )
20
+
21
+ music.touch_dir( "sinead_o_connor" ) do |dir|
22
+ dir.touch_dir( "sean_nos_nua" ) do |subdir|
23
+ subdir.touch( "03-lord_franklin.mp3" )
24
+ end
25
+
26
+ dir.touch_dir( "theology" ) do |subdir|
27
+ subdir.touch( "05-darker_than_blue.mp3" )
28
+ end
29
+ end
30
+
31
+ mp3s = music.glob( "**/*.mp3" ) # => [<FunWith::Files::FilePath:/home/user/Music/sinead_o_connor/sean_nos_nua/03-lord_franklin.mp3>,
32
+ # <FunWith::Files::FilePath:/home/user/Music/sinead_o_connor/theology/05-darker_than_blue.mp3>]
33
+ mp3s.last.write( Hypothetical::MP3::Source.new.read )
34
+
35
+ # whole buchcha other goodies, yet to be documented.
36
+
37
+
38
+
39
+
4
40
 
5
41
  == Contributing to fun_with_files
6
-
42
+
43
+ Boilerplate from Jeweler, but seems to make sense.
44
+
7
45
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
46
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
47
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -187,7 +187,7 @@ module FunWith
187
187
  self.glob( "**", "*" ).length == 0
188
188
  end
189
189
  end
190
-
190
+
191
191
  # Does not return a filepath
192
192
  def basename_no_ext
193
193
  self.basename.to_s.split(".")[0..-2].join(".")
@@ -239,28 +239,97 @@ module FunWith
239
239
  # in an argument, but it should always be the same value for
240
240
  # a given set of files.
241
241
  SUCC_DIGIT_COUNT = 6
242
- def succ( digit_count = SUCC_DIGIT_COUNT )
243
- chunks = basename.to_s.split(".")
244
- if chunks.length == 1 # not being counted, no file extension.
245
- chunks.push( "0" * digit_count )
242
+ def succ( opts = { digit_count: SUCC_DIGIT_COUNT, timestamp: false } )
243
+ if opts[:timestamp]
244
+ timestamp = Time.now.strftime("%Y%m%d%H%M%S%L")
245
+ digit_count = timestamp.length
246
+ else
247
+ timestamp = false
248
+ digit_count = opts[:digit_count]
249
+ end
250
+
251
+ chunks = self.basename.to_s.split(".")
252
+ # not yet sequence stamped, no file extension.
253
+ if chunks.length == 1
254
+ if timestamp
255
+ chunks.push( timestamp )
256
+ else
257
+ chunks.push( "0" * digit_count )
258
+ end
259
+ # sequence stamp before file extension
246
260
  elsif match_data = chunks[-2].match( /^(\d{#{digit_count}})$/ )
247
- i = match_data[1].to_i + 1
248
- chunks[-2] = sprintf("%0#{digit_count}i", i)
249
- elsif match_data = chunks[-1].match( /^(\d{#{digit_count}})$/ )
250
- i = match_data[1].to_i + 1
251
- chunks[-1] = sprintf("%0#{digit_count}i", i)
252
- else # not being counted, has file extension
253
- chunks = [chunks[0..-2], ("0" * digit_count), chunks[-1]].flatten
261
+ if timestamp
262
+ chunks[-2] = timestamp
263
+ else
264
+ i = match_data[1].to_i + 1
265
+ chunks[-2] = sprintf("%0#{digit_count}i", i)
266
+ end
267
+ # try to match sequence stamp to end of filename
268
+ elsif match_data = chunks[-1].match( /^(\d{#{digit_count}})$/ )
269
+ if timestamp
270
+ chunks[-1] = timestamp
271
+ else
272
+ i = match_data[1].to_i + 1
273
+ chunks[-1] = sprintf("%0#{digit_count}i", i)
274
+ end
275
+ # not yet sequence_stamped, has file extension
276
+ else
277
+ chunks = [chunks[0..-2], (timestamp ? timestamp : "0" * digit_count), chunks[-1]].flatten
254
278
  end
255
279
 
256
280
  self.up.join( chunks.join(".") )
257
281
  end
258
- end
259
282
 
260
- # TODO: succession : enumerates a sequence of files that get passed
261
- # to a block in order.
262
283
 
263
- # TODO: succ_last : find the last existing file of the given sequence.
264
- # TODO: succ_next : find the first free file of the given sequence
284
+ # TODO: succession : enumerates a sequence of files that get passed
285
+ # to a block in order.
286
+ def succession( opts = { digit_count: SUCC_DIGIT_COUNT, timestamp: false } )
287
+ if opts[:timestamp]
288
+ timestamp = Time.now.strftime("%Y%m%d%H%M%S%L")
289
+ digit_count = timestamp.length
290
+ else
291
+ timestamp = false
292
+ digit_count = opts[:digit_count]
293
+ end
294
+
295
+ chunks = self.basename.to_s.split(".")
296
+ glob_stamp_matcher = '[0-9]' * digit_count
297
+
298
+ # unstamped filename, no extension
299
+ if chunks.length == 1
300
+ original = chunks.first
301
+ stamped = [original, glob_stamp_matcher].join(".")
302
+ # stamped filename, no extension
303
+ elsif chunks[-1].match( /^\d{#{digit_count}}$/ )
304
+ original = chunks[0..-2].join(".")
305
+ stamped = [original, glob_stamp_matcher].join(".")
306
+ # stamped filename, has extension
307
+ elsif chunks[-2].match( /^\d{#{digit_count}}$/ )
308
+ original = [chunks[0..-3], chunks.last].flatten.join(".")
309
+ stamped = [chunks[0..-3], glob_stamp_matcher, chunks.last].join(".")
310
+ # unstamped filename, has extension
311
+ else
312
+ original = chunks.join(".")
313
+ stamped = [ chunks[0..-2], glob_stamp_matcher, chunks[-1] ].flatten.join(".")
314
+ end
315
+
316
+ [self.dirname.glob(original), self.dirname.glob(stamped)].flatten
317
+ end
318
+
319
+
320
+
321
+ # TODO: succ_last : find the last existing file of the given sequence.
322
+ # TODO: succ_next : find the first free file of the given sequence
323
+
324
+
325
+ # File manipulation
326
+ def rename( filename )
327
+
328
+ end
329
+
330
+ def rename_all( pattern, gsubbed )
331
+
332
+ end
333
+ end
265
334
  end
266
335
  end
@@ -79,5 +79,66 @@ class TestFilePath < Test::Unit::TestCase
79
79
  end
80
80
  end
81
81
 
82
-
82
+ context "test sequence" do
83
+ setup do
84
+ @tmp_dir = FunWith::Files.root( 'test', 'tmp' )
85
+ end
86
+
87
+ teardown do
88
+ `rm -rf #{@tmp_dir.join('*')}`
89
+ end
90
+
91
+ should "sequence files nicely" do
92
+ seqfile = @tmp_dir.join("sequence.txt")
93
+
94
+ 10.times do |i|
95
+ seqfile.write( i.to_s )
96
+ seqfile = seqfile.succ
97
+ end
98
+
99
+ assert @tmp_dir.join("sequence.txt").exist?
100
+ assert @tmp_dir.join("sequence.000000.txt").exist?
101
+ assert @tmp_dir.join("sequence.000008.txt").exist?
102
+
103
+ assert_equal "0", @tmp_dir.join("sequence.txt").read
104
+ assert_equal "9", @tmp_dir.join("sequence.000008.txt").read
105
+ end
106
+
107
+ should "sequence files with custom stamp length" do
108
+ seqfile = @tmp_dir.join("sequence.txt")
109
+
110
+ 10.times do |i|
111
+ seqfile.write( i.to_s )
112
+ seqfile = seqfile.succ( digit_count: 4 )
113
+ end
114
+
115
+ assert @tmp_dir.join("sequence.txt").exist?
116
+ assert @tmp_dir.join("sequence.0000.txt").exist?
117
+ assert @tmp_dir.join("sequence.0008.txt").exist?
118
+
119
+ assert_equal "0", @tmp_dir.join("sequence.txt").read
120
+ assert_equal "9", @tmp_dir.join("sequence.0008.txt").read
121
+ end
122
+
123
+ should "sequence files with datestamps" do
124
+ seqfile = @tmp_dir.join("sequence.txt")
125
+
126
+ 10.times do |i|
127
+ seqfile.write( i.to_s )
128
+ seqfile = seqfile.succ( timestamp: true )
129
+ sleep(0.002)
130
+ end
131
+
132
+ files = seqfile.succession( timestamp: true )
133
+ assert files.length == 10
134
+
135
+ files.each_with_index do |file, i|
136
+ assert file.exist?
137
+ assert_equal i.to_s, file.read
138
+ end
139
+
140
+ file_name_strings = files.map(&:to_s)
141
+ assert_equal file_name_strings[1..-1], file_name_strings[1..-1].sort
142
+ end
143
+ end
83
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fun_with_files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-05-06 00:00:00.000000000 Z
12
+ date: 2013-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
@@ -138,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  segments:
140
140
  - 0
141
- hash: -2333632354630146614
141
+ hash: -4306784684161945434
142
142
  required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  none: false
144
144
  requirements: