Class: BibSync::Actions::SynchronizeFiles

Inherits:
Object
  • Object
show all
Includes:
Log, Utils
Defined in:
lib/bibsync/actions/synchronize_files.rb

Constant Summary collapse

FileTypes =
%w(djvu pdf ps)

Constants included from Log

Log::Blue, Log::Level, Log::Red, Log::Reset, Log::Yellow

Instance Method Summary collapse

Methods included from Log

#log

Methods included from Utils

#arxiv_download, #arxiv_id, #fetch, #fetch_html, #fetch_xml, #name_without_ext

Constructor Details

#initialize(options) ⇒ SynchronizeFiles

Returns a new instance of SynchronizeFiles.



9
10
11
12
# File 'lib/bibsync/actions/synchronize_files.rb', line 9

def initialize(options)
  raise 'Option --bib is required' unless @bib = options[:bib]
  raise 'Option --dir is required' unless @dir = options[:dir]
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bibsync/actions/synchronize_files.rb', line 14

def run
  notice 'Synchronize with files'

  files = {}
  Dir[File.join(@dir, "**/*.{#{FileTypes.join(',')}}")].sort.each do |file|
    name = File.basename(file)
    key = name_without_ext(name)
    raise "Duplicate file #{name}" if files[key]
    files[key] = file
  end

  files.each do |key, file|
    unless entry = @bib[key]
      info('New file', key: key)
      entry = Entry.new(key: key)
      @bib << entry
    end

    entry.type ||= :ARTICLE
    entry.file = file
  end

  @bib.save
end