fast 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = Fast
2
+
3
+ DSL for file handling focused in intuitivity and semantics.
4
+
5
+ Library is pure Ruby 1.8.7, no FileUtils nor other dependencies (except Metafun for DSL delegation)
6
+
7
+ == Installation
8
+
9
+ Don't install right now, as 13-09-2011 this is pre-alpha, but in intensive development.
10
+
11
+ == Philosophy
12
+
13
+ <tt>Fast</tt> embraces the more straightforward view of files as strings of data and directories as arrays of files/directories. Some arguments:
14
+
15
+ * It is more realistic in everyday usage
16
+ * It makes them more object-like (and thus, more friendly to OOP)
17
+ * Is more semantic
18
+ * Files as IOs are still accessible through the harder-to-use native Ruby API
19
+
20
+ <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
+
22
+ == Quick notes
23
+ * 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.
24
+ * An instance of Fast::Dir should be possible to be created from a Array.
25
+
26
+ == License
27
+
28
+ GPL License. Why else?
29
+
30
+ @ Xavier Via
@@ -0,0 +1,9 @@
1
+ module Fast
2
+ # Specific filter for Dir entries. Should inherit or invoke a Generic Filter
3
+ # Just a stub for the moment
4
+ class DirFilter
5
+ def initialize set
6
+ @set = set
7
+ end
8
+ end
9
+ end
data/lib/fast/dir.rb CHANGED
@@ -1,16 +1,117 @@
1
1
  module Fast
2
2
  # Directory handling class
3
- class Dir
4
- # .call -> like Fast::File.call
3
+ class Dir < Array
4
+ # .call -> like Fast::File.call -> NOUP: make delegator pattern Singleton-like
5
+ def initialize path = nil
6
+ super()
7
+ @path = normalize path if path
8
+ end
5
9
 
6
- # #list
10
+ # Returns a Fast::Dir list with all items in the directory, except ".." and "."
11
+ def list path = nil, &block
12
+ @path = normalize path if path
13
+ ::Dir.foreach @path do |entry|
14
+ unless entry == "." or entry == ".."
15
+ self << entry
16
+ block.call entry if block
17
+ end
18
+ end
19
+ self
20
+ end
7
21
 
8
- # #files
22
+ # Returns a Fast::Dir list of all files in the directory. Non recursive
23
+ # (at least yet)
24
+ def files path = nil, &block
25
+ @path = normalize path if path
26
+ ::Dir.foreach @path do |entry|
27
+ unless ::File.directory? "#{@path}/#{entry}"
28
+ self << entry
29
+ block.call entry if block
30
+ end
31
+ end
32
+ self
33
+ end
9
34
 
10
- # #dirs
35
+ # Returns a Fast::Dir list of all directories in the directory, non-recursive
36
+ # and excluding points
37
+ def dirs path = nil, &block
38
+ @path = normalize path if path
39
+ ::Dir.foreach @path do |entry|
40
+ if ::File.directory? "#{@path}/#{entry}" and entry != "." and entry != ".."
41
+ self << entry
42
+ block.call entry if block
43
+ end
44
+ end
45
+ self
46
+ end
47
+
48
+ # Creates the dir, if it doesn't exist. Otherwise remains silent
49
+ def create path = nil
50
+ @path = normalize path if path
51
+ route = []
52
+ @path.split("/").each do |part|
53
+ unless part.empty?
54
+ route << part
55
+ unless ::File.directory? route.join "/"
56
+ ::Dir.mkdir route.join "/"
57
+ end
58
+ end
59
+ end unless ::File.directory? @path
60
+ self
61
+ end
11
62
 
12
- # #create
63
+ alias :create! :create
13
64
 
14
- # #exist?
65
+ # Deletes the directory along with all its content. Powerful, simple, risky!
66
+ def delete path = nil
67
+ @path = normalize path if path
68
+ Dir.new.list @path do |entry|
69
+ if ::File.directory? "#{@path}/#{entry}"
70
+ Dir.new.delete "#{@path}/#{entry}"
71
+ else
72
+ ::File.unlink "#{@path}/#{entry}"
73
+ end
74
+ end
75
+ ::Dir.unlink @path
76
+ @path
77
+ end
78
+
79
+ alias :delete! :delete
80
+ alias :destroy :delete
81
+ alias :del :delete
82
+ alias :unlink :delete
83
+
84
+ # Checks for existence. True if the directory exists, false otherwise
85
+ def exist? path = nil
86
+ @path = normalize path if path
87
+ ::File.directory? @path
88
+ end
89
+
90
+ alias :exists? :exist?
91
+
92
+ # Returns a String brief of the dir
93
+ def to_s
94
+ return "#{@path}"
95
+ end
96
+
97
+ # Sends self to a DirFilter filter
98
+ def filter
99
+ DirFilter.new self
100
+ end
101
+
102
+ alias :by :filter
103
+
104
+ # Expands the path to absolute is it is relative
105
+ def expand path = nil
106
+ @path = normalize path if path
107
+ ::File.expand_path @path
108
+ end
109
+
110
+ alias :absolute :expand
111
+
112
+ private
113
+ def normalize path
114
+ @path = "#{path}"
115
+ end
15
116
  end
