fast 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,11 +6,13 @@ Library is pure Ruby 1.8.7, no FileUtils nor other dependencies (except Metafun
6
6
 
7
7
  == Installation
8
8
 
9
- Don't install right now, as 13-09-2011 this is pre-alpha, but in intensive development.
9
+ gem install fast
10
+
11
+ Cucumber features still to do, but specs are stable (2011-12-11)
10
12
 
11
13
  == Philosophy
12
14
 
13
- <tt>Fast</tt> embraces the more straightforward view of files as strings of data and directories as arrays of files/directories. Some arguments:
15
+ <tt>Fast</tt> embraces the more straightforward view of files as strings of data and directories as arrays of files/directories. Some reasons:
14
16
 
15
17
  * It is more realistic in everyday usage
16
18
  * It makes them more object-like (and thus, more friendly to OOP)
@@ -20,8 +22,8 @@ Don't install right now, as 13-09-2011 this is pre-alpha, but in intensive devel
20
22
  <tt>Fast::Dir</tt> is a subclass of <tt>Array</tt>, usable as a hash, and <tt>Fast::File</tt> if a subclass of String.
21
23
 
22
24
  == Quick notes
23
- * Move all Fast code to fast/main.rb. Update Metafun/Delegator so it will inject the #delegate method into every Object instance. Reduce the "fast.rb" to a call to external libraries, fast/main and DSLization via #delegate.
24
- * Describe the SubSetter pattern for filtering.
25
+ * Read bytes as binary ASCII-8BIT always and then try to perform an heuristic conversion, if there is any reasonable way to do it. Otherwise, leave it to the user. Google: "ruby string encode utf-8 ascii" for some good readings.
26
+ * Apply the SubSetter pattern for filtering instead of FileFilter and DirFilter ad described in http://xaviervia.com.ar/patterns/sub-setter
25
27
  * File lists as returned by Fast::Dir#list and Fast::Dir#dirs will be filtered after the list is retrieved by a set of filtering methods of the returned list, which should be an instance of Fast::Dir.
26
28
  * An instance of Fast::Dir should be possible to be created from a Array.
27
29
  * The path can be setted indirectly by any method of Fast::File instances, and the same works for Dir. This is fine because allows for very quick calls, but once an instance gets a path setted it should be fixed and raise an exception in case some other method call is trying to change it.
data/lib/fast/dir.rb CHANGED
@@ -44,22 +44,37 @@ module Fast
44
44
  self
45
45
  end
46
46
 
47
- # Creates the dir, if it doesn't exist. Otherwise remains silent
47
+ # Creates the dir, if it doesn't exist. Otherwise raises an ArgumentException
48
48
  # Returns the last dir path passed as argument
49
49
  def create *args
50
- raise ArgumentError, "No arguments passed, at least one is required" if args.empty?
51
50
  if args.length > 0
52
51
  return_me = nil
53
52
  args.each do |path|
53
+ raise ArgumentError, "Dir '#{path}' already exists" if Dir.new.exist? path
54
54
  return_me = do_create path
55
55
  end
56
56
  return return_me
57
57
  else
58
- do_create
58
+ raise ArgumentError, "No arguments passed, at least one is required" unless @path
59
+ raise ArgumentError, "Dir '#{@path}' already exists" if Dir.new.exist? @path
60
+ do_create @path
61
+ end
62
+ end
63
+
64
+ # Creates the dir, if it doesn't exist. Otherwise remains silent
65
+ # Returns the last dir path passed as argument
66
+ def create! *args
67
+ if args.length > 0
68
+ return_me = nil
69
+ args.each do |path|
70
+ return_me = do_create path
71
+ end
72
+ return return_me
73
+ else
74
+ raise ArgumentError, "No arguments passed, at least one is required" unless @path
75
+ do_create @path
59
76
  end
60
77
  end
61
-
62
- alias :create! :create
63
78
 
64
79
  # Deletes the directory along with all its content. Powerful, simple, risky!
65
80
  # Many arguments can be passed
@@ -78,6 +93,7 @@ module Fast
78
93
  alias :destroy :delete
79
94
  alias :del :delete
80
95
  alias :unlink :delete
96
+ alias :remove :delete
81
97
 
82
98
  # Like #delete, but raises no error if some directory is missing
83
99
  def delete! *args
@@ -100,6 +116,8 @@ module Fast
100
116
  end
101
117
  end
102
118
 
119
+ alias :remove! :delete!
120
+
103
121
  # Checks for existence. True if the directory exists, false otherwise
104
122
  def exist? path = nil
105
123
  @path = normalize path if path
@@ -107,6 +125,45 @@ module Fast
107
125
  end
108
126
 
109
127
  alias :exists? :exist?
128
+
129
+ # Returns true if all passed dirs exist, false otherwise
130
+ def exist_all? *args
131
+ unless args.empty?
132
+ args.each do |path|
133
+ return false if not Dir.new.exist? path
134
+ end
135
+ return true
136
+ else
137
+ exist?
138
+ end
139
+ end
140
+
141
+ # Returns true if any of passed dirs exists, false otherwise
142
+ def exist_any? *args
143
+ unless args.empty?
144
+ args.each do |path|
145
+ return true if Dir.new.exist? path
146
+ end
147
+ return false
148
+ else
149
+ exist?
150
+ end
151
+ end
152
+
153
+ # Return a list with the existing dirs path
154
+ # Note: This should be delegated to the SubSetter::Fast::Dir
155
+ def exist_which *args
156
+ if args.empty?
157
+ raise ArgumentError, "Wrong number of arguments, at least one dir should be passed"
158
+ end
159
+
160
+ existing_ones = []
161
+ args.each do |path|
162
+ existing_ones << path if Dir.new.exist? path
163
+ end
164
+
165
+ return existing_ones
166
+ end
110
167
 
111
168
  # Returns a String brief of the dir
112
169
  def to_s
@@ -183,6 +240,44 @@ module Fast
183
240
  Dir.new.delete target
184
241
  end
185
242
 
243
+ def copy *args
244
+ if args.length > 1
245
+ current, target = *args
246
+ @path = normalize current
247
+ target = Dir.new target
248
+ else
249
+ target = Dir.new args.first
250
+ end
251
+
252
+ target.create
253
+ list do |entry|
254
+ if File.new.exist? "#{@path}/#{entry}" # This is a "is file?" check and should be more obvious
255
+ File.new.copy "#{@path}/#{entry}", "#{target.path}/#{entry}"
256
+ else # is a Dir then
257
+ Dir.new.copy "#{@path}/#{entry}", "#{target.path}/#{entry}"
258
+ end
259
+ end
260
+ end
261
+
262
+ alias :copy! :copy
263
+
264
+ def [] name
265
+ if name.is_a? Integer # I do not wish to disable Array behaviour
266
+ super
267
+ else
268
+ return Dir.new "#{@path}/#{name}" if dirs.include? normalize name
269
+ return File.new "#{@path}/#{name}" if files.include? normalize name
270
+ end
271
+ end
272
+
273
+ def []= name, content
274
+ if name.is_a? Integer # I do not wish to disable Array behaviour
275
+ super
276
+ else
277
+ return File.new.write "#{@path}/#{name}", content
278
+ end
279
+ end
280
+
186
281
  private
187
282
  def do_delete path = nil
188
283
  @path = normalize path if path
data/lib/fast/file.rb CHANGED
@@ -22,11 +22,17 @@ module Fast
22
22
  else
23
23
  content = args.first
24
24
  end
25
- Fast::Dir.new.create! ::File.dirname @path if ::File.dirname(@path) != "."
26
- ::File.open @path, "a" do |handler|
27
- handler.write content
28
- end
29
- self
25
+
26
+ do_append content
27
+ end
28
+
29
+ # Appends the passed content to the file
30
+ # Creates the file if it doesn't exist.
31
+ # Creates all the necesary folders if they don't exist
32
+ # Fails if file path is not defined
33
+ def << content
34
+ raise "No path specified in the file" unless @path
35
+ do_append content
30
36
  end
31
37
 
32
38
  # Writes data into the file. If is does not exist, creates it
@@ -45,27 +51,53 @@ module Fast
45
51
  self
46
52
  end
47
53
 
48
- # Deletes the file (wrapper for `File.unlink <path>`)
49
- def delete path = nil
50
- @path = normalize path if path
51
- ::File.unlink @path
52
- @path
54
+ # Deletes the files (wrapper for `File.unlink <path>`)
55
+ # Fails if file does not exist
56
+ def delete *args
57
+ unless args.empty?
58
+ return_me = nil
59
+ args.each do |path|
60
+ return_me = normalize path
61
+ ::File.unlink return_me
62
+ end
63
+ return return_me
64
+ else
65
+ ::File.unlink @path
66
+ @path
67
+ end
53
68
  end
54
69
 
55
70
  alias :destroy :delete
56
71
  alias :unlink :delete
57
72
  alias :del :delete
58
- alias :delete! :delete
73
+
74
+ # Deletes the file(s) if it exists, does nothing otherwise
75
+ def delete! *args
76
+ unless args.empty?
77
+ return_me = nil
78
+ args.each do |path|
79
+ return_me = normalize path
80
+ ::File.unlink return_me if File.new.exist? path
81
+ end
82
+ return return_me
83
+ else
84
+ ::File.unlink @path if exist?
85
+ @path
86
+ end
87
+ end
59
88
 
60
89
  # Touches the file passed. Like bash `touch`, but creates
61
90
  # all required directories if they don't exist
62
- def touch path
63
- @path = normalize path if path
64
- Fast::Dir.new.create ::File.dirname @path if ::File.dirname(@path) != "."
65
- ::File.open @path, "a+" do |file|
66
- file.gets; file.write ""
91
+ def touch *args
92
+ if args.length > 0
93
+ return_me = nil
94
+ args.each do |path|
95
+ return_me = do_create path
96
+ end
97
+ return return_me
98
+ else
99
+ do_create @path
67
100
  end
68
- @path
69
101
  end
70
102
 
71
103
  alias :create :touch
@@ -78,14 +110,45 @@ module Fast
78
110
  end
79
111
 
80
112
  # Returns true if file exists, false otherwise
81
- def exist? path
113
+ def exist? path = nil
82
114
  @path = normalize path if path
83
- ::File.exist? @path
115
+ do_check_existence @path
84
116
  end
85
117
 
86
118
  alias :exists? :exist?
87
- alias :exist_all? :exist?
88
- alias :exist_any? :exist?
119
+
120
+ def exist_all? *args
121
+ unless args.empty?
122
+ return_me = true
123
+ args.each do |path|
124
+ return_me &= do_check_existence path
125
+ end
126
+ return return_me
127
+ else
128
+ do_check_existence @path
129
+ end
130
+ end
131
+
132
+ def exist_any? *args
133
+ unless args.empty?
134
+ return_me = false
135
+ args.each do |path|
136
+ return_me |= do_check_existence path
137
+ end
138
+ return return_me
139
+ else
140
+ do_check_existence @path
141
+ end
142
+ end
143
+
144
+ def exist_which *args
145
+ raise ArgumentError, "Wrong number of arguments (at least one is needed)" if args.empty?
146
+ return_list = []
147
+ args.each do |path|
148
+ return_list << path if do_check_existence path
149
+ end
150
+ return_list
151
+ end
89
152
 
90
153
  # Sends self to a FileFilter filter
91
154
  def filter
@@ -137,11 +200,89 @@ module Fast
137
200
  @path if @path
138
201
  end
139
202
 
203
+ # Appends the contents of the target file into self and erase the target
204
+ def merge *args
205
+ if args.length > 1
206
+ source, target = *args
207
+ @path = normalize source
208
+ target = File.new target
209
+ else
210
+ target = File.new args.first
211
+ end
212
+
213
+ raise Errno::ENOENT, "No such file - #{@path}" unless exist?
214
+ raise Errno::ENOENT, "No such file - #{target.path}" unless target.exist?
215
+
216
+ append target.read
217
+ target.delete!
218
+ self
219
+ end
220
+
221
+ # Returns true if the file is empty or does not exist
222
+ def empty? path = nil
223
+ @path = normalize path if path
224
+ return true if not exist?
225
+ read.empty?
226
+ end
227
+
228
+ # Copies current file into target file. Does not rely on OS nor FileUtils
229
+ def copy *args
230
+ if args.length > 1
231
+ current, target = *args
232
+ @path = normalize current
233
+ target = File.new target
234
+ else
235
+ target = File.new args.first
236
+ end
237
+
238
+ raise ArgumentError, "Target '#{target.path}' already exists." if target.exist?
239
+ do_copy target
240
+ end
241
+
242
+ def copy! *args
243
+ if args.length > 1
244
+ current, target = *args
245
+ @path = normalize current
246
+ target = File.new target
247
+ else
248
+ target = File.new args.first
249
+ end
250
+
251
+ do_copy target
252
+ end
253
+
140
254
  private
141
255
  def normalize path
142
256
  "#{path}"
143
257
  end
258
+
259
+ def do_copy target
260
+ target.write read
261
+ target
262
+ end
263
+
264
+ def do_create path
265
+ path = normalize path
266
+ Fast::Dir.new.create! ::File.dirname path if ::File.dirname(path) != "."
267
+ ::File.open path, "a+" do |file|
268
+ file.gets; file.write ""
269
+ end
270
+ path
271
+ end
272
+
144
273
 
274
+ def do_append content
275
+ touch @path unless exist?
276
+ ::File.open @path, "a" do |handler|
277
+ handler.write content
278
+ end
279
+ self
280
+ end
281
+
282
+ def do_check_existence path
283
+ ::File.exist?(path) && !::File.directory?(path)
284
+ end
285
+
145
286
  # Deprecated!
146
287
  def self.call *args
147
288
  if args.empty?
data/lib/fast/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fast
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -8,7 +8,7 @@ describe Fast::Dir do
8
8
  ::File.should_not be_directory "demo"
9
9
  Fast::File.new.touch "demo/myfile.txt"
10
10
  Fast::File.new.touch "demo/otherfile.txt"
11
- Fast::Dir.new.create "demo/subdir"
11
+ Fast::Dir.new.create! "demo/subdir"
12
12
 
13
13
  iteration = []
14
14
  list = Fast::Dir.new.send @method, "demo" do |entry|
@@ -112,6 +112,16 @@ describe Fast::Dir do
112
112
  }.to raise_error ArgumentError, "No arguments passed, at least one is required"
