Class: RBS::Collection::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/collection/cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_path:) ⇒ Cleaner

Returns a new instance of Cleaner.



8
9
10
# File 'lib/rbs/collection/cleaner.rb', line 8

def initialize(lockfile_path:)
  @lock = Config.from_path(lockfile_path)
end

Instance Attribute Details

#lockObject (readonly)

Returns the value of attribute lock.



6
7
8
# File 'lib/rbs/collection/cleaner.rb', line 6

def lock
  @lock
end

Instance Method Details

#cleanObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbs/collection/cleaner.rb', line 12

def clean
  lock.repo_path.glob('*/*') do |dir|
    *_, gem_name, version = dir.to_s.split('/')
    gem_name or raise
    version or raise
    next if needed? gem_name, version

    case
    when dir.symlink?
      dir.unlink
    when dir.directory?
      FileUtils.remove_entry_secure(dir.to_s)
    else
      raise
    end
  end
end

#needed?(gem_name, version) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/rbs/collection/cleaner.rb', line 30

def needed?(gem_name, version)
  gem = lock.gem(gem_name)
  return false unless gem

  gem['version'] == version
end