16
117
  end
@@ -0,0 +1,9 @@
1
+ module Fast
2
+ # Specific filter for File entries. Should inherit or invoke a Generic Filter
3
+ # Just a stub for the moment
4
+ class FileFilter
5
+ def initialize set
6
+ @set = set
7
+ end
8
+ end
9
+ end
data/lib/fast/file.rb CHANGED
@@ -1,11 +1,26 @@
1
1
  module Fast
2
2
  # File handling class.
3
- class File
3
+ class File < String
4
+
5
+ # Initializes the file
6
+ def initialize source = nil
7
+ unless source.nil?
8
+ super( "#{source}" )
9
+ else
10
+ super()
11
+ end
12
+ end
13
+
4
14
  # Appends the passed content to the file `path`
5
15
  # Creates the file if it doesn't exist.
6
16
  # Creates all the necesary folders if they don't exist
7
- def append path, content
8
- @path = normalize path
17
+ def append *args
18
+ if args.length > 1
19
+ path, content = *args
20
+ @path = normalize path
21
+ else
22
+ content = args.first
23
+ end
9
24
  Fast.dir! ::File.dirname @path if ::File.dirname(@path) != "."
10
25
  ::File.open @path, "a" do |handler|
11
26
  handler.write content
@@ -14,8 +29,8 @@ module Fast
14
29
  end
15
30
 
16
31
  # Deletes the file (wrapper for `File.unlink <path>`)
17
- def delete path
18
- @path = normalize path
32
+ def delete path = nil
33
+ @path = normalize path if path
19
34
  ::File.unlink @path
20
35
  @path
21
36
  end
@@ -23,18 +38,57 @@ module Fast
23
38
  alias :destroy :delete
24
39
  alias :unlink :delete
25
40
  alias :del :delete
41
+ alias :delete! :delete
26
42
 
27
43
  # Touches the file passed. Like bash `touch`, but creates
28
44
  # all required directories if they don't exist
29
45
  def touch path
30
- @path = normalize path
31
- Fast.dir! ::File.dirname @path if ::File.dirname(@path) != "."
46
+ @path = normalize path if path
47
+ Fast::Dir.new.create ::File.dirname @path if ::File.dirname(@path) != "."
32
48
  ::File.open @path, "a+" do |file|
33
49
  file.gets; file.write ""
34
50
  end
35
51
  @path
36
52
  end
37
53
 
54
+ alias :create :touch
55
+ alias :create! :touch
56
+
57
+ # Returns the contents of the file, all at once
58
+ def read path
59
+ @path = normalize path if path
60
+ ::File.read @path
61
+ end
62
+
63
+ # Returns true if file exists, false otherwise
64
+ def exist? path
65
+ @path = normalize path if path
66
+ ::File.exist? @path
67
+ end
68
+
69
+ alias :exists? :exist?
70
+
71
+ # Sends self to a FileFilter filter
72
+ def filter
73
+ FileFilter.new self
74
+ end
75
+
76
+ alias :by :filter
77
+
78
+ # Expands the path if it's a relative path
79
+ def expand path = nil
80
+ @path = normalize path if path
81
+ ::File.expand_path @path
82
+ end
83
+
84
+ alias :absolute :expand
85
+
86
+ private
87
+ def normalize path
88
+ "#{path}"
89
+ end
90
+
91
+ # Deprecated!
38
92
  def self.call *args
39
93
  if args.empty?
40
94
  File.new
@@ -42,10 +96,5 @@ module Fast
42
96
  ::File.read args.shift
43
97
  end
44
98
  end
45
-
46
- private
47
- def normalize path
48
- "#{path}"
49
- end
50
99
  end
51
100
  end
data/lib/fast/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fast
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/fast.rb CHANGED
@@ -2,6 +2,8 @@ require "metafun/delegator"
2
2
 
3
3
  require "fast/file"
4
4
  require "fast/dir"
5
+ require "fast/dir-filter"
6
+ require "fast/file-filter"
5
7
 
6
8
  module Fast
7
9
  # Returns the list of entries in the directory
@@ -11,10 +13,11 @@ module Fast
11
13
  # `:strip_extension => true` # If an `:extension` is given, returns the
12
14
  # # entries without it
13
15
  def self.dir path, options = nil
16
+ current = Dir.new path
14
17
  if options