113
113
  end
114
114
 
115
+ context "from an existing Dir object" do
116
+ it "should create the dir" do
117
+ Fast::Dir.new.should_not exist :demo
118
+ the_dir = Fast::Dir.new :demo
119
+ the_dir.send @method
120
+ the_dir.should exist
121
+ the_dir.remove
122
+ end
123
+ end
124
+
115
125
  context "is a simple path" do
116
126
  it "should create the dir" do
117
127
  ::File.should_not be_directory "demo"
@@ -176,14 +186,30 @@ describe Fast::Dir do
176
186
  before :each do @method = :create end
177
187
  it_behaves_like "any dir creation"
178
188
 
179
- it "should fail if the dir already exists"
189
+ it "should fail if the dir already exists" do
190
+ Fast::Dir.new.should_not exist :demo
191
+ Fast::Dir.new.create :demo
192
+ expect { Fast::Dir.new.create :demo
193
+ }.to raise_error ArgumentError, "Dir 'demo' already exists"
194
+ Fast::Dir.new.delete! :demo
195
+ end
196
+
197
+ after do
198
+ Fast::Dir.new.delete! :demo
199
+ end
180
200
  end
181
201
 
182
202
  describe "#create!" do
183
203
  before :each do @method = :create! end
184
204
  it_behaves_like "any dir creation"
