Sha256: 88c02a728c26830fecc8ef2562439850778b4e46a47cfdbacd8cfbdd59294d8e

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module RelatonBib
  # Document identifier.
  class DocumentIdentifier
    # @return [String]
    attr_reader :id

    # @return [String, NilClass]
    attr_reader :type, :scope

    # @param id [String]
    # @param type [String, NilClass]
    # @param scoope [String, NilClass]
    def initialize(id:, type: nil, scope: nil)
      @id    = id
      @type  = type
      @scope = scope
    end

    # in docid manipulations, assume ISO as the default: id-part:year
    def remove_part
      case @type
      when "Chinese Standard" then @id.sub!(/\.\d+/, "")
      else
        @id.sub!(/-\d+/, "")
      end
    end

    def remove_date
      case @type
      when "Chinese Standard" then @id.sub!(/-[12]\d\d\d/, "")
      else
        @id.sub!(/:[12]\d\d\d/, "")
      end
    end

    def all_parts
      @id += " (all parts)"
    end

    #
    # Add docidentifier xml element
    #
    # @param [Nokogiri::XML::Builder] builder
    #
    def to_xml(builder)
      element = builder.docidentifier id
      element[:type] = type if type
      element[:scope] = scope if scope
    end

    # @return [Hash]
    def to_hash
      hash = { "id" => id }
      hash["type"] = type if type
      hash["scope"] = scope if scope
      hash
    end

    # @param prefix [String]
    # @param count [Integer] number of docids
    # @return [String]
    def to_asciibib(prefix = "", count = 1)
      pref = prefix.empty? ? prefix : prefix + "."
      out = count > 1 ? "#{pref}docid::\n" : ""
      out += "#{pref}docid.type:: #{type}\n" if type
      out += "#{pref}docid.scope:: #{scope}\n" if scope
      out += "#{pref}docid.id:: #{id}\n"
      out
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
relaton-bib-1.3.1 lib/relaton_bib/document_identifier.rb
relaton-bib-1.3.0 lib/relaton_bib/document_identifier.rb