Class: RelatonJis::HitCollection

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

Instance Method Summary collapse

Constructor Details

#initialize(text, year = nil, result:) ⇒ HitCollection

Initialize hit collection



10
11
12
13
# File 'lib/relaton_jis/hit_collection.rb', line 10

def initialize(text, year = nil, result:)
  super text, year
  @array = result.map { |h| Hit.create h, self }
end

Instance Method Details

#create_relation(hit) ⇒ Object



65
66
67
68
69
70
# File 'lib/relaton_jis/hit_collection.rb', line 65

def create_relation(hit)
  docid = RelatonBib::DocumentIdentifier.new id: hit.hit[:id], type: "JIS", primary: true
  fref = RelatonBib::FormattedRef.new content: hit.hit[:id]
  bibitem = BibliographicItem.new docid: [docid], formattedref: fref
  RelatonBib::DocumentRelation.new(type: "instanceOf", bibitem: bibitem)
end

#findRelatonJis::BibliographicItem, Array<Strig>

Find hit in collection



20
21
22
23
24
25
26
27
# File 'lib/relaton_jis/hit_collection.rb', line 20

def find
  ref_year = year || ref_parts[:year]
  if ref_year
    find_by_year ref_year
  else
    find_all_years
  end
end

#find_all_partsObject

rubocop:disable Metrics/AbcSize



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

def find_all_parts # rubocop:disable Metrics/AbcSize
  hits = @array.select { |hit| hit.eq? ref_parts, all_parts: true }
  item = hits.min_by { |i| i.id_parts[:part].to_i }.fetch.to_all_parts
  hits.each do |hit|
    next if hit.hit[:id] == item.docidentifier.first.id

    item.relation << create_relation(hit)
  end
  item
end

#find_all_yearsObject

rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/relaton_jis/hit_collection.rb', line 39

def find_all_years # rubocop:disable Metrics/AbcSize
  hits = @array.select { |hit| hit.eq? ref_parts }
  return [] if hits.empty?

  item = hits.max_by { |i| i.id_parts[:year].to_i }.fetch
  item_id = item.docidentifier.first.id
  parent = item.to_most_recent_reference
  hits.each do |hit|
    next if hit.hit[:id] == item_id

    parent.relation << create_relation(hit)
  end
  parent
end

#find_by_year(ref_year) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/relaton_jis/hit_collection.rb', line 29

def find_by_year(ref_year)
  missed_years = []
  @array.each do |hit|
    return hit.fetch if hit.eq? ref_parts, ref_year

    missed_years << hit.id_parts[:year] if hit.eq?(ref_parts)
  end
  missed_years
end

#parse_ref(ref) ⇒ Hash

Parse reference



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/relaton_jis/hit_collection.rb', line 88

def parse_ref(ref)
  %r{
    ^(?<code>\w+\s\w\s?\w+)
    (?:-(?<part>\w+))?
    (?::(?<year>\d{4}))?
    (?:/(?<expl>EXPL(?:ANATION)?)(?:\s(?<expl_num>\d+))?)?
    (?:/(?<amd>AMDENDMENT)(?:\s(?<amd_num>\d+)(?::(?<amd_year>\d{4}))?)?)?
  }x =~ ref
  { code: code, part: part, year: year, expl: expl, expl_num: expl_num,
    amd: amd, amd_num: amd_num, amd_year: amd_year }
end

#ref_partsHash

Return parts of reference



77
78
79
# File 'lib/relaton_jis/hit_collection.rb', line 77

def ref_parts
  @ref_parts ||= parse_ref text
end