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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d031d969fbd0db57f5ec06b0a02d363433312a3
4
- data.tar.gz: 0bb0fea0e4bfbffc1f0bdd21ca4dc17276d5a9c8
3
+ metadata.gz: 943eb63a834b2c3622df251eeecb0d16c46fc860
4
+ data.tar.gz: 4cdf00730e7ad425f768667891b9f23a0182e834
5
5
  SHA512:
6
- metadata.gz: 1dbe528955a923d9a448a5e17161eaa8d6a12960fa52d6fed879f02398997689eea16da8941452371024563cd55ff047124e19af151f96768e7c13e6d836efd5
7
- data.tar.gz: 0970f3cf25f9cf6c0cb10e79612aa5214db99f9a950af6e4ebd05911e35dc88d2d7ceef4205b24d13132841c8ef85222d95cc3226a343e578f604cfc79124349
6
+ metadata.gz: 51e364d0b062290793fe40c6e6ddfbd9b4def9f56f5b41df971a61bae9b204a2c26037462404f8546a860b8646d11fd87794abb7457e4560ca7435e20fd031df
7
+ data.tar.gz: 6ffead0bd9caea29da9e8476fe01f2383baec42c9f72df930d820d73259132139e0d3c24e40548392db9334a45762ee41d8490930bc8273d0ee4f535c2344d23
@@ -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 !!/#\s*stub\s*/i.match(line)
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 !!/#\s*endstub\s*/i.match(line)
20
+ return false if /#\s*endstub\s*/i.match(line)
19
21
  stubbing
20
22
  end
21
23
 
@@ -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
- !!/^\.?\w+$/.match(directory)
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 is_glob?(file)
16
- !!/\*/.match(file)
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 << (is_glob?(file) ? Dir.glob(file) : file) }
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
- !!/\//.match(file)
28
+ !(%r{\/} =~ file).nil?
27
29
  end
28
30
 
29
- def parse_files_in_directories(file)
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 << parse_files_in_directories(file) if in_directory?(file)
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
- .concat(parse_out_directories(files))
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
- raise 'Not enough arguments' if args.size < 2
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
- raise 'The last argument needs to be a directory' unless PathParser.directory?(target)
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 { |dir| Dir.mkdir("#{target}/#{dir}") unless dir == target }
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rstub'
3
- s.version = '0.1.1'
3
+ s.version = '0.1.2'
4
4
 
5
5
  s.date = '2015-05-30'
6
6
  s.summary = 'A gem to stub out code'
@@ -14,7 +14,7 @@ describe FileParser do
14
14
  end
15
15
 
16
16
  def add_text(text)
17
- File.open('foo.rb', 'w') { |f| f.puts text}
17
+ File.open('foo.rb', 'w') { |f| f.puts text }
18
18
  end
19
19
 
20
20
  def stub_all
@@ -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]).to match_array(['foo.rb'])
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({ directories: ['foobar'],
34
- files: ['baz.rb', 'foo.rb', 'foobar/foobaz.rb']})
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(['foo', 'foobar'])
40
+ .to match_array(%w(foo foobar))
40
41
  expect(path_parser.get_globs(['*', 'foo/bar.rb'])[:files])
41
- .to match_array(['foo/bar.rb', 'baz.rb', 'foo.rb', 'foobar/foobaz.rb'])
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({ directories: ['foobar'],
47
- files: ['foobar/foobaz.rb'] })
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Kim