15
18
  if options[:extension]
16
19
  filtered = []
17
- ::Dir.entries(path).each do |entry|
20
+ current.list path do |entry|
18
21
  if entry.end_with? ".#{options[:extension]}"
19
22
  if options[:strip_extension]
20
23
  filtered << entry[0..-("#{options[:extension]}".length)-2]
@@ -26,20 +29,25 @@ module Fast
26
29
  return filtered
27
30
  end
28
31
  else
29
- ::Dir.entries path
32
+ current.list path
30
33
  end
31
34
  end
32
35
 
33
36
  # Like `dir`, but creates the directory if it does not exist
34
37
  def self.dir! path, options = nil
35
- mkdir path unless dir? path
38
+ Dir.new.create! path unless dir? path
36
39
  dir path, options
37
40
  end
38
41
 
39
42
  # Returns true if a directory named `path` exists, false otherwise.
40
43
  # (wrapper for `File.directory? <path>`)
41
44
  def self.dir? path
42
- ::File.directory? path
45
+ Dir.new.exist? path
46
+ end
47
+
48
+ # Example. This all will be deprecated when features are done
49
+ def self.file? path
50
+ File.new.exist? path
43
51
  end
44
52
 
45
53
  # Returns an instance of Fast::File and passed all methods
@@ -47,17 +55,8 @@ module Fast
47
55
  def self.file *args
48
56
  Fast::File.call *args
49
57
  end
50
-
51
- private
52
- def self.mkdir path
53
- path.split("/").each do |part|
54
- route ||= part
55
- ::Dir.mkdir route unless route == "" || ::File.directory?( route )
56
- route += "/#{path}"
57
- end
58
- end
59
58
  end
60
59
 
61
60
  include Metafun::Delegator
62
61
 
63
- delegate Fast, :dir, :dir?, :dir!, :file, :from
62
+ delegate Fast, :dir, :dir?, :dir!, :file, :file?
@@ -1,35 +1,348 @@
1
1
  require "fast"
2
+ require "zucker/os"
2
3
 
3
4
  describe Fast::Dir do
5
+ shared_examples_for "any dir list" do
6
+ context "a block is passed as an argument" do
7
+ it "should pass each entry as argument to the block" do
8
+ ::File.should_not be_directory "demo"
9
+ Fast::File.new.touch "demo/myfile.txt"
10
+ Fast::File.new.touch "demo/otherfile.txt"
11
+ Fast::Dir.new.create "demo/subdir"
12
+
13
+ iteration = []
14
+ list = Fast::Dir.new.send @method, "demo" do |entry|
15
+ iteration << entry
16
+ end
17
+
18
+ iteration.should == list
19
+
20
+ Fast::Dir.new.delete! "demo"
21
+ end
22
+ end
23
+ end
24
+
4
25
  describe "#list" do
5
- it "should return a list of all items in the directory"
26
+ before :each do @method = :list end
27
+ it_behaves_like "any dir list"
28
+
29
+ it "should return a list of all items in the directory, excluding '.' & '..'" do
30
+ ::File.should_not be_directory "demo"
31
+ ::Dir.mkdir "demo"
32
+ Fast::File.new.touch "demo/alice.txt"
33
+ Fast::File.new.touch "demo/betty.txt"
34
+ Fast::File.new.touch "demo/chris.txt"
35
+ ::Dir.mkdir "demo/subdir"
36
+
37
+ Fast::Dir.new.list( "demo" ).should include "alice.txt"
38
+ Fast::Dir.new.list( "demo" ).should include "betty.txt"
39
+ Fast::Dir.new.list( "demo" ).should include "chris.txt"
40
+ Fast::Dir.new.list( "demo" ).should include "subdir"
41
+ Fast::Dir.new.list( "demo" ).should_not include ".."
42
+ Fast::Dir.new.list( "demo" ).should_not include "."
43
+
44
+ ::Dir.unlink "demo/subdir"
45
+ ::File.unlink "demo/chris.txt"
46
+ ::File.unlink "demo/betty.txt"
47
+ ::File.unlink "demo/alice.txt"
48
+ ::Dir.unlink "demo"
49
+ end
6
50
  end
7
51
 
8
52
  describe "#files" do
