Sha256: 1f4973b2837212335c7f41a46746c266b882623e3b83356d2770f69e883bfc07

Contents?: true

Size: 1.41 KB

Versions: 9

Compression:

Stored size: 1.41 KB

Contents

require_relative "databases/bibtex"
require_relative "databases/rfc"
require_relative "errors"

module AsciidoctorBibliography
  # This is an array of citeproc entries.
  class Database < Array
    def initialize(*filepaths)
      filepaths.each do |filepath|
        append filepath
      end
    end

    def append(filepath)
      concat Database.load(filepath)
      ensure_no_conflicts!
      self
    end

    def find_entry_by_id(id)
      result = detect { |entry| entry["id"] == id }
      if result.nil?
        message = "No entry with id '#{id}' was found in the bibliographic database."
        raise Errors::Database::IdNotFound, message
      end
      result
    end

    def self.load(filepath)
      raise Errors::Database::FileNotFound, filepath unless File.exist?(filepath)

      fileext = File.extname filepath
      case fileext
      when *Databases::BibTeX::EXTENSIONS
        Databases::BibTeX.load filepath
      when *Databases::RFC::EXTENSIONS
        Databases::RFC.load filepath
      else
        raise Errors::Database::UnsupportedFormat, fileext
      end
    end

    private

    def ensure_no_conflicts!
      ids = map { |entry| entry["id"] }
      conflicting_ids = ids.select { |id| ids.count(id) > 1 }.uniq.sort
      raise Errors::Database::ConflictingIds, <<~MESSAGE if conflicting_ids.any?
        Conflicting ids were found during database import: #{conflicting_ids}.
      MESSAGE
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
asciidoctor-bibliography-0.11.0 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.10.3 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.10.2 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.10.1 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.10.0 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.9.2 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.9.1 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.9.0 lib/asciidoctor-bibliography/database.rb
asciidoctor-bibliography-0.8.0 lib/asciidoctor-bibliography/database.rb