185
205
 
186
- it "should do nothing if the dir already exists"
206
+ it "should do nothing if the dir already exists" do
207
+ Fast::Dir.new.should_not exist :demo
208
+ Fast::Dir.new.create :demo
209
+ expect { Fast::Dir.new.create! :demo
210
+ }.to_not raise_error ArgumentError, "Dir 'demo' already exists"
211
+ Fast::Dir.new.delete! :demo
212
+ end
187
213
  end
188
214
 
189
215
  shared_examples_for "any dir subsetter" do
@@ -313,6 +339,27 @@ describe Fast::Dir do
313
339
  end
314
340
  end
315
341
 
342
+ describe "#remove" do
343
+ before :each do @method = :remove end
344
+ it_behaves_like "any dir deletion"
345
+
346
+ it "should fail if the directory does not exist" do
347
+ ::File.should_not be_directory "demo"
348
+ expect { Fast::Dir.new.send @method, "demo"
349
+ }.to raise_error
350
+ end
351
+ end
352
+
353
+ describe "#remove!" do
354
+ before :each do @method = :remove! end
355
+ it_behaves_like "any dir deletion"
356
+
357
+ it "should not fail even if the directory does not exist" do
358
+ Fast::Dir.new.should_not exist :demo
359
+ Fast::Dir.new.remove! :demo
360
+ end
361
+ end
362
+
316
363
  shared_examples_for "any dir existencialism" do