9
- it "should return a list of all files in the directory"
10
-
11
- context "args :extension => 'txt'" do
12
- it "should return a list of all files with .txt as extension in the directory"
53
+ before :each do @method = :files end
54
+ it_behaves_like "any dir list"
55
+
56
+ it "should return a list of all files in the directory" do
57
+ ::File.should_not be_directory "demo"
58
+ ::Dir.mkdir "demo"
59
+ Fast::File.new.touch "demo/alice.txt"
60
+ Fast::File.new.touch "demo/betty.txt"
61
+ Fast::File.new.touch "demo/chris.txt"
62
+ ::Dir.mkdir "demo/subdir"
63
+
64
+ Fast::Dir.new.files( "demo" ).should include "alice.txt"
65
+ Fast::Dir.new.files( "demo" ).should include "betty.txt"
66
+ Fast::Dir.new.files( "demo" ).should include "chris.txt"
67
+ Fast::Dir.new.files( "demo" ).should_not include "subdir"
68
+ Fast::Dir.new.files( "demo" ).should_not include ".."
69
+ Fast::Dir.new.files( "demo" ).should_not include "."
70
+
71
+ ::Dir.unlink "demo/subdir"
72
+ ::File.unlink "demo/chris.txt"
73
+ ::File.unlink "demo/betty.txt"
74
+ ::File.unlink "demo/alice.txt"
75
+ ::Dir.unlink "demo"
13
76
  end
14
77
  end
15
78
 
16
79
  describe "#dirs" do
17
- it "should return a list containing all dirs in the directory"
18
-
19
- context "args :skip => :dots" do
20
- it "should return a list of dirs excluding '.' and '..'"
80
+ before :each do @method = :dirs end
81
+ it_behaves_like "any dir list"
82
+
83
+ it "should return a list containing all dirs in the directory, excludind dots" do
84
+ ::File.should_not be_directory "demo"
85
+ ::Dir.mkdir "demo"
86
+ Fast::File.new.touch "demo/alice.txt"
87
+ Fast::File.new.touch "demo/betty.txt"
88
+ Fast::File.new.touch "demo/chris.txt"
89
+ ::Dir.mkdir "demo/subdir"
90
+ ::Dir.mkdir "demo/endodir"
91
+
92
+ Fast::Dir.new.dirs( "demo" ).should_not include "alice.txt"
93
+ Fast::Dir.new.dirs( "demo" ).should_not include "betty.txt"
94
+ Fast::Dir.new.dirs( "demo" ).should_not include "chris.txt"
95
+ Fast::Dir.new.dirs( "demo" ).should include "subdir"
96
+ Fast::Dir.new.dirs( "demo" ).should include "endodir"
97
+ Fast::Dir.new.dirs( "demo" ).should_not include ".."
98
+ Fast::Dir.new.dirs( "demo" ).should_not include "."
99
+
100
+ ::Dir.unlink "demo/subdir"
101
+ ::Dir.unlink "demo/endodir"
102
+ ::File.unlink "demo/chris.txt"
103
+ ::File.unlink "demo/betty.txt"
104
+ ::File.unlink "demo/alice.txt"
105
+ ::Dir.unlink "demo"
21
106
  end
22
107
  end
23
108
 
24
- describe "#create" do
109
+ shared_examples_for "any dir creation" do
25
110
  context "is a simple path" do
26
- it "should create the dir"
111
+ it "should create the dir" do
112
+ ::File.should_not be_directory "demo"
113
+
114
+ Fast::Dir.new.send @method, "demo"
115
+ ::File.should be_directory "demo"
116
+ ::Dir.unlink "demo"
117
+ end
27
118
 
28
- it "should return the directory path"
119
+ it "should return the directory path" do
120
+ ::File.should_not be_directory "demo"
121
+ "#{Fast::Dir.new.create('demo')}".should include "demo"
122
+ ::Dir.unlink "demo"
123
+ end
29
124
  end
30
125
 
31
126
  context "it is nested within dirs" do
32
- it "should create the directory tree"
127
+ it "should create the directory tree" do
128
+ ::File.should_not be_directory "demo"
129
+
130
+ Fast::Dir.new.send @method, "demo/subdir"
131
+ ::File.should be_directory "demo"
132
+ ::File.should be_directory "demo/subdir"
133
+
134
+ ::Dir.unlink "demo/subdir"
135
+ ::Dir.unlink "demo"
136
+ end
137
+ end
138
+
139
+ context "it is nested within several dirs" do
140
+ it "should create the directory tree" do
141
+ ::File.should_not be_directory "demo"
142
+
143
+ Fast::Dir.new.send @method, "demo/in/several/subdirs"
144
+ ::File.should be_directory "demo/in/several/subdirs"
145
+
146
+ ::Dir.unlink "demo/in/several/subdirs"
147
+ ::Dir.unlink "demo/in/several"
148
+ ::Dir.unlink "demo/in"
149
+ ::Dir.unlink "demo"
150
+ end
33
151
  end
34
152
  end
