Class: Spectator::SpecsMatcher

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

Instance Attribute Summary collapse

Class Method 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.



45
46
47
# File 'lib/spectator/specs_matcher.rb', line 45

def config
  @config
end

#filesObject (readonly)

Returns the value of attribute files.



45
46
47
# File 'lib/spectator/specs_matcher.rb', line 45

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

Class Method Details

.reset_matchable_spec_files!Object



41
42
43
# File 'lib/spectator/specs_matcher.rb', line 41

def self.reset_matchable_spec_files!
  @@all_matchable_spec_files = nil
end

Instance Method Details

#all_matchable_spec_filesObject



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

def all_matchable_spec_files
  @@all_matchable_spec_files ||= Dir['**/**'].grep(%r{^#{config.spec_dir_regexp}})
end

#match_specs(matchable_paths) ⇒ Object



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

def match_specs matchable_paths
  matchable_paths.uniq.flat_map do |path|
    all_matchable_spec_files.grep(/\b#{path}_spec\.rb$/)
  end
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