fast 0.0.1 → 0.0.2

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.
@@ -15,6 +15,9 @@ Gem::Specification.new do |s|
15
15
  s.rubyforge_project = "fast"
16
16
 
17
17
  s.add_dependency "metafun"
18
+
19
+ s.add_development_dependency "rspec"
20
+ s.add_development_dependency "zucker"
18
21
 
19
22
  s.files = `git ls-files`.split("\n")
20
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -1,6 +1,7 @@
1
1
  require "metafun/delegator"
2
2
 
3
3
  require "fast/file"
4
+ require "fast/dir"
4
5
 
5
6
  module Fast
6
7
  # Returns the list of entries in the directory
@@ -13,7 +14,7 @@ module Fast
13
14
  if options
14
15
  if options[:extension]
15
16
  filtered = []
16
- Dir.entries(path).each do |entry|
17
+ ::Dir.entries(path).each do |entry|
17
18
  if entry.end_with? ".#{options[:extension]}"
18
19
  if options[:strip_extension]
19
20
  filtered << entry[0..-("#{options[:extension]}".length)-2]
@@ -25,7 +26,7 @@ module Fast
25
26
  return filtered
26
27
  end
27
28
  else
28
- Dir.entries path
29
+ ::Dir.entries path
29
30
  end
30
31
  end
31
32
 
@@ -51,10 +52,9 @@ module Fast
51
52
  def self.mkdir path
52
53
  path.split("/").each do |part|
53
54
  route ||= part
54
- Dir.mkdir route unless route == "" || ::File.directory?( route )
55
+ ::Dir.mkdir route unless route == "" || ::File.directory?( route )
55
56
  route += "/#{path}"
56
57
  end
57
- # Dir.mkdir path # It seems this was redundant
58
58
  end
59
59
  end
60
60
 
@@ -0,0 +1,16 @@
1
+ module Fast
2
+ # Directory handling class
3
+ class Dir
4
+ # .call -> like Fast::File.call
5
+
6
+ # #list
7
+
8
+ # #files
9
+
10
+ # #dirs
11
+
12
+ # #create
13
+
14
+ # #exist?
15
+ end
16
+ end
@@ -2,21 +2,39 @@ module Fast
2
2
  # File handling class.
3
3
  class File
4
4
  # Appends the passed content to the file `path`
5
+ # Creates the file if it doesn't exist.
6
+ # Creates all the necesary folders if they don't exist
5
7
  def append path, content
6
- ::File.open path, "a" do |handler|
8
+ @path = normalize path
9
+ Fast.dir! ::File.dirname @path if ::File.dirname(@path) != "."
10
+ ::File.open @path, "a" do |handler|
7
11
  handler.write content
8
12
  end
13
+ @path
9
14
  end
10
15
 
11
16
  # Deletes the file (wrapper for `File.unlink <path>`)
12
17
  def delete path
13
- ::File.unlink path
18
+ @path = normalize path
19
+ ::File.unlink @path
20
+ @path
14
21
  end
15
22
 
16
23
  alias :destroy :delete
17
24
  alias :unlink :delete
18
25
  alias :del :delete
19
26
 
27
+ # Touches the file passed. Like bash `touch`, but creates
28
+ # all required directories if they don't exist
29
+ def touch path
30
+ @path = normalize path
31
+ Fast.dir! ::File.dirname @path if ::File.dirname(@path) != "."
32
+ ::File.open @path, "a+" do |file|
33
+ file.gets; file.write ""
34
+ end
35
+ @path
36
+ end
37
+
20
38
  def self.call *args
21
39
  if args.empty?
22
40
  File.new
@@ -24,5 +42,10 @@ module Fast
24
42
  ::File.read args.shift
25
43
  end
26
44
  end
45
+
46
+ private
47
+ def normalize path
48
+ "#{path}"
49
+ end
27
50
  end
28
51
  end