153
+
154
+ describe "#create" do
155
+ before :each do @method = :create end
156
+ it_behaves_like "any dir creation"
157
+ end
158
+
159
+ describe "#create!" do
160
+ before :each do @method = :create! end
161
+ it_behaves_like "any dir creation"
162
+ end
163
+
164
+ shared_examples_for "any dir subsetter" do
165
+ # This is a reminder: along with Serializer, the Subsette pattern
166
+ # (and later, the Sorting one) should be implemented Fast
167
+
168
+ # I guess filtering in Fast will be done in Fast::DirFilter
169
+ it "should forward self to a filtering object" do
170
+ Fast::Dir.new.should_not exist "demo"
171
+ Fast::File.new.touch "demo/in/subdir.file"
172
+
173
+ the_demo_dir = Fast::Dir.new :demo
174
+
175
+ Fast::DirFilter.should_receive( :new ).with the_demo_dir
176
+
177
+ the_demo_dir.by
178
+
179
+ the_demo_dir.delete!
180
+ end
181
+ end
182
+
183
+ describe "#by" do
184
+ before :each do @method = :by end
185
+ it_behaves_like "any dir subsetter"
186
+ end
187
+
188
+ describe "#filter" do
189
+ before :each do @method = :filter end
190
+ it_behaves_like "any dir subsetter"
191
+ end
192
+
193
+
194
+ shared_examples_for "any dir deletion" do
195
+ it "should fail if the directory does not exist" do
196
+ ::File.should_not be_directory "demo"
197
+ expect { Fast::Dir.new.send @method, "demo"
198
+ }.to raise_error
199
+ end
200
+
201
+ it "should delete the directory if it exists" do
202
+ ::File.should_not be_directory "demo"
203
+ Fast::Dir.new.create "demo"
204
+ Fast::Dir.new.send @method, "demo"
205
+ ::File.should_not be_directory "demo"
206
+ end
207
+
208
+ context "the dir has content" do
209
+ it "should delete the content and the dir" do
210
+ ::File.should_not be_directory "demo"
211
+
212
+ Fast::Dir.new.create "demo"
213
+ Fast::Dir.new.create "demo/subdir"
214
+ Fast::File.new.touch "demo/in/subdir.txt"
215
+
216
+ Fast::Dir.new.send @method, "demo"
217
+
218
+ ::File.should_not exist "demo/in/subdir.txt"
219
+ ::File.should_not be_directory "demo/in"
220
+ ::File.should_not be_directory "demo/subdir"
221
+ ::File.should_not be_directory "demo"
222
+ end
223
+ end
224
+
225
+ it "should return the deleted dir path" do
226
+ ::File.should_not be_directory "demo"
227
+ Fast::Dir.new.create "demo"
228
+ Fast::Dir.new.send( @method, "demo" ).should == "demo"
229
+ end
230
+ end
231
+
232
+ describe "#delete" do
233
+ before :each do @method = :delete end
234
+ it_behaves_like "any dir deletion"
235
+ end
236
+
237
+ describe "#delete!" do
238
+ before :each do @method = :delete! end
239
+ it_behaves_like "any dir deletion"
240
+ end
241
+
242
+ describe "#del" do
243
+ before :each do @method = :del end
244
+ it_behaves_like "any dir deletion"
245
+ end
246
+
247
+ describe "#destroy" do
248
+ before :each do @method = :destroy end
249
+ it_behaves_like "any dir deletion"
250
+ end
251
+
252
+ describe "#unlink" do
253
+ before :each do @method = :unlink end
254
+ it_behaves_like "any dir deletion"
255
+ end
256
+
257
+ shared_examples_for "any dir existencialism" do
258
+ it "should return true if the dir exists" do
259
+ ::File.should_not be_directory "demo"
260
+ Fast::Dir.new.create! "demo"
261
+ Fast::Dir.new.send( @method, "demo" ).should be_true
262
+ Fast::Dir.new.delete! "demo"
263
+ end
264
+
265
+ it "should return false if the dir does not exist" do
266
+ ::File.should_not be_directory "demo"
267
+ Fast::Dir.new.send( @method, "demo" ).should be_false
268
+ end
269
+ end
270
+
271
+ describe "#exist?" do
272
+ before :each do @method = :exist? end
273
+ it_behaves_like "any dir existencialism"
274
+ end
275
+
276
+ describe "#exists?" do
277
+ before :each do @method = :exists? end
278
+ it_behaves_like "any dir existencialism"
279
+ end
280
+
281
+ describe ".new" do
282
+ it "should accept a string path as argument" do
283
+ Fast::Dir.new "demo"
284
+ end
285
+
286
+ it "should accept a symbol path as argument" do
287
+ Fast::Dir.new :demo
288
+ end
289
+ end
290
+
291
+ describe "#to_s" do
292
+ it "should include the path to the dir" do
293
+ Fast::Dir.new.should_not exist "demo"
294
+ Fast::Dir.new(:demo).to_s.should include "demo"
295
+ end
296
+ end
297
+
298
+ shared_examples_for "any dir absolutizer" do
299
+ context "dir path is a relative route" do
300
+ it "should expand the dir path with the pwd" do
301
+ Fast::Dir.new.send( @method, :demo ).should == "#{Dir.pwd}/demo"
302
+ end
303
+ end
304
+
305
+ context "dir path is an absolute route" do
306
+ it "should return the same path as given" do
307
+ unless OS.windows?
308
+ Fast::Dir.new.send( @method, "/dev/null").should == "/dev/null"
309
+ else
310
+ pending "POSIX only!"
311
+ end
312
+ end
313
+ end
314
+ end
315
+
316
+ describe "#expand" do
317
+ before :each do @method = :expand end
318
+ it_behaves_like "any dir absolutizer"
319
+ end
320
+
321
+ describe "#absolute" do
322
+ before :each do @method = :absolute end
323
+ it_behaves_like "any dir absolutizer"
324
+ end
325
+
326
+ describe "#rename" do
327
+ it "should change the dir's name"
328
+ end
329
+
330
+ describe "#[]" do
331
+ context "a file named like the argument exists" do
332
+ it "should return it"
333
+ end
334
+
335
+ context "a dir named like the argument exists" do
336
+ it "should return it"
337
+ end
338
+
339
+ context "there's nothing there" do
340
+ it "should return nil"
341
+ end
342
+ end
343
+
344
+ describe "#[]=" do # This is an absolute WIN
345
+ it "should create the file with the given content"
346
+ end
347
+
35
348
  end