317
364
  it "should return true if the dir exists" do
318
365
  ::File.should_not be_directory "demo"
@@ -337,6 +384,77 @@ describe Fast::Dir do
337
384
  it_behaves_like "any dir existencialism"
338
385
  end
339
386
 
387
+ describe "#exist_all?" do
388
+ before :each do @method = :exist_all? end
389
+ it_behaves_like "any dir existencialism"
390
+
391
+ it "should return true if all exist" do
392
+ Fast::Dir.new.should_not exist :demo
393
+ Fast::Dir.new.should_not exist :demo2
394
+ Fast::Dir.new.should_not exist :demo3
395
+
396
+ Fast::Dir.new.create :demo, :demo2, :demo3
397
+
398
+ Fast::Dir.new.exist_all?(:demo, :demo2, :demo3).should be_true
399
+
400
+ Fast::Dir.new.remove :demo, :demo2, :demo3
401
+ end
402
+
403
+ it "should return false if any does not exist" do
404
+ Fast::Dir.new.should_not exist :demo
405
+ Fast::Dir.new.should_not exist :demo2
406
+ Fast::Dir.new.should_not exist :demo3
407
+
408
+ Fast::Dir.new.create :demo, :demo2
409
+
410
+ Fast::Dir.new.exist_all?(:demo, :demo2, :demo3).should be_false
411
+
412
+ Fast::Dir.new.remove :demo, :demo2
413
+ end
414
+ end
415
+
416
+ describe "#exist_any?" do
417
+ it_behaves_like "any dir existencialism"
418
+ before :each do @method = :exist_any? end
419
+
420
+ it "should return true if at least one exists" do
421
+ Fast::Dir.new.should_not exist :demo
422
+ Fast::Dir.new.should_not exist :demo2
423
+ Fast::Dir.new.should_not exist :demo3
424
+
425
+ Fast::Dir.new.create :demo, :demo2
426
+
427
+ Fast::Dir.new.exist_any?(:demo, :demo2, :demo3).should be_true
428
+
429
+ Fast::Dir.new.remove! :demo, :demo2, :demo3
430
+ end
431
+
432
+ it "should return false if none exists" do
433
+ Fast::Dir.new.should_not exist :demo
434
+ Fast::Dir.new.should_not exist :demo2
435
+ Fast::Dir.new.should_not exist :demo3
436
+
437
+ Fast::Dir.new.exist_any?(:demo, :demo2, :demo3).should be_false
438
+ end
439
+ end
440
+
441
+ describe "#exist_which" do
442
+ it "should return a list with the dir that exists" do
443
+ Fast::Dir.new.should_not exist :demo
444
+ Fast::Dir.new.should_not exist :demo2
445
+ Fast::Dir.new.should_not exist :demo3
446
+
447
+ Fast::Dir.new.create :demo, :demo2
448
+
449
+ the_list = Fast::Dir.new.exist_which :demo, :demo2, :demo3
450
+ the_list.should include :demo
451
+ the_list.should include :demo2
452
+ the_list.should_not include :demo3
453
+
454
+ Fast::Dir.new.remove! :demo, :demo2, :demo3
455
+ end
456
+ end
457
+
340
458
  describe ".new" do
341
459
  it "should accept a string path as argument" do
342
460
  Fast::Dir.new "demo"
@@ -468,27 +586,73 @@ describe Fast::Dir do
468
586
  end
469
587
 
470
588
  shared_examples_for "any dir copy" do
589
+ before :each do
590
+ Fast::Dir.new.should_not exist :demo
591
+ Fast::Dir.new.should_not exist :target
592
+ end
593
+
471
594
  context "target dir do not exist" do
472
- it "should create the target dir"
595
+ it "should create the target dir" do
596
+ Fast::Dir.new.create :demo
597
+
598
+ Fast::Dir.new.send @method, :demo, :target
599
+
600
+ Fast::Dir.new.should exist :target
601
+ end
473
602
 
474
- it "should not erase current dir"
603
+ it "should not erase current dir" do
604
+ Fast::Dir.new.create :demo
605
+
606
+ Fast::Dir.new.send @method, :demo, :target
607
+
608
+ Fast::Dir.new.should exist :demo
609
+ end
475
610
  end
476
611
 
477
- it "should be present all of source data in the target"
612
+ it "should be present all of source data in the target" do
613
+ # Create demo data
614
+ Fast::File.new.touch "demo/content.txt"
615
+ Fast::File.new.touch "demo/more/content/in/subdir.txt"
616
+ Fast::Dir.new.create "demo/empty_dir"
617
+
618
+ # Do the copying
619
+ Fast::Dir.new.send @method, :demo, :target
620
+
621
+ # Check the copy
622
+ Fast::File.new.should exist "target/content.txt"
623
+ Fast::File.new.should exist "target/more/content/in/subdir.txt"
624
+ Fast::Dir.new.should exist "target/empty_dir"
625
+ end
478
626
 
