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



39
40
41
42
43
44
# File 'lib/retest/repository.rb', line 39

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



46
47
48
49
50
51
52
53
54
# File 'lib/retest/repository.rb', line 46

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



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

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