Class: RelatonIso::HitCollection

Inherits:
RelatonBib::HitCollection
  • Object
show all
Defined in:
lib/relaton_iso/hit_collection.rb

Overview

Page of hit collection.

Constant Summary collapse

INDEXFILE =
"index-v1.yaml"
ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-iso/main/"

Instance Method Summary collapse

Constructor Details

#initialize(pubid, opts = {}) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • text (Pubid::Iso::Identifier)

    reference to search



12
13
14
15
# File 'lib/relaton_iso/hit_collection.rb', line 12

def initialize(pubid, opts = {})
  super
  @opts = opts
end

Instance Method Details

#create_pubid(id) ⇒ Object



47
48
49
50
51
# File 'lib/relaton_iso/hit_collection.rb', line 47

def create_pubid(id)
  Pubid::Iso::Identifier.create(**id)
rescue StandardError => e
  Util.warn e.message, key: ref_pubid.to_s
end

#create_relation(hit) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/relaton_iso/hit_collection.rb', line 92

def create_relation(hit)
  docid = DocumentIdentifier.new(id: hit.pubid, type: "ISO", primary: true)
  isobib = RelatonIsoBib::IsoBibliographicItem.new(
    formattedref: RelatonBib::FormattedRef.new(content: hit.pubid.to_s), docid: [docid],
  )
  RelatonBib::DocumentRelation.new(type: "instanceOf", bibitem: isobib)
end

#excludingsObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/relaton_iso/hit_collection.rb', line 53

def excludings
  return @excludings if defined? @excludings

  excl_parts = %i[year]
  excl_parts << :part if ref_pubid.root.part.nil? || @opts[:all_parts]
  if ref_pubid.stage.nil? || @opts[:all_parts]
    excl_parts << :stage
    excl_parts << :iteration
  end
  # excl_parts << :edition if ref_pubid.root.edition.nil? || all_parts
  @escludings = excl_parts
end

#fetchObject

rubocop:disable Metrics/AbcSize



28
29
30
31
32
33
34
35
# File 'lib/relaton_iso/hit_collection.rb', line 28

def fetch # rubocop:disable Metrics/AbcSize
  @array = index.search do |row|
    row[:id].is_a?(Hash) ? pubid_match?(row[:id]) : ref_pubid.to_s == row[:id]
  end.map { |row| Hit.new row, self }
    .sort_by! { |h| h.pubid.to_s }
    .reverse!
  self
end

#fetch_docObject



70
71
72
73
74
75
76
# File 'lib/relaton_iso/hit_collection.rb', line 70

def fetch_doc
  if !@opts[:all_parts] || size == 1
    any? && first.fetch(@opts[:lang])
  else
    to_all_parts(@opts[:lang])
  end
end

#indexObject



66
67
68
# File 'lib/relaton_iso/hit_collection.rb', line 66

def index
  @index ||= Relaton::Index.find_or_create :iso, url: "#{ENDPOINT}index-v1.zip", file: INDEXFILE
end

#pubid_match?(id) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/relaton_iso/hit_collection.rb', line 37

def pubid_match?(id)
  pubid = create_pubid(id)
  return false unless pubid

  pubid.base = pubid.base.exclude(:year, :edition) if pubid.base
  dir_excludings = excludings.dup
  dir_excludings << :edition unless pubid.typed_stage_abbrev == "DIR"
  pubid.exclude(*dir_excludings) == ref_pubid_excluded
end

#ref_pubid_excludedObject



24
25
26
# File 'lib/relaton_iso/hit_collection.rb', line 24

def ref_pubid_excluded
  @ref_pubid_excluded ||= ref_pubid_no_year.exclude(*excludings)
end

#ref_pubid_no_yearObject



20
21
22
# File 'lib/relaton_iso/hit_collection.rb', line 20

def ref_pubid_no_year
  @ref_pubid_no_year ||= ref_pubid.dup.tap { |r| r.base = r.base.exclude(:year) if r.base }
end

#to_all_parts(lang = nil) ⇒ RelatonIsoBib::IsoBibliographicItem?

Parameters:

  • lang (String, nil) (defaults to: nil)

Returns:

  • (RelatonIsoBib::IsoBibliographicItem, nil)


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/relaton_iso/hit_collection.rb', line 80

def to_all_parts(lang = nil) # rubocop:disable Metrics/AbcSize
  hit = @array.min_by { |h| h.pubid.part.to_i }
  return @array.first&.fetch lang unless hit

  bibitem = hit.fetch(lang)
  all_parts_item = bibitem.to_all_parts
  @array.reject { |h| h.pubid.part == hit.pubid.part }.each do |hi|
    all_parts_item.relation << create_relation(hi)
  end
  all_parts_item
end