rstub 0.1.1 → 0.1.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.
- checksums.yaml +4 -4
- data/lib/rstub/file_parser.rb +4 -2
- data/lib/rstub/path_parser.rb +10 -8
- data/lib/rstub.rb +9 -4
- data/rstub.gemspec +1 -1
- data/spec/file_parser_spec.rb +1 -1
- data/spec/path_parser_spec.rb +8 -7
- data/spec/rstub_spec.rb +5 -5
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 943eb63a834b2c3622df251eeecb0d16c46fc860
|
|
4
|
+
data.tar.gz: 4cdf00730e7ad425f768667891b9f23a0182e834
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 51e364d0b062290793fe40c6e6ddfbd9b4def9f56f5b41df971a61bae9b204a2c26037462404f8546a860b8646d11fd87794abb7457e4560ca7435e20fd031df
|
|
7
|
+
data.tar.gz: 6ffead0bd9caea29da9e8476fe01f2383baec42c9f72df930d820d73259132139e0d3c24e40548392db9334a45762ee41d8490930bc8273d0ee4f535c2344d23
|
data/lib/rstub/file_parser.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# FileParser goes through a file and writes the contents of it to a target file,
|
|
2
|
+
# but it ignores anything between the delimiters # STUB and # ENDSTUB.
|
|
1
3
|
class FileParser
|
|
2
4
|
def stub(target_file, file)
|
|
3
5
|
File.open(file, 'r') do |readable_file|
|
|
@@ -10,12 +12,12 @@ class FileParser
|
|
|
10
12
|
private
|
|
11
13
|
|
|
12
14
|
def start_stubbing?(line, stubbing)
|
|
13
|
-
return true if
|
|
15
|
+
return true if /#\s*stub\s*/i.match(line)
|
|
14
16
|
stubbing
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def end_stubbing?(line, stubbing)
|
|
18
|
-
return false if
|
|
20
|
+
return false if /#\s*endstub\s*/i.match(line)
|
|
19
21
|
stubbing
|
|
20
22
|
end
|
|
21
23
|
|
data/lib/rstub/path_parser.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
# PathParser parses directory and file paths into a hash with directory and file
|
|
2
|
+
# arrays.
|
|
1
3
|
class PathParser
|
|
2
4
|
def self.directory?(directory)
|
|
3
|
-
|
|
5
|
+
!(/^\.?\w+$/ =~ directory).nil?
|
|
4
6
|
end
|
|
5
7
|
|
|
6
8
|
def get_globs(files)
|
|
@@ -12,35 +14,35 @@ class PathParser
|
|
|
12
14
|
|
|
13
15
|
private
|
|
14
16
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
+
def glob?(file)
|
|
18
|
+
!(/\*/ =~ file).nil?
|
|
17
19
|
end
|
|
18
20
|
|
|
19
21
|
def check_globs(files)
|
|
20
22
|
new_files = []
|
|
21
|
-
files.each { |file| new_files << (
|
|
23
|
+
files.each { |file| new_files << (glob?(file) ? Dir.glob(file) : file) }
|
|
22
24
|
new_files.flatten
|
|
23
25
|
end
|
|
24
26
|
|
|
25
27
|
def in_directory?(file)
|
|
26
|
-
|
|
28
|
+
!(%r{\/} =~ file).nil?
|
|
27
29
|
end
|
|
28
30
|
|
|
29
|
-
def
|
|
31
|
+
def extract_directories(file)
|
|
30
32
|
file.split('/').slice(0...-1)
|
|
31
33
|
end
|
|
32
34
|
|
|
33
35
|
def parse_out_directories(files)
|
|
34
36
|
directories = []
|
|
35
37
|
files.each do |file|
|
|
36
|
-
directories <<
|
|
38
|
+
directories << extract_directories(file) if in_directory?(file)
|
|
37
39
|
end
|
|
38
40
|
directories.flatten
|
|
39
41
|
end
|
|
40
42
|
|
|
41
43
|
def get_directories(files)
|
|
42
44
|
files.select { |file| PathParser.directory?(file) }
|
|
43
|
-
|
|
45
|
+
.concat(parse_out_directories(files))
|
|
44
46
|
end
|
|
45
47
|
|
|
46
48
|
def get_files_from_directory(files, directories)
|
data/lib/rstub.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
require 'rstub/file_parser'
|
|
2
2
|
require 'rstub/path_parser'
|
|
3
3
|
|
|
4
|
+
# RStub takes command line arguments, calls PathParser to parse them, creates
|
|
5
|
+
# new files according to those arguments, and calls FileParser to parse those
|
|
6
|
+
# files and write them to the newly generated files.
|
|
4
7
|
class RStub
|
|
5
8
|
attr_reader :file_parser, :path_parser
|
|
6
9
|
attr_accessor :target, :files, :directories, :target_files
|
|
@@ -11,7 +14,7 @@ class RStub
|
|
|
11
14
|
end
|
|
12
15
|
|
|
13
16
|
def start(args = [])
|
|
14
|
-
|
|
17
|
+
fail ArgumentError, 'Not enough arguments' if args.size < 2
|
|
15
18
|
parse_args(args)
|
|
16
19
|
make_new_directory_structure
|
|
17
20
|
parse_files
|
|
@@ -24,7 +27,9 @@ class RStub
|
|
|
24
27
|
# string
|
|
25
28
|
def parse_args(args)
|
|
26
29
|
self.target = args.pop
|
|
27
|
-
|
|
30
|
+
unless PathParser.directory?(target)
|
|
31
|
+
fail ArgumentError, 'The last argument needs to be a directory'
|
|
32
|
+
end
|
|
28
33
|
self.files = args
|
|
29
34
|
end
|
|
30
35
|
|
|
@@ -33,7 +38,7 @@ class RStub
|
|
|
33
38
|
end
|
|
34
39
|
|
|
35
40
|
def make_new_directories
|
|
36
|
-
directories.each { |
|
|
41
|
+
directories.each { |d| Dir.mkdir("#{target}/#{d}") unless d == target }
|
|
37
42
|
end
|
|
38
43
|
|
|
39
44
|
def make_new_files
|
|
@@ -43,7 +48,7 @@ class RStub
|
|
|
43
48
|
def parse_files_and_directories
|
|
44
49
|
parsed_path = path_parser.get_globs(files)
|
|
45
50
|
self.files = parsed_path[:files].select { |file| File.exist? file }
|
|
46
|
-
self.target_files = files.map { |file| "#{target}/#{file}"}
|
|
51
|
+
self.target_files = files.map { |file| "#{target}/#{file}" }
|
|
47
52
|
self.directories = parsed_path[:directories]
|
|
48
53
|
end
|
|
49
54
|
|
data/rstub.gemspec
CHANGED
data/spec/file_parser_spec.rb
CHANGED
data/spec/path_parser_spec.rb
CHANGED
|
@@ -25,26 +25,27 @@ describe PathParser do
|
|
|
25
25
|
let(:path_parser) { PathParser.new }
|
|
26
26
|
|
|
27
27
|
it 'returns the same array when there is no match' do
|
|
28
|
-
expect(path_parser.get_globs(['foo.rb'])[:files])
|
|
28
|
+
expect(path_parser.get_globs(['foo.rb'])[:files])
|
|
29
|
+
.to match_array(['foo.rb'])
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
it 'returns glob matches from current directory' do
|
|
32
33
|
expect(path_parser.get_globs(['*']))
|
|
33
|
-
.to match_array(
|
|
34
|
-
|
|
34
|
+
.to match_array(directories: ['foobar'],
|
|
35
|
+
files: %w(baz.rb foo.rb foobar/foobaz.rb))
|
|
35
36
|
end
|
|
36
37
|
|
|
37
38
|
it 'adds glob matches to the rest of the files' do
|
|
38
39
|
expect(path_parser.get_globs(['*', 'foo/bar.rb'])[:directories])
|
|
39
|
-
.to match_array(
|
|
40
|
+
.to match_array(%w(foo foobar))
|
|
40
41
|
expect(path_parser.get_globs(['*', 'foo/bar.rb'])[:files])
|
|
41
|
-
.to match_array(
|
|
42
|
+
.to match_array(%w(foo/bar.rb baz.rb foo.rb foobar/foobaz.rb))
|
|
42
43
|
end
|
|
43
44
|
|
|
44
45
|
it 'returns glob matches from lowel directory' do
|
|
45
46
|
expect(path_parser.get_globs(['*/*']))
|
|
46
|
-
.to match_array(
|
|
47
|
-
|
|
47
|
+
.to match_array(directories: ['foobar'],
|
|
48
|
+
files: ['foobar/foobaz.rb'])
|
|
48
49
|
end
|
|
49
50
|
end
|
|
50
51
|
end
|
data/spec/rstub_spec.rb
CHANGED
|
@@ -21,16 +21,16 @@ describe RStub do
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it 'raises error with invalid number of arguments' do
|
|
24
|
-
expect{rstub.start}.to raise_error('Not enough arguments')
|
|
25
|
-
expect{rstub.start(['foo'])}.to raise_error('Not enough arguments')
|
|
24
|
+
expect { rstub.start }.to raise_error('Not enough arguments')
|
|
25
|
+
expect { rstub.start(['foo']) }.to raise_error('Not enough arguments')
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it 'raises error if the last argument isn\'t a directory' do
|
|
29
|
-
expect{rstub.start(['foo.rb', 'baz.rb'])}
|
|
29
|
+
expect { rstub.start(['foo.rb', 'baz.rb']) }
|
|
30
30
|
.to raise_error('The last argument needs to be a directory')
|
|
31
|
-
expect{rstub.start(['target', 'foo.rb'])}
|
|
31
|
+
expect { rstub.start(['target', 'foo.rb']) }
|
|
32
32
|
.to raise_error('The last argument needs to be a directory')
|
|
33
|
-
expect{rstub.start(['foo.rb', '*'])}
|
|
33
|
+
expect { rstub.start(['foo.rb', '*']) }
|
|
34
34
|
.to raise_error('The last argument needs to be a directory')
|
|
35
35
|
end
|
|
36
36
|
|