479
- it "should return current dir"
627
+ it "should return the current dir" do
628
+ the_dir = Fast::Dir.new.create! :demo
629
+ # Do the copying
630
+ the_dir.send(@method, :target).should be the_dir
631
+ end
632
+
633
+ after :each do
634
+ Fast::Dir.new.delete! :demo, :target
635
+ end
480
636
  end
481
637
 
482
638
  describe "#copy" do
483
639
  it_behaves_like "any dir copy"
640
+ before :all do @method = :copy end
484
641
 
485
- it "should fail if any conflict appears"
642
+ context "in case of conflict in any file" do
643
+ it "should fail"
644
+
645
+ it "should not do any copying"
646
+ end
486
647
  end
487
648
 
488
649
  describe "#copy!" do
489
650
  it_behaves_like "any dir copy"
651
+ before :all do @method = :copy! end
490
652
 
491
- it "should overwrite in every conflict"
653
+ context "in case of conflict" do
654
+ it "should overwrite"
655
+ end
492
656
  end
493
657
 
494
658
  describe "#merge" do
@@ -528,7 +692,7 @@ describe Fast::Dir do
528
692
  Fast::File.new.should exist "demo/data/nested/is.informative.file"
529
693
  end
530
694
 
531
- context "two fails in the source and target have the same name" do
695
+ context "two files in the source and target have the same name" do
532
696
  it "should fail"
533
697
 
534
698
  it "should not do any changes in any dir"
@@ -544,7 +708,7 @@ describe Fast::Dir do
544
708
  it "should behave like #merge but never fail"
545
709
  end
546
710
 
547
- describe "#mergeable?" do
711
+ describe "#conflicts?" do
548
712
  context "both dirs exist and no file or dir in any has the same name in the other" do
549
713
  it "should return true"
550
714
  end
@@ -552,26 +716,78 @@ describe Fast::Dir do
552
716
  context "some files in target dir have the same name as other in source" do
553
717
  it "should return false"
554
718
  end
555
-
556
- it "should fail it the target dir does not exist"
557
719
  end
558
720
 
559
721
  describe "#[]" do
722
+ before :each do
723
+ Fast::Dir.new.should_not exist :demo
724
+ end
725
+
560
726
  context "a file named like the argument exists" do
561
- it "should return it"
727
+ it "should return it" do
728
+ Fast::File.new.touch "demo/file.txt"
729
+
730
+ the_file = Fast::Dir.new(:demo)["file.txt"]
731
+ the_file.path.should == "demo/file.txt"
732
+ end
562
733
  end
563
734
 
564
735
  context "a dir named like the argument exists" do
565
- it "should return it"
736
+ it "should return it" do
737
+ Fast::Dir.new.create "demo/other_dir"
738
+ the_dir = Fast::Dir.new(:demo)[:other_dir]
739
+ the_dir.path.should == "demo/other_dir"
740
+ end
741
+ end
742
+
743
+ context "an integet is sent" do
744
+ it "should behave like an array" do
745
+ Fast::File.new.touch "demo/file.txt"
746
+
747
+ Fast::Dir.new.list(:demo)[0].should == "file.txt"
748
+ end
566
749
  end
567
750
 
568
751
  context "there's nothing there" do
569
- it "should return nil"
752
+ it "should return nil" do
753
+ Fast::Dir.new.create :demo
754
+ Fast::Dir.new.list(:demo)[:no].should be_nil
755
+ end
756
+ end
757
+
758
+ after :each do
759
+ Fast::Dir.new.delete! :demo
570
760
  end
571
761
  end
572
762
 
573
763
  describe "#[]=" do # This is an absolute WIN
574
- it "should create the file with the given content"
764
+ before :each do
765
+ Fast::Dir.new.should_not exist :demo
766
+ end
767
+
768
+ context "the content is a String" do
769
+ it "should create the file with the given content" do
770
+ the_dir = Fast::Dir.new :demo # Showoff..
771
+ the_dir[:Pianofile] = "set :port, 80" # So condensed, so solid
772
+ Fast::File.new.read("demo/Pianofile").should == "set :port, 80"
773
+ end
774
+
775
+ it "should create self" do
776
+ the_dir = Fast::Dir.new :demo # Showoff..
777
+ the_dir[:Pianofile] = "set :port, 80" # So condensed, so solid
778
+ Fast::Dir.new.should exist :demo
779
+ end
780
+ end
781
+
782
+ context "the content is a hash" do
783
+ it "should create the subdir"
784
+
785
+ it "should create recursively the tree"
786
+ end
787
+
788
+ after :each do
789
+ Fast::Dir.new.delete! :demo
790
+ end
575
791
  end
576
792
 
577
793
  end
@@ -5,35 +5,39 @@ require "zucker/os"
5
5
  ::File.unlink "demo.txt" if ::File.exist? "demo.txt"
6
6
 
7
7
  describe Fast::File do
8
- describe "#append" do
8
+
9
+ shared_examples_for "any file content appending" do
9
10
  it "should create the file if it does not exist" do
10
11
  ::File.should_not exist "demo.txt"
