Sha256: 28518e15b3d9371f5d3a45e8e2a9e88b5efa8e9c242707461262bc5039b2b7ae

Contents?: true

Size: 1.95 KB

Versions: 56

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module RelatonBib
  # Document relation collection
  class DocRelationCollection
    extend Forwardable

    def_delegators :@array, :<<, :[], :first, :last, :empty?, :any?, :size,
                   :each, :detect, :map, :reduce, :length, :unshift, :max_by

    # @param relation [Array<RelatonBib::DocumentRelation, Hash>]
    # @option relation [String] :type
    # @option relation [String] :identifier
    # @option relation [String, NIllClass] :url (nil)
    # @option relation [Array<RelatonBib::Locality,
    #                   RelatonBib::LocalityStack>] :locality
    # @option relation [Array<RelatonBib::SourceLocality,
    #                   RelatonBib::SourceLocalityStack>] :source_locality
    # @option relation [RelatonBib::BibliographicItem, NillClass] :bibitem (nil)
    def initialize(relation)
      @array = relation.map { |r| r.is_a?(Hash) ? DocumentRelation.new(**r) : r }
    end

    #
    # Returns new DocumentRelationCollection with selected relations.
    #
    # @example Select relations with type "obsoletes"
    #   relations.select { |r| r.type == "obsoletes" }
    #   #=> <RelatonBib::DocRelationCollection:0x00007f9a0191d5f0 @array=[...]>
    #
    # @return [RelatonBib::DocRelationCollection] new DocumentRelationCollection
    #   with selected relations
    #
    def select(&block)
      arr = @array.select(&block)
      self.class.new arr
    end

    # @todo We don't have replace type anymore. Suppose we should update this
    #   method or delete it.
    #
    # @return [RelatonBib::DocRelationCollection]
    def replaces
      DocRelationCollection.new(@array.select { |r| r.type == "replace" })
    end

    # @param prefix [String]
    # @return [String]
    def to_asciibib(prefix = "")
      pref = prefix.empty? ? "relation" : "#{prefix}.relation"
      out = ""
      @array.each do |r|
        out += size > 1 ? "#{pref}::\n" : ""
        out += r.to_asciibib pref
      end
      out
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
relaton-bib-1.19.7 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.6 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.5 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.4 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.3 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.2 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.1 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.19.0 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.18.5 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.18.3 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.18.2 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.18.1 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.18.0 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.17.2 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.17.1 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.17.0 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.16.5 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.16.4 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.16.3 lib/relaton_bib/document_relation_collection.rb
relaton-bib-1.16.2 lib/relaton_bib/document_relation_collection.rb