@@ -47,10 +47,7 @@ describe Fast::File do
47
47
  ::File.unlink "demo.txt"
48
48
  end
49
49
 
50
- it "should return the path to the file" do
51
- Fast::File.new.append( "demo.txt", "Hola." ).should == "demo.txt"
52
- ::File.unlink "demo.txt"
53
- end
50
+ it "should return the file"
54
51
 
55
52
  it "should work even when a symbol is passed as argument" do
56
53
  Fast::File.new.append :demo_txt, "Hola."
@@ -69,14 +66,23 @@ describe Fast::File do
69
66
  end
70
67
  end
71
68
 
69
+ describe "#write" do
70
+ it "should create the file if it does not exist"
71
+
72
+ it "should write the given content into the file"
73
+
74
+ it "should overwrite the file if it exists"
75
+
76
+ it "should return the file"
77
+ end
72
78
 
73
- shared_examples_for "any deletion" do
79
+ shared_examples_for "any file deletion" do
74
80
  it "should delete the file" do
75
81
  ::File.open "demo.txt", "w" do |file|
76
82
  file.write "Hola."
77
83
  end
78
84
 
79
- Fast::File.new.delete "demo.txt"
85
+ Fast::File.new.send @method, "demo.txt"
80
86
  ::File.should_not exist "demo.txt"
81
87
  end
82
88
 
@@ -85,7 +91,7 @@ describe Fast::File do
85
91
  file.write "Hola."
86
92
  end
87
93
 
88
- Fast::File.new.delete( "demo.txt" ).should == "demo.txt"
94
+ Fast::File.new.send( @method, "demo.txt" ).should == "demo.txt"
89
95
  end
90
96
 
91
97
  it "should be possible to use a symbol" do
@@ -93,42 +99,73 @@ describe Fast::File do
93
99
  file.write "Hola."
94
100
  end
95
101
 
96
- Fast::File.new.delete :demo_txt
102
+ Fast::File.new.send @method, :demo_txt
97
103
  ::File.should_not exist "demo.txt"
98
104
  end
99
105
  end
100
106
 
101
- describe "#delete" do it_behaves_like "any deletion" end
102
- describe "#unlink" do it_behaves_like "any deletion" end
103
- describe "#del" do it_behaves_like "any deletion" end
104
- describe "#destroy" do it_behaves_like "any deletion" end
107
+ describe "#delete" do
108
+ before :each do @method = :delete end
109
+ it_behaves_like "any file deletion"
110
+ end
111
+
112
+ describe "#delete!" do
113
+ before :each do @method = :delete! end
114
+ it_behaves_like "any file deletion"
115
+ end
105
116
 
106
- describe "#touch" do
117
+ describe "#unlink" do
118
+ before :each do @method = :unlink end
119
+ it_behaves_like "any file deletion"
120
+ end
121
+
122
+ describe "#del" do
123
+ before :each do @method = :del end
124
+ it_behaves_like "any file deletion"
125
+ end
126
+
127
+ describe "#destroy" do
128
+ before :each do @method = :destroy end
129
+ it_behaves_like "any file deletion"
130
+ end
131
+
132
+ shared_examples_for "any file creation" do
107
133
  context "in current folder" do
108
134
  it "should create the file if it does not exist" do
109
135
  ::File.should_not exist "demo.txt"
