Class: TrelloFs::AttachmentCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/trello-fs/attachment_cleaner.rb

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ AttachmentCleaner

Returns a new instance of AttachmentCleaner.



6
7
8
# File 'lib/trello-fs/attachment_cleaner.rb', line 6

def initialize(repository)
  @repository = repository
end

Instance Method Details

#remove_old_attachmentsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trello-fs/attachment_cleaner.rb', line 16

def remove_old_attachments
  new_attachments = set_of_attachment_paths

  Dir.
    glob(File.join(@repository.path, "Attachments/**/*")).
    reject {|fn| File.directory?(fn) }.
    each do |file|
      next if new_attachments.include? file
      FileUtils.rm(file)

      # remove parent dir if empty
      dirname = File.dirname(file)
      FileUtils.rm_rf(dirname) if Dir[File.join(dirname, '*')].empty?
    end
end

#set_of_attachment_pathsObject



10
11
12
13
14
# File 'lib/trello-fs/attachment_cleaner.rb', line 10

def set_of_attachment_paths
  @repository.attachments.map do |attachment|
    AttachmentBuilder.new_by_attachment(@repository, attachment).path
  end.to_set
end