fast 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fast/dir-filter.rb +17 -1
- data/lib/fast/dir.rb +1 -0
- data/lib/fast/fast.rb +34 -0
- data/lib/fast/file.rb +2 -2
- data/lib/fast/version.rb +1 -1
- data/lib/fast.rb +1 -56
- data/spec/fast/dir-filter_spec.rb +26 -3
- data/spec/fast/dir_spec.rb +9 -0
- data/spec/fast/fast_spec.rb +109 -0
- metadata +6 -4
data/lib/fast/dir-filter.rb
CHANGED
@@ -7,11 +7,27 @@ module Fast
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def extension the_extension
|
10
|
-
return_me =
|
10
|
+
return_me = Dir.new
|
11
11
|
@set.each do |entry|
|
12
12
|
return_me << entry if entry.end_with? the_extension
|
13
13
|
end
|
14
14
|
return_me
|
15
15
|
end
|
16
|
+
|
17
|
+
def strip_extension
|
18
|
+
return_me = Dir.new
|
19
|
+
@set.each do |entry|
|
20
|
+
return_me.push entry.gsub /\.(\w+?)$/, ""
|
21
|
+
end
|
22
|
+
return_me
|
23
|
+
end
|
24
|
+
|
25
|
+
def match regexp
|
26
|
+
return_me = Dir.new
|
27
|
+
@set.each do |entry|
|
28
|
+
return_me << entry if entry.match regexp
|
29
|
+
end
|
30
|
+
return_me
|
31
|
+
end
|
16
32
|
end
|
17
33
|
end
|
data/lib/fast/dir.rb
CHANGED
@@ -48,6 +48,7 @@ module Fast
|
|
48
48
|
# Creates the dir, if it doesn't exist. Otherwise remains silent
|
49
49
|
# Returns the last dir path passed as argument
|
50
50
|
def create *args
|
51
|
+
raise ArgumentError, "No arguments passed, at least one is required" if args.empty?
|
51
52
|
if args.length > 0
|
52
53
|
return_me = nil
|
53
54
|
args.each do |path|
|
data/lib/fast/fast.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Fast
|
2
|
+
# Returns a Dir with the file list already called (if a path is provided)
|
3
|
+
def self.dir path = nil
|
4
|
+
Dir.new.list path if path
|
5
|
+
Dir.new
|
6
|
+
end
|
7
|
+
|
8
|
+
# Like `dir`, but creates the directory if it does not exist
|
9
|
+
def self.dir! path
|
10
|
+
Dir.new.create( path ).list
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns a File with its content (if a path to a valid file is provided)
|
14
|
+
def self.file path = nil
|
15
|
+
File.new.read path if path
|
16
|
+
File.new
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns a File, and creates it if is does not exist
|
20
|
+
def self.file! path
|
21
|
+
File.new.create path
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns true if a directory named `path` exists, false otherwise.
|
25
|
+
# (wrapper for `File.directory? <path>`)
|
26
|
+
def self.dir? path
|
27
|
+
Dir.new.exist? path
|
28
|
+
end
|
29
|
+
|
30
|
+
# Example. This all will be deprecated when features are done
|
31
|
+
def self.file? path
|
32
|
+
File.new.exist? path
|
33
|
+
end
|
34
|
+
end
|
data/lib/fast/file.rb
CHANGED
@@ -22,7 +22,7 @@ module Fast
|
|
22
22
|
else
|
23
23
|
content = args.first
|
24
24
|
end
|
25
|
-
Fast.
|
25
|
+
Fast::Dir.new.create! ::File.dirname @path if ::File.dirname(@path) != "."
|
26
26
|
::File.open @path, "a" do |handler|
|
27
27
|
handler.write content
|
28
28
|
end
|
@@ -38,7 +38,7 @@ module Fast
|
|
38
38
|
else
|
39
39
|
content = args.first
|
40
40
|
end
|
41
|
-
Fast.
|
41
|
+
Fast::Dir.new.create! ::File.dirname @path if ::File.dirname(@path) != "."
|
42
42
|
::File.open @path, "w" do |handler|
|
43
43
|
handler.write content
|
44
44
|
end
|
data/lib/fast/version.rb
CHANGED
data/lib/fast.rb
CHANGED
@@ -4,62 +4,7 @@ require "fast/file"
|
|
4
4
|
require "fast/dir"
|
5
5
|
require "fast/dir-filter"
|
6
6
|
require "fast/file-filter"
|
7
|
-
|
8
|
-
module Fast
|
9
|
-
# Returns the list of entries in the directory
|
10
|
-
# Options:
|
11
|
-
#
|
12
|
-
# `:extension => ".jpg"` # Filters returned files by extension
|
13
|
-
# `:strip_extension => true` # If an `:extension` is given, returns the
|
14
|
-
# # entries without it
|
15
|
-
def self.dir path, options = nil
|
16
|
-
current = Dir.new path
|
17
|
-
if options
|
18
|
-
if options[:extension]
|
19
|
-
filtered = []
|
20
|
-
current.list path do |entry|
|
21
|
-
if entry.end_with? ".#{options[:extension]}"
|
22
|
-
if options[:strip_extension]
|
23
|
-
filtered << entry[0..-("#{options[:extension]}".length)-2]
|
24
|
-
else
|
25
|
-
filtered << entry
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
return filtered
|
30
|
-
end
|
31
|
-
else
|
32
|
-
current.list path
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# Like `dir`, but creates the directory if it does not exist
|
37
|
-
def self.dir! path, options = nil
|
38
|
-
Dir.new.create! path unless dir? path
|
39
|
-
dir path, options
|
40
|
-
end
|
41
|
-
|
42
|
-
# Returns true if a directory named `path` exists, false otherwise.
|
43
|
-
# (wrapper for `File.directory? <path>`)
|
44
|
-
def self.dir? 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
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.file! path
|
54
|
-
File.new.touch path
|
55
|
-
end
|
56
|
-
|
57
|
-
# Returns an instance of Fast::File and passed all methods
|
58
|
-
# and arguments to it
|
59
|
-
def self.file *args
|
60
|
-
Fast::File.call *args
|
61
|
-
end
|
62
|
-
end
|
7
|
+
require "fast/fast"
|
63
8
|
|
64
9
|
include Metafun::Delegator
|
65
10
|
|
@@ -4,9 +4,10 @@ require "zucker/os"
|
|
4
4
|
describe Fast::DirFilter do
|
5
5
|
describe "#extension" do
|
6
6
|
context "when given a list of file names and a extension" do
|
7
|
-
it "should return a
|
7
|
+
it "should return a Dir of the files with the given extension" do
|
8
8
|
list = ["file.txt", "data.txt", "thumbs.db", "data.txt.jpg"]
|
9
9
|
final_list = Fast::DirFilter.new( list ).extension "txt"
|
10
|
+
final_list.should be_a Fast::Dir
|
10
11
|
final_list.should include "file.txt"
|
11
12
|
final_list.should include "data.txt"
|
12
13
|
final_list.should_not include "thumbs.db"
|
@@ -14,7 +15,20 @@ describe Fast::DirFilter do
|
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
17
|
-
|
18
|
+
|
19
|
+
describe "#strip_extension" do
|
20
|
+
it "should return a Dir with the files names without extension" do
|
21
|
+
list = ["file.txt", "data.txt", "thumbs.db", "data.txt.jpg", "noext"]
|
22
|
+
final_list = Fast::DirFilter.new( list ).strip_extension
|
23
|
+
final_list.should be_a Fast::Dir
|
24
|
+
final_list.should include "file"
|
25
|
+
final_list.should include "data"
|
26
|
+
final_list.should include "thumbs"
|
27
|
+
final_list.should include "data.txt"
|
28
|
+
final_list.should include "noext"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
18
32
|
# VERY IMPORTANT: DirFilter should extend a class ListFilter
|
19
33
|
# #match implementation (along with a short list of other things) should
|
20
34
|
# be done in ListFilter, not in DirFilter
|
@@ -23,7 +37,16 @@ describe Fast::DirFilter do
|
|
23
37
|
# finishing Fast and after that I'll dig deeper into SubSetting
|
24
38
|
describe "#match" do
|
25
39
|
context "when given a list of strings and a regexp" do
|
26
|
-
it "should return a
|
40
|
+
it "should return a Dir containing the strings that match the expression" do
|
41
|
+
list = %w{is_a_file think_of_me not_my_file filesystem see_file_you}
|
42
|
+
final_list = Fast::DirFilter.new( list ).match /file/
|
43
|
+
final_list.should be_a Fast::Dir
|
44
|
+
final_list.should include "is_a_file"
|
45
|
+
final_list.should include "not_my_file"
|
46
|
+
final_list.should include "filesystem"
|
47
|
+
final_list.should include "see_file_you"
|
48
|
+
final_list.should_not include "think_of_me"
|
49
|
+
end
|
27
50
|
end
|
28
51
|
end
|
29
52
|
end
|
data/spec/fast/dir_spec.rb
CHANGED
@@ -107,6 +107,11 @@ describe Fast::Dir do
|
|
107
107
|
end
|
108
108
|
|
109
109
|
shared_examples_for "any dir creation" do
|
110
|
+
it "should fail if no argument is passed" do
|
111
|
+
expect { Fast::Dir.new.send @method
|
112
|
+
}.to raise_error ArgumentError, "No arguments passed, at least one is required"
|
113
|
+
end
|
114
|
+
|
110
115
|
context "is a simple path" do
|
111
116
|
it "should create the dir" do
|
112
117
|
::File.should_not be_directory "demo"
|
@@ -170,11 +175,15 @@ describe Fast::Dir do
|
|
170
175
|
describe "#create" do
|
171
176
|
before :each do @method = :create end
|
172
177
|
it_behaves_like "any dir creation"
|
178
|
+
|
179
|
+
it "should fail if the dir already exists"
|
173
180
|
end
|
174
181
|
|
175
182
|
describe "#create!" do
|
176
183
|
before :each do @method = :create! end
|
177
184
|
it_behaves_like "any dir creation"
|
185
|
+
|
186
|
+
it "should do nothing if the dir already exists"
|
178
187
|
end
|
179
188
|
|
180
189
|
shared_examples_for "any dir subsetter" do
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "fast"
|
2
|
+
|
3
|
+
describe Fast do
|
4
|
+
describe ".dir" do
|
5
|
+
it "should return an instance of Fast::Dir" do
|
6
|
+
Fast.dir.should be_a Fast::Dir
|
7
|
+
end
|
8
|
+
|
9
|
+
context "a path to a dir is passed" do
|
10
|
+
it "should call #list on that instance" do
|
11
|
+
module Fast
|
12
|
+
Dir.any_instance.should_receive :list
|
13
|
+
end
|
14
|
+
|
15
|
+
Fast.dir :demo
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".dir!" do
|
21
|
+
it "should fail if no argument is passed" do
|
22
|
+
expect { Fast.dir!
|
23
|
+
}.to raise_error ArgumentError
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should call #create on that instance" do # So hakzee this one...
|
27
|
+
module Fast
|
28
|
+
Dir.any_instance.should_receive( :create ).and_return Dir.new( :demo )
|
29
|
+
end
|
30
|
+
|
31
|
+
Fast.dir.create! :demo
|
32
|
+
Fast.dir! :demo
|
33
|
+
Fast.dir.delete! :demo
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should call #list on that instance" do
|
37
|
+
module Fast
|
38
|
+
Dir.any_instance.should_receive( :list ).and_return Dir.new
|
39
|
+
end
|
40
|
+
|
41
|
+
Fast.dir! :demo
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe ".dir?" do
|
46
|
+
before do
|
47
|
+
Fast.dir.delete! :demo
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return true if passed dir exists" do
|
51
|
+
Fast.dir.create! :demo
|
52
|
+
Fast.dir?(:demo).should be_true
|
53
|
+
Fast.dir.delete! :demo
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return false otherwise" do
|
57
|
+
Fast.dir?(:demo).should be_false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe ".file" do
|
62
|
+
it "should return an instance of Fast::File" do
|
63
|
+
Fast.file.should be_a Fast::File
|
64
|
+
end
|
65
|
+
|
66
|
+
context "called with argument" do
|
67
|
+
it "should call #read on that instance" do
|
68
|
+
Fast.file.touch "demo.file"
|
69
|
+
module Fast
|
70
|
+
File.any_instance.should_receive :read
|
71
|
+
end
|
72
|
+
|
73
|
+
Fast.file "demo.file"
|
74
|
+
Fast.file.delete! "demo.file"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe ".file!" do
|
80
|
+
it "should fail if not argument is passed" do
|
81
|
+
expect { Fast.file!
|
82
|
+
}.to raise_error ArgumentError
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should call #create on that instance" do
|
86
|
+
module Fast
|
87
|
+
File.any_instance.should_receive :create
|
88
|
+
end
|
89
|
+
|
90
|
+
Fast.file! "demo.file"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe ".file?" do
|
95
|
+
before do
|
96
|
+
Fast.file.should_not exist "demo.file"
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should return true if file exists" do
|
100
|
+
Fast.file.create! "demo.file"
|
101
|
+
Fast.file?("demo.file").should be_true
|
102
|
+
Fast.file.delete! "demo.file"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should return false otherwise" do
|
106
|
+
Fast.file?("demo.file").should be_false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Xavier Via
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: metafun
|
@@ -77,11 +77,13 @@ files:
|
|
77
77
|
- lib/fast.rb
|
78
78
|
- lib/fast/dir-filter.rb
|
79
79
|
- lib/fast/dir.rb
|
80
|
+
- lib/fast/fast.rb
|
80
81
|
- lib/fast/file-filter.rb
|
81
82
|
- lib/fast/file.rb
|
82
83
|
- lib/fast/version.rb
|
83
84
|
- spec/fast/dir-filter_spec.rb
|
84
85
|
- spec/fast/dir_spec.rb
|
86
|
+
- spec/fast/fast_spec.rb
|
85
87
|
- spec/fast/file_spec.rb
|
86
88
|
homepage: ""
|
87
89
|
licenses: []
|