11
- Fast::File.new.append "demo.txt", "some text"
12
+ the_file = Fast::File.new "demo.txt"
13
+ the_file.send @method, "some text"
12
14
  ::File.should exist "demo.txt"
13
15
  ::File.unlink "demo.txt"
14
16
  end
15
-
17
+
16
18
  it "should not erase the content of the file if it has some" do
17
19
  ::File.open "demo.txt", "w" do |file|
18
20
  file.write "some demo content"
19
21
  end
20
22
 
21
- Fast::File.new.append "demo.txt", "\nmore demo content"
23
+ the_file = Fast::File.new "demo.txt"
24
+ the_file.send @method, "\nmore demo content"
22
25
 
23
26
  ::File.read( "demo.txt" ).should match /^some demo content/
24
27
  ::File.unlink "demo.txt"
25
28
  end
26
-
29
+
27
30
  it "should append the new content last in the file" do
28
31
  ::File.open "demo.txt", "w" do |file|
29
32
  file.write "This comes first: "
30
33
  end
31
- Fast::File.new.append "demo.txt", "and this comes after."
34
+ the_file = Fast::File.new "demo.txt"
35
+ the_file.send @method, "and this comes after."
32
36
 
33
37
  ::File.read( "demo.txt" ).should match /and this comes after.$/
34
38
  ::File.unlink "demo.txt"
35
39
  end
36
-
40
+
37
41
  it "should update the modification time" do
38
42
  ::File.open "demo.txt", "w" do |file|
39
43
  file.write "Written earlier"
@@ -41,28 +45,18 @@ describe Fast::File do
41
45
  mtime = ::File.mtime "demo.txt"
42
46
 
43
47
  sleep 1
44
- Fast::File.new.append "demo.txt", "\nWritten later"
48
+ the_file = Fast::File.new "demo.txt"
49
+ the_file.send @method, "\nWritten later"
45
50
  ::File.mtime( "demo.txt" ).should > mtime
46
51
 
47
52
  ::File.unlink "demo.txt"
48
53
  end
49
-
50
- it "should return the file" do
51
- the_file = ::Fast::File.new.append "demo.file", "Some content."
52
- the_file.should be_a Fast::File
53
- the_file.delete!
54
- end
55
-
56
- it "should work even when a symbol is passed as argument" do
57
- Fast::File.new.append :demo_txt, "Hola."
58
- ::File.should exist "demo_txt"
59
- ::File.unlink "demo_txt"
60
- end
61
54
 
62
55
  context "the file is inside a non existing directory" do
63
56
  it "should create the directory aswell as the file" do
64
57
  ::File.should_not be_directory "demo"
65
- Fast::File.new.append "demo/demo.txt", "Nice content!"
58
+ the_file = Fast::File.new "demo/demo.txt"
59
+ the_file.send @method, "Nice content!"
66
60
  ::File.should be_directory "demo"
67
61
  ::File.unlink "demo/demo.txt"
68
62
  ::Dir.unlink "demo"
@@ -71,7 +65,25 @@ describe Fast::File do
71
65
  end
72
66
 
73
67
  describe "#<<" do
74
- it "should behave almost like #append"
68
+ it_should_behave_like "any file content appending"
69
+ before :all do @method = :"<<" end
70
+
71
+ it "should fail if not path previously defined" do
72
+ the_file = Fast::File.new
73
+ expect { the_file << "More content"
74
+ }.to raise_error "No path specified in the file"
75
+ end
76
+ end
77
+
78
+ describe "#append" do
79
+ it_should_behave_like "any file content appending"
80
+ before :all do @method = :append end
81
+
82
+ it "should return the file" do
83
+ the_file = ::Fast::File.new.append "demo.file", "Some content."
84
+ the_file.should be_a Fast::File
85
+ the_file.delete!
86
+ end
75
87
  end
76
88
 
77
89
  describe "#write" do
@@ -134,6 +146,16 @@ describe Fast::File do
134
146
  Fast::File.new.send @method, :demo_txt
135
147
  ::File.should_not exist "demo.txt"
136
148
  end
149
+
150
+ it "should accept multiple arguments" do
151
+ Fast::File.new.should_not exist "demo1.txt"
152
+ Fast::File.new.should_not exist "demo2.txt"
153
+ Fast::File.new.should_not exist "demo3.txt"
154
+
155
+ Fast::File.new.touch "demo1.txt", "demo2.txt", "demo3.txt"
156
+
157
+ Fast::File.new.send @method, "demo1.txt", "demo2.txt", "demo3.txt"
158
+ end
137
159
  end
138
160
 
139
161
  describe "#delete" do
@@ -144,6 +166,12 @@ describe Fast::File do
144
166
  describe "#delete!" do
145
167
  before :each do @method = :delete! end
146
168
  it_behaves_like "any file deletion"
169
+
170
+ it "should not fail if the file does not exist" do
171
+ Fast::File.new.should_not exist "the_file.txt"
172
+ expect { Fast::File.new.delete! "the_file.txt"
173
+ }.to_not raise_error
174
+ end
147
175
  end
148
176
 
149
177
  describe "#unlink" do
@@ -229,7 +257,21 @@ describe Fast::File do
229
257
  ::File.unlink "demo_txt"
230
258
  end
231
259
 