@@ -1,3 +1,3 @@
1
1
  module Fast
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,35 @@
1
+ require "fast"
2
+
3
+ describe Fast::Dir do
4
+ describe "#list" do
5
+ it "should return a list of all items in the directory"
6
+ end
7
+
8
+ 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"
13
+ end
14
+ end
15
+
16
+ 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 '..'"
21
+ end
22
+ end
23
+
24
+ describe "#create" do
25
+ context "is a simple path" do
26
+ it "should create the dir"
27
+
28
+ it "should return the directory path"
29
+ end
30
+
31
+ context "it is nested within dirs" do
32
+ it "should create the directory tree"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,178 @@
1
+ require "fast"
2
+ require "zucker/os"
3
+
4
+ # Just in case, delete the demo.txt
5
+ ::File.unlink "demo.txt" if ::File.exist? "demo.txt"
6
+
7
+ describe Fast::File do
8
+ describe "#append" do
9
+ it "should create the file if it does not exist" do
10
+ ::File.should_not exist "demo.txt"
11
+ Fast::File.new.append "demo.txt", "some text"
12
+ ::File.should exist "demo.txt"
13
+ ::File.unlink "demo.txt"
14
+ end
15
+
16
+ it "should not erase the content of the file if it has some" do
17
+ ::File.open "demo.txt", "w" do |file|
18
+ file.write "some demo content"
19
+ end
20
+
21
+ Fast::File.new.append "demo.txt", "\nmore demo content"
22
+
23
+ ::File.read( "demo.txt" ).should match /^some demo content/
24
+ ::File.unlink "demo.txt"
25
+ end
26
+
27
+ it "should append the new content last in the file" do
28
+ ::File.open "demo.txt", "w" do |file|
29
+ file.write "This comes first: "
30
+ end
31
+ Fast::File.new.append "demo.txt", "and this comes after."
32
+
33
+ ::File.read( "demo.txt" ).should match /and this comes after.$/
34
+ ::File.unlink "demo.txt"
35
+ end
36
+
37
+ it "should update the modification time" do
38
+ ::File.open "demo.txt", "w" do |file|
39
+ file.write "Written earlier"
40
+ end
41
+ mtime = ::File.mtime "demo.txt"
42
+
43
+ sleep 1
44
+ Fast::File.new.append "demo.txt", "\nWritten later"
45
+ ::File.mtime( "demo.txt" ).should > mtime
46
+
47
+ ::File.unlink "demo.txt"
48
+ end
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
54
+
55
+ it "should work even when a symbol is passed as argument" do
56
+ Fast::File.new.append :demo_txt, "Hola."
57
+ ::File.should exist "demo_txt"
58
+ ::File.unlink "demo_txt"
59
+ end
60
+
61
+ context "the file is inside a non existing directory" do
62
+ it "should create the directory aswell as the file" do
63
+ ::File.should_not be_directory "demo"
64
+ Fast::File.new.append "demo/demo.txt", "Nice content!"
65
+ ::File.should be_directory "demo"
66
+ ::File.unlink "demo/demo.txt"
67
+ ::Dir.unlink "demo"
68
+ end
69
+ end
70
+ end
71
+
72
+
73
+ shared_examples_for "any deletion" do
74
+ it "should delete the file" do
75
+ ::File.open "demo.txt", "w" do |file|
76
+ file.write "Hola."
77
+ end
78
+
79
+ Fast::File.new.delete "demo.txt"
80
+ ::File.should_not exist "demo.txt"
81
+ end
82
+
83
+ it "should return the path of the deleted file" do
84
+ ::File.open "demo.txt", "w" do |file|
85
+ file.write "Hola."
86
+ end
87
+
88
+ Fast::File.new.delete( "demo.txt" ).should == "demo.txt"
89
+ end
90
+
91
+ it "should be possible to use a symbol" do
92
+ ::File.open "demo_txt", "w" do |file|
93
+ file.write "Hola."
94
+ end
95
+
96
+ Fast::File.new.delete :demo_txt
97
+ ::File.should_not exist "demo.txt"
98
+ end
99
+ end
100
+
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
105
+
106
+ describe "#touch" do
107
+ context "in current folder" do
108
+ it "should create the file if it does not exist" do
109
+ ::File.should_not exist "demo.txt"
110
+ Fast::File.new.touch "demo.txt"
111
+ ::File.should exist "demo.txt"
112
+ ::File.unlink "demo.txt"
113
+ end
114
+
115
+ end
116
+
117
+ context "the file is inside a directory" do
118
+ it "should create the file if it does not exist" do
119
+ ::Dir.mkdir "demo" unless ::File.directory? "demo"
120
+ ::File.should_not exist "demo/demo.txt"
121
+ Fast::File.new.touch "demo/demo.txt"
122
+ ::File.should exist "demo/demo.txt"
123
+ ::File.unlink "demo/demo.txt"
124
+ ::Dir.unlink "demo"
125
+ end
126
+ end
127
+
128
+ context "the file is inside a non existing directory" do
129
+ it "should create the directory aswell as the file" do
130
+ ::File.should_not be_directory "demo"
131
+ Fast::File.new.touch "demo/demo.txt"
132
+ ::File.should exist "demo/demo.txt"
133
+ ::File.unlink "demo/demo.txt"
134
+ ::Dir.unlink "demo"
135
+ end
136
+ end
137
+
138
+ it "should not rise any error if the file already exists" do
139
+ ::File.open "demo.txt", "w" do |file|
140
+ file.write "something"
141
+ end
142
+ Fast::File.new.touch "demo.txt"
143
+ ::File.unlink "demo.txt"
144
+ end
145
+
146
+ it "should change its atime if already exists" do
147
+ unless OS.windows?
148
+ ::File.open "demo.txt", "w" do |file|
149
+ file.write "Some content."
150
+ end
151
+ atime = ::File.atime "demo.txt"
152
+ sleep 1
153
+ Fast::File.new.touch "demo.txt"
154
+ ::File.atime( "demo.txt" ).should > atime
155
+ ::File.unlink "demo.txt"
156
+ else
157
+ pending "This is for POSIX only."
158
+ end
159
+ 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"
166
+ end
167
+
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"
171
+ end
172
+
173
+ it "should accept a symbol as an argument" do
174
+ Fast::File.new.touch :demo_txt
175
+ ::File.unlink "demo_txt"
176
+ end
177
+ end
178
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Xavier Via
@@ -29,6 +29,30 @@ dependencies:
29
29
  version: "0"
30
30
  type: :runtime
31
31
  version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rspec
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: zucker
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
32
56
  description: DSL for file system interaction
33
57
  email:
34
58
  - xavierviacanel@gmail.com
@@ -44,8 +68,11 @@ files:
44
68
  - Rakefile
45
69
  - fast.gemspec
46
70
  - lib/fast.rb
71
+ - lib/fast/dir.rb
47
72
  - lib/fast/file.rb
48
73
  - lib/fast/version.rb
74
+ - spec/fast/dir_spec.rb
75
+ - spec/fast/file_spec.rb
49
76
  has_rdoc: true
50
77
  homepage: ""
51
78
  licenses: []