110
- Fast::File.new.touch "demo.txt"
136
+ Fast::File.new.send @method, "demo.txt"
111
137
  ::File.should exist "demo.txt"
112
138
  ::File.unlink "demo.txt"
113
- end
114
-
139
+ end
115
140
  end
116
-
117
141
  context "the file is inside a directory" do
118
142
  it "should create the file if it does not exist" do
119
143
  ::Dir.mkdir "demo" unless ::File.directory? "demo"
120
144
  ::File.should_not exist "demo/demo.txt"
121
- Fast::File.new.touch "demo/demo.txt"
145
+ Fast::File.new.send @method, "demo/demo.txt"
122
146
  ::File.should exist "demo/demo.txt"
123
147
  ::File.unlink "demo/demo.txt"
124
148
  ::Dir.unlink "demo"
125
149
  end
126
150
  end
127
151
 
152
+ context "the file is inside several non existing directories" do
153
+ it "should create all the required directory tree" do
154
+ ::File.should_not be_directory "demo"
155
+ Fast::File.new.send @method, "demo/in/several/subdirs/file.txt"
156
+ ::File.should exist "demo/in/several/subdirs/file.txt"
157
+ ::File.unlink "demo/in/several/subdirs/file.txt"
158
+ ::Dir.unlink "demo/in/several/subdirs"
159
+ ::Dir.unlink "demo/in/several"
160
+ ::Dir.unlink "demo/in"
161
+ ::Dir.unlink "demo"
162
+ end
163
+ end
164
+
128
165
  context "the file is inside a non existing directory" do
129
166
  it "should create the directory aswell as the file" do
130
167
  ::File.should_not be_directory "demo"
131
- Fast::File.new.touch "demo/demo.txt"
168
+ Fast::File.new.send @method, "demo/demo.txt"
132
169
  ::File.should exist "demo/demo.txt"
133
170
  ::File.unlink "demo/demo.txt"
134
171
  ::Dir.unlink "demo"
@@ -139,10 +176,42 @@ describe Fast::File do
139
176
  ::File.open "demo.txt", "w" do |file|
140
177
  file.write "something"
141
178
  end
142
- Fast::File.new.touch "demo.txt"
179
+ Fast::File.new.send @method, "demo.txt"
180
+ ::File.unlink "demo.txt"
181
+ end
182
+
183
+ it "should be empty if it did not exist" do
184
+ ::File.should_not exist "demo.txt"
185
+ Fast::File.new.send @method, "demo.txt"
186
+ ::File.read( "demo.txt" ).should == ""
143
187
  ::File.unlink "demo.txt"
144
188
  end
145
189
 
190
+ it "should return the path to the file" do
191
+ Fast::File.new.send( @method, "demo.txt" ).should == "demo.txt"
192
+ ::File.unlink "demo.txt"
193
+ end
194
+
195
+ it "should accept a symbol as an argument" do
196
+ Fast::File.new.send @method, :demo_txt
197
+ ::File.unlink "demo_txt"
198
+ end
199
+ end
200
+
201
+ describe "#create" do
202
+ before :each do @method = :create end
203
+ it_behaves_like "any file creation"
204
+ end
205
+
206
+ describe "#create!" do
207
+ before :each do @method = :create! end
208
+ it_behaves_like "any file creation"
209
+ end
210
+
211
+ describe "#touch" do
212
+ before :each do @method = :touch end
213
+ it_behaves_like "any file creation"
214
+
146
215
  it "should change its atime if already exists" do
147
216
  unless OS.windows?
148
217
  ::File.open "demo.txt", "w" do |file|
@@ -157,22 +226,133 @@ describe Fast::File do
157
226
  pending "This is for POSIX only."
158
227
  end
159
228
  end
160
-
161
- it "should be empty if it did not exist" do
162
- ::File.should_not exist "demo.txt"
163
- Fast::File.new.touch "demo.txt"
164
- ::File.read( "demo.txt" ).should == ""
165
- ::File.unlink "demo.txt"
229
+ end
230
+
231
+ describe "#read" do
232
+ context "the file exists" do
233
+ it "should return all the contents of the file" do
234
+ ::File.should_not exist "demo.txt"
235
+ Fast::File.new.append "demo.txt", "New content!"
236
+ Fast::File.new.read( "demo.txt" ).should == "New content!"
237
+ ::File.unlink "demo.txt"
238
+ end
239
+
240
+ it "should change the atime of the file" do
241
+ ::File.should_not exist "demo.txt"
242
+ Fast::File.new.append "demo.txt", "New content!"
243
+ atime = ::File.atime "demo.txt"
244
+ sleep 1
245
+ Fast::File.new.read "demo.txt"
246
+ ::File.atime("demo.txt").should > atime
247
+ ::File.unlink "demo.txt"
248
+ end
249
+
250
+ it "should accept a symbol as an argument" do
251
+ ::File.should_not exist "demo_txt"
252
+ Fast::File.new.append "demo_txt", "New content!"
253
+ Fast::File.new.read :demo_txt
254
+ ::File.unlink "demo_txt"
255
+ end
256
+
257
+ context "a block is passed" do
258
+ it "should be passed a read-only file as an argument, as in File.open"
259
+ end
166
260
  end