232
- it "should work with multiple arguments"
260
+ it "should work with multiple arguments" do
261
+ Fast::File.new.should_not exist "demo1.txt"
262
+ Fast::File.new.should_not exist "demo2.txt"
263
+ Fast::File.new.should_not exist "demo3.txt"
264
+
265
+ Fast::File.new.send @method, "demo1.txt", "demo2.txt", "demo3.txt"
266
+
267
+ Fast::File.new.should exist "demo1.txt"
268
+ Fast::File.new.should exist "demo2.txt"
269
+ Fast::File.new.should exist "demo3.txt"
270
+
271
+ Fast::File.new.delete "demo1.txt"
272
+ Fast::File.new.delete "demo2.txt"
273
+ Fast::File.new.delete "demo3.txt"
274
+ end
233
275
  end
234
276
 
235
277
  describe "#create" do
@@ -315,7 +357,12 @@ describe Fast::File do
315
357
  Fast::File.new.send( @method, "demo.file" ).should be_false
316
358
  end
317
359
 
318
- it "should return false if path represents a directory!"
360
+ it "should return false if path represents a directory!" do
361
+ Fast::Dir.new.should_not exist "demo"
362
+ Fast::Dir.new.create "demo"
363
+ Fast::File.new.send( @method, "demo" ).should be_false
364
+ Fast::Dir.new.delete "demo"
365
+ end
319
366
  end
320
367
 
321
368
  describe "#exist?" do
@@ -332,25 +379,105 @@ describe Fast::File do
332
379
  before :each do @method = :exist_all? end
333
380
  it_behaves_like "any file existencialism"
334
381
 
335
- it "should return true if all exist"
382
+ it "should return true if all exist" do
383
+ # Create the demo files
384
+ Fast::File.new.touch "demo1.txt", "demo2.txt", "demo3.txt"
385
+
386
+ # Try it
387
+ Fast::File.new.exist_all?("demo1.txt", "demo2.txt", "demo3.txt").should be_true
388
+ end
336
389
 
337
- it "should return false if any does not exist"
390
+ it "should return false if any does not exist" do
391
+ Fast::File.new.touch "demo1.txt", "demo2.txt", "demo3.txt"
392
+ Fast::File.new.should_not exist "demo4.txt"
393
+
394
+ Fast::File.new.exist_all?("demo2.txt", "demo4.txt", "demo1.txt").should be_false
395
+ end
396
+
397
+ after :all do
398
+ # Clean after deeds
399
+ Fast::File.new.delete! "demo1.txt"
400
+ Fast::File.new.delete! "demo2.txt"
401
+ Fast::File.new.delete! "demo3.txt"
402
+ end
338
403
  end
339
404
 
340
405
  describe "#exist_any?" do
341
406
  before :each do @method = :exist_any? end
342
407
  it_behaves_like "any file existencialism"
343
408
 
344
- it "should return true if at least one exists"
409
+ it "should return true if at least one exists" do
410
+ Fast::File.new.touch "demo1.txt"
411
+ Fast::File.new.should_not exist "demo2.txt"
412
+
413
+ Fast::File.new.exist_any?("demo1.txt", "demo2.txt").should be_true
414
+ end
345
415
 
346
- it "should return false if none exist"
416
+ it "should return false if none exist" do
417
+ Fast::File.new.should_not exist "demo2.txt"
418
+ Fast::File.new.should_not exist "demo3.txt"
419
+
420
+ Fast::File.new.exist_any?("demo2.txt", "demo3.txt").should be_false
421
+ end
422
+
423
+ after :all do
424
+ Fast::File.new.delete "demo1.txt"
425
+ end
426
+ end
427
+
428
+ describe "#exist_which" do
429
+ it "should return a list with the files that exist" do
430
+ Fast::File.new.should_not exist "demo1.txt"
431
+ Fast::File.new.should_not exist "demo2.txt"
432
+ Fast::File.new.should_not exist "demo3.txt"
433
+
434
+ Fast::File.new.touch "demo1.txt", "demo2.txt"
435
+ the_files = Fast::File.new.exist_which "demo1.txt", "demo2.txt", "demo3.txt"
436
+ the_files.should include "demo1.txt"
437
+ the_files.should include "demo2.txt"
438
+ the_files.should_not include "demo3.txt"
439
+ end
440
+
441
+ after :all do
442
+ Fast::File.new.delete "demo1.txt"
443
+ Fast::File.new.delete "demo2.txt"
444
+ end
445
+ end
446
+
447
+ describe "#empty?" do
448
+ it "should return true if the file has no content" do
449
+ Fast::File.new.should_not exist "demo.txt"
450
+ Fast::File.new.touch "demo.txt"
451
+
452
+ Fast::File.new.empty?("demo.txt").should be_true
453
+ end
454
+
455
+ it "should return true if the file does not exist" do
456
+ Fast::File.new.should_not exist "demo.txt"
457
+
458
+ Fast::File.new.empty?("demo.txt").should be_true
459
+ end
460
+
461
+ it "should return false if the file has content" do
462
+ Fast::File.new.should_not exist "demo.txt"
463
+ Fast::File.new.write "demo.txt", "Some irrelevant content."
464
+
465
+ Fast::File.new.empty?("demo.txt").should be_false
466
+ end
467
+
468
+ after :each do
469
+ Fast::File.new.delete! "demo.txt"
470
+ end
347
471
  end
348
472
 
349
473
  shared_examples_for "any file subsetter" do
474
+ # This should follow more precisely the SubSetter pattern as specified
475
+ # on http://xaviervia.com.ar/patterns/sub-setter
476
+ #
350
477
  # This is a reminder: along with Serializer, the Subsetter pattern
351
478
  # (and later, the Sorting one) should be implemented Fast
