Class: Spectator::SpecsMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/spectator/specs_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SpecsMatcher

Returns a new instance of SpecsMatcher.



6
7
8
9
10
11
12
# File 'lib/spectator/specs_matcher.rb', line 6

def initialize(config)
  @config = config
  @matchers = [
    %r{^#{config.spec_dir_regexp}/(.*)_spec\.rb$},
    %r{^(?:#{config.base_dir_regexp})/(.*)(?:\.rb|\.\w+|)$},
  ]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



38
39
40
# File 'lib/spectator/specs_matcher.rb', line 38

def config
  @config
end

#filesObject (readonly)

Returns the value of attribute files.



38
39
40
# File 'lib/spectator/specs_matcher.rb', line 38

def files
  @files
end

#matchersObject (readonly)

Returns the value of attribute matchers.



13
14
15
# File 'lib/spectator/specs_matcher.rb', line 13

def matchers
  @matchers
end

Instance Method Details

#match_specs(matchable_paths) ⇒ Object



31
32
33
34
35
36
# File 'lib/spectator/specs_matcher.rb', line 31

def match_specs matchable_paths
  matchable_paths.uniq.map do |path|
    # Dir['**/**'].grep(%r{^#{config.spec_dir_regexp}}).grep(/\b#{path}((_spec)?\.rb)?$/)
    Dir['**/**'].grep(%r{^#{config.spec_dir_regexp}}).grep(/\b#{path}_spec\.rb$/)
  end.flatten
end

#matchable_paths(file) ⇒ Object



25
26
27
28
29
# File 'lib/spectator/specs_matcher.rb', line 25

def matchable_paths(file)
  matchers.map do |matcher|
    file.scan(matcher).flatten.first.to_s.gsub(/\.rb$/,'')
  end.flatten.reject(&:empty?)
end

#specs_for(files) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/spectator/specs_matcher.rb', line 15

def specs_for(files)
  files.flat_map do |path|
    matchable_paths = self.matchable_paths(path)
    print "--- Searching specs for #{path.inspect} (as: #{matchable_paths.join(", ")})...".yellow
    specs = match_specs(matchable_paths)
    puts specs.empty? ? ' nothing found.'.red : " #{specs.size} matched.".green
    specs
  end
end