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

Parameters:

  • text (String)

    reference

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

    year

  • result (Nokogiri::XML::NodeSet)

    <description>



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

#findRelatonJis::BibliographicItem, Array<Strig>

Find hit in collection

Returns:



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

def find
  missed_years = []
  y = year || ref_parts[:year]
  @array.each do |hit|
    return hit.fetch if hit.match? ref_parts, y

    missed_years << hit.id_parts[:year] if y && hit.match?(ref_parts)
  end
  missed_years
end

#find_all_partsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/relaton_jis/hit_collection.rb', line 31

def find_all_parts
  hits = @array.select { |hit| hit.match? ref_parts, year, 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

    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
    item.relation << RelatonBib::DocumentRelation.new(type: "instance", bibitem: bibitem)
  end
  item
end

#parse_ref(ref) ⇒ Hash

Parse reference

Parameters:

  • ref (String)

    reference

Returns:

  • (Hash)

    hash with parts of reference



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/relaton_jis/hit_collection.rb', line 61

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

Returns:

  • (Hash)

    hash with parts of reference



50
51
52
# File 'lib/relaton_jis/hit_collection.rb', line 50

def ref_parts
  @ref_parts ||= parse_ref text
end