Sha256: 1376a965b15cddab6c2e6f26bdc968981fecc9dbb051359302a1056eae470767

Contents?: true

Size: 1.31 KB

Versions: 10

Compression:

Stored size: 1.31 KB

Contents

require "thor"
require "relaton"
require_relative "cli/command"

module Relaton
  def self.db
    Cli.relaton
  end

  module Cli
    class RelatonDb
      include Singleton

      def db
        @db ||= Relaton::Db.new("#{Dir.home}/.relaton/cache", nil)
      end
    end

    def self.start(arguments)
      Relaton::Cli::Command.start(arguments)
    end

    # Relaton
    #
    # Based on current setup, we need to initiate a Db instance to
    # register all of it's supported processor backends. To make it
    # easier we have added it as a class method so we can use this
    # whenever necessary.
    #
    def self.relaton
      RelatonDb.instance.db
    end

    # @param content [Nokogiri::XML::Document]
    # @return [RelatonBib::BibliographicItem, RelatonIsoBib::IsoBibliongraphicItem]
    def self.parse_xml(doc)
      if (proc = Cli.processor(doc))
        proc.from_xml(doc.to_s)
      else
        RelatonBib::XMLParser.from_xml(doc.to_s)
      end
    end

    # @param doc [Nokogiri::XML::Element]
    # @return [String] Type prefix
    def self.processor(doc)
      docid = doc.at "docidentifier"
      if docid && docid[:type]
        proc = Relaton::Registry.instance.by_type(docid[:type])
        return proc if proc
      end
      Relaton::Registry.instance.by_type(docid&.text&.match(/^\w+/)&.to_s)
    end
  end
end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
relaton-cli-1.5.0 lib/relaton/cli.rb
relaton-cli-1.5.pre lib/relaton/cli.rb
relaton-cli-1.4.0 lib/relaton/cli.rb
relaton-cli-1.3.0 lib/relaton/cli.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/relaton-cli-1.2.0/lib/relaton/cli.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/relaton-cli-1.2.1/lib/relaton/cli.rb
relaton-cli-1.2.1 lib/relaton/cli.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/relaton-cli-1.2.0/lib/relaton/cli.rb
relaton-cli-1.2.0 lib/relaton/cli.rb
relaton-cli-1.1.4 lib/relaton/cli.rb