Class: Relaton::Jis::HitCollection

Inherits:
Core::HitCollection
  • Object
show all
Defined in:
lib/relaton/jis/hit_collection.rb

Constant Summary collapse

GH_URL =
"https://raw.githubusercontent.com/relaton/relaton-data-jis/data-v2/"

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 (Array<Hash>)

    search results



17
18
19
20
# File 'lib/relaton/jis/hit_collection.rb', line 17

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



72
73
74
75
76
77
78
79
80
# File 'lib/relaton/jis/hit_collection.rb', line 72

def create_relation(hit)
  docid = Docidentifier.new(
    content: hit.hit[:id], type: "JIS", primary: true,
  )
  bibitem = Bib::ItemData.new(
    formattedref: Bib::Formattedref.new(content: hit.hit[:id]), docidentifier: [docid],
  )
  Bib::Relation.new(type: "instanceOf", bibitem: bibitem)
end

#findRelaton::Bib::ItemData, Array<String>

Find hit in collection

Returns:

  • (Relaton::Bib::ItemData, Array<String>)


27
28
29
30
31
32
33
34
# File 'lib/relaton/jis/hit_collection.rb', line 27

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



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

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 }.item.to_all_parts
  hits.each do |hit|
    next if hit.hit[:id] == item.docidentifier.first.content

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

#find_all_yearsObject

rubocop:disable Metrics/AbcSize



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/relaton/jis/hit_collection.rb', line 46

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 }.item
  item_id = item.docidentifier.first.content
  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



36
37
38
39
40
41
42
43
44
# File 'lib/relaton/jis/hit_collection.rb', line 36

def find_by_year(ref_year)
  missed_years = []
  @array.each do |hit|
    return hit.item 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

Parameters:

  • ref (String)

    reference

Returns:

  • (Hash)

    hash with parts of reference



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/relaton/jis/hit_collection.rb', line 98

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



87
88
89
# File 'lib/relaton/jis/hit_collection.rb', line 87

def ref_parts
  @ref_parts ||= parse_ref ref
end