Class: RelatonBib::DocRelationCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/relaton_bib/document_relation_collection.rb

Overview

Document relation collection

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ DocRelationCollection

Returns a new instance of DocRelationCollection.

Options Hash (relation):



18
19
20
# File 'lib/relaton_bib/document_relation_collection.rb', line 18

def initialize(relation)
  @array = relation.map { |r| r.is_a?(Hash) ? DocumentRelation.new(**r) : r }
end

Instance Method Details

#replacesRelatonBib::DocRelationCollection



41
42
43
# File 'lib/relaton_bib/document_relation_collection.rb', line 41

def replaces
  DocRelationCollection.new(@array.select { |r| r.type == "replace" })
end

#select(&block) ⇒ RelatonBib::DocRelationCollection

Returns new DocumentRelationCollection with selected relations.

Examples:

Select relations with type “obsoletes”

relations.select { |r| r.type == "obsoletes" }
#=> <RelatonBib::DocRelationCollection:0x00007f9a0191d5f0 @array=[...]>


32
33
34
35
# File 'lib/relaton_bib/document_relation_collection.rb', line 32

def select(&block)
  arr = @array.select(&block)
  self.class.new arr
end

#to_asciibib(prefix = "") ⇒ String



47
48
49
50
51
52
53
54
55
# File 'lib/relaton_bib/document_relation_collection.rb', line 47

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