Class: Retest::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/retest/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files: [], cache: {}, prompt: nil) ⇒ Repository

Returns a new instance of Repository.



5
6
7
8
9
# File 'lib/retest/repository.rb', line 5

def initialize(files: [], cache: {}, prompt: nil)
  @cache  = cache
  @files  = files
  @prompt = prompt || Prompt.new
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



3
4
5
# File 'lib/retest/repository.rb', line 3

def cache
  @cache
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/retest/repository.rb', line 3

def files
  @files
end

#promptObject

Returns the value of attribute prompt.



3
4
5
# File 'lib/retest/repository.rb', line 3

def prompt
  @prompt
end

Instance Method Details

#add(added) ⇒ Object



43
44
45
46
47
48
# File 'lib/retest/repository.rb', line 43

def add(added)
  return if added&.empty?

  files.push(*added)
  files.sort!
end

#find_test(path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/retest/repository.rb', line 11

def find_test(path)
  return unless path
  return if path.empty?

  unless cache.key?(path)
    ok_to_cache, test_file = select_from path, MatchingOptions.for(path, files: files)
    if ok_to_cache
      cache[path] = test_file
    end
  end

  cache[path]
end

#find_tests(paths) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/retest/repository.rb', line 25

def find_tests(paths)
  paths
    .select { |path| Regexp.new("\.rb$") =~ path }
    .map    { |path| find_test(path) }
    .compact
    .uniq
    .sort
end

#remove(removed) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/retest/repository.rb', line 50

def remove(removed)
  return if removed&.empty?

  if removed.is_a?(Array)
    removed.each { |file| files.delete(file) }
  else
    files.delete(removed)
  end
end

#sync(added:, removed:) ⇒ Object



38
39
40
41
# File 'lib/retest/repository.rb', line 38

def sync(added:, removed:)
  add(added)
  remove(removed)
end

#test_filesObject



34
35
36
# File 'lib/retest/repository.rb', line 34

def test_files
  files.select { |file| MatchingOptions::Path.new(file).test? }
end