Class: Splot::CorrespondingFile

Inherits:
Object
  • Object
show all
Defined in:
lib/splot/corresponding_file.rb

Class Method Summary collapse

Class Method Details

.fetch(file) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/splot/corresponding_file.rb', line 3

def self.fetch(file)
  catch :test_file do
    root_path = Pathname.new File.expand_path Dir.pwd
    file_path = Pathname.new File.expand_path file
    relative_path = file_path.relative_path_from(root_path).to_s
    if %r{\Alib/(?<bit>.*)\.rb} =~ relative_path
      ['test', 'spec'].each do |e|
        try_file "#{e}/lib/#{bit}_#{e}.rb"
        try_file "#{e}/#{bit}_#{e}.rb"
      end
    end
    if %r{\Aapp/(?<subdir>[a-z_]+)/(?<bit>.*)\.rb} =~ relative_path
      ['test', 'spec'].each do |e|
        alternate_subdir = {
          models:      "unit",
          controllers: "functional",
        }[subdir.to_sym]
        try_file "#{e}/#{subdir}/#{bit}_#{e}.rb"
        try_file "#{e}/#{alternate_subdir}/#{bit}_#{e}.rb" if alternate_subdir
      end
    end
    file
  end
end

.file_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/splot/corresponding_file.rb', line 32

def self.file_exists?(file)
  File.size? file
end

.try_file(test_path) ⇒ Object



28
29
30
# File 'lib/splot/corresponding_file.rb', line 28

def self.try_file(test_path)
  throw :test_file, test_path if file_exists? test_path
end