167
261
 
168
- it "should return the path to the file" do
169
- Fast::File.new.touch( "demo.txt" ).should == "demo.txt"
170
- ::File.unlink "demo.txt"
262
+ context "the file doesn't exist" do
263
+ it "should rise an exception" do
264
+ ::File.should_not exist "demo.txt"
265
+ expect { Fast::File.new.read "demo.txt"
266
+ }.to raise_error
267
+ end
268
+ end
269
+ end
270
+
271
+ shared_examples_for "any file existencialism" do
272
+ it "should return true if file exists" do
273
+ ::File.should_not exist "demo.file"
274
+ Fast::File.new.create! "demo.file"
275
+ Fast::File.new.send( @method, "demo.file" ).should be_true
276
+ Fast::File.new.delete "demo.file"
171
277
  end
172
278
 
173
- it "should accept a symbol as an argument" do
174
- Fast::File.new.touch :demo_txt
175
- ::File.unlink "demo_txt"
279
+ it "should return false if file does not exist" do
280
+ ::File.should_not exist "demo.file"
281
+ Fast::File.new.send( @method, "demo.file" ).should be_false
282
+ end
283
+ end
284
+
285
+ describe "#exist?" do
286
+ before :each do @method = :exist? end
287
+ it_behaves_like "any file existencialism"
288
+ end
289
+
290
+ describe "#exists?" do
291
+ before :each do @method = :exists? end
292
+ it_behaves_like "any file existencialism"
293
+ end
294
+
295
+ shared_examples_for "any file subsetter" do
296
+ # This is a reminder: along with Serializer, the Subsetter pattern
297
+ # (and later, the Sorting one) should be implemented Fast
298
+
299
+ # I guess filtering in Fast will be done in Fast::FileFilter
300
+ it "should forward self to a filtering object" do
301
+ the_demo_file = Fast::File.new :demo
302
+ Fast::FileFilter.should_receive( :new ).with the_demo_file
303
+ the_demo_file.by
304
+ end
305
+ end
306
+
307
+ describe "#by" do
308
+ before :each do @method = :by end
309
+ it_behaves_like "any file subsetter"
310
+ end
311
+
312
+ describe "#filter" do
313
+ before :each do @method = :filter end
314
+ it_behaves_like "any file subsetter"
315
+ end
316
+
317
+ describe ".new" do
318
+ it "should accept a string path as argument" do
319
+ Fast::File.new "demo"
320
+ end
321
+
322
+ it "should accept a symbol path as argument" do
323
+ Fast::File.new :demo
176
324
  end
177
325
  end
326
+
327
+ shared_examples_for "any file absolutizer" do
328
+ context "file path is a relative route" do
329
+ it "should expand the file path with pwd" do
330
+ Fast::File.new.send( @method, "demo.file" ).should == "#{Dir.pwd}/demo.file"
331
+ end
332
+ end
333
+
334
+ context "file path is an absolute route" do
335
+ it "should return the same as given path" do
336
+ unless OS.windows?
337
+ Fast::File.new.send( @method, "/dev/null" ).should == "/dev/null"
338
+ else
339
+ pending "POSIX only!"
340
+ end
341
+ end
342
+ end
343
+ end
344
+
345
+ describe "#expand" do
346
+ before :each do @method = :expand end
347
+ it_behaves_like "any file absolutizer"
348
+ end
349
+
350
+ describe "#absolute" do
351
+ before :each do @method = :absolute end
352
+ it_behaves_like "any file absolutizer"
353
+ end
354
+
355
+ describe "#rename" do
356
+ it "should change the file's name"
357
+ end
178
358
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Xavier Via
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-11-11 00:00:00 +01:00
17
+ date: 2011-11-13 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -65,10 +65,13 @@ extra_rdoc_files: []
65
65
  files:
66
66
  - .gitignore
67
67
  - Gemfile
68
+ - README.rdoc
68
69
  - Rakefile
69
70
  - fast.gemspec
70
71
  - lib/fast.rb
72
+ - lib/fast/dir-filter.rb
71
73
  - lib/fast/dir.rb
74
+ - lib/fast/file-filter.rb
72
75
  - lib/fast/file.rb
73
76
  - lib/fast/version.rb
74
77
  - spec/fast/dir_spec.rb