352
-
353
- # I guess filtering in Fast will be done in Fast::FileFilter
479
+ #
480
+ # Fast::FileFilter will be deprecated soon
354
481
  it "should forward self to a filtering object" do
355
482
  the_demo_file = Fast::File.new :demo
356
483
  Fast::FileFilter.should_receive( :new ).with the_demo_file
@@ -474,8 +601,117 @@ describe Fast::File do
474
601
  end
475
602
 
476
603
  describe "#merge" do
477
- it "should delete the target file"
604
+ before :each do
605
+ Fast::File.new.should_not exist "demo.txt"
606
+ Fast::File.new.should_not exist "target.txt"
607
+
608
+ Fast::File.new.write "demo.txt", "Basic content"
609
+ Fast::File.new.write "target.txt", "\nSome extra content"
610
+ end
611
+
612
+ it "should delete the target file" do
613
+ Fast::File.new.merge "demo.txt", "target.txt"
614
+
615
+ Fast::File.new.should_not exist "target.txt"
616
+ Fast::File.new.should exist "demo.txt"
617
+ end
478
618
 
479
- it "should append the contents of the target into this"
619
+ it "should append the contents of the target into this" do
620
+ Fast::File.new.merge "demo.txt", "target.txt"
621
+
622
+ Fast::File.new.read("demo.txt").should include "Basic content\nSome extra content"
623
+ end
624
+
625
+ it "should return self" do
626
+ the_file = Fast::File.new "demo.txt"
627
+ the_file.merge("target.txt").should be the_file
628
+ end
629
+
630
+ context "some is missing" do
631
+ it "should fail if self is missing" do
632
+ expect { Fast::File.new.merge "hola.txt", "target.txt"
633
+ }.to raise_error Errno::ENOENT
634
+ end
635
+
636
+ it "should fail if target is missing" do
637
+ expect { Fast::File.new.merge "demo.txt", "hola.txt"
638
+ }.to raise_error Errno::ENOENT
639
+ end
640
+ end
641
+
642
+ after :each do
643
+ Fast::File.new.delete! "demo.txt"
644
+ Fast::File.new.delete! "target.txt"
645
+ end
646
+ end
647
+
648
+ shared_examples_for "any file copying" do
649
+ before :each do
650
+ Fast::File.new.should_not exist "demo.txt"
651
+ Fast::File.new.write "demo.txt", "Need for content"
652
+ end
653
+
654
+ it "should exist the target file after the copy" do
655
+ Fast::File.new.send @method, "demo.txt", "target.txt"
656
+
657
+ Fast::File.new.should exist "target.txt"
658
+ end
659
+
660
+ it "should exist the source file after the copy" do
661
+ Fast::File.new.send @method, "demo.txt", "target.txt"
662
+
663
+ Fast::File.new.should exist "demo.txt"
664
+ end
665
+
666
+ it "should contain the same content both the source and the target files" do
667
+ Fast::File.new.send @method, "demo.txt", "target.txt"
668
+
669
+ Fast::File.new.read("demo.txt").should == Fast::File.new.read("target.txt")
670
+ end
671
+
672
+ after :each do
673
+ Fast::File.new.delete! "demo.txt"
674
+ Fast::File.new.delete! "target.txt"
675
+ end
676
+ end
677
+
678
+ describe "#copy" do
679
+ it_behaves_like "any file copying"
680
+ before :all do @method = :copy end
681
+
682
+ context "the target file exists" do
683
+ it "should fail" do
684
+ Fast::File.new.should_not exist "demo.txt"
685
+ Fast::File.new.should_not exist "target.txt"
686
+ Fast::File.new.write "demo.txt", "Once upon a time."
687
+ Fast::File.new.write "target.txt", "A hobbit there was."
688
+
689
+ expect { Fast::File.new.copy "demo.txt", "target.txt"
690
+ }.to raise_error ArgumentError, "Target 'target.txt' already exists."
691
+
692
+ Fast::File.new.delete! "demo.txt"
693
+ Fast::File.new.delete! "target.txt"
694
+ end
695
+ end
696
+ end
697
+
698
+ describe "#copy!" do
699
+ it_behaves_like "any file copying"
700
+ before :all do @method = :copy! end
701
+
702
+ context "the target file exists" do
703
+ it "should overwrite the target file" do
704
+ Fast::File.new.should_not exist "demo.txt"
705
+ Fast::File.new.should_not exist "target.txt"
706
+ Fast::File.new.write "demo.txt", "Once upon a time."
707
+ Fast::File.new.write "target.txt", "A hobbit there was."
708
+
709
+ Fast::File.new.copy! "demo.txt", "target.txt"
710
+ Fast::File.new.read("demo.txt").should == Fast::File.new.read("target.txt")
711
+
712
+ Fast::File.new.delete! "demo.txt"
713
+ Fast::File.new.delete! "target.txt"
714
+ end
715
+ end
480
716
  end
481
717
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-17 00:00:00.000000000 Z
12
+ date: 2011-12-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metafun
16
- requirement: &15528200 !ruby/object:Gem::Requirement
16
+ requirement: &15252960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15528200
24
+ version_requirements: *15252960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &15527120 !ruby/object:Gem::Requirement
27
+ requirement: &15252120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *15527120
35
+ version_requirements: *15252120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: zucker
38
- requirement: &15526180 !ruby/object:Gem::Requirement
38
+ requirement: &15251420 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *15526180
46
+ version_requirements: *15251420
47
47
  description: DSL for file system interaction
48
48
  email:
49
49
  - xavierviacanel@gmail.com