Class: Relaton::Render::Citations

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/render/general/citations.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Citations

Returns a new instance of Citations.



4
5
6
7
8
# File 'lib/relaton/render/general/citations.rb', line 4

def initialize(opt = {})
  @type = opt[:type]
  @i18n = opt[:i18n]
  @renderer = opt[:renderer]
end

Instance Method Details

#add_date_accessed(data, uri, status) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/relaton/render/general/citations.rb', line 49

def add_date_accessed(data, uri, status)
  if status
    data[:data_liquid][:date_accessed] = { on: ::Date.today.to_s }
    data[:data_liquid] = @renderer.fieldsklass.new(renderer: @renderer)
      .compound_fields_format(data[:data_liquid])
  else
    @renderer.url_warn(uri)
  end
end

#breakdown(ret) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/relaton/render/general/citations.rb', line 88

def breakdown(ret)
  ret.each_with_object({}) do |b, m|
    m[b[:author]] ||= {}
    m[b[:author]][b[:date]] ||= []
    m[b[:author]][b[:date]] << b
  end
end

#citations(ret) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/relaton/render/general/citations.rb', line 68

def citations(ret)
  case @type
  when "author-date" then disambig_author_date_citations(ret)
  when nil then generic_citation(ret)
  else raise "Unimplemented citation type"
  end
end

#disambig_author_date_citations(ret) ⇒ Object

takes array of { id, type, author, date, ord, data_liquid }



84
85
86
# File 'lib/relaton/render/general/citations.rb', line 84

def disambig_author_date_citations(ret)
  to_hash(suffix_date(sort_ord(breakdown(ret))))
end

#enhance_data(cites) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/relaton/render/general/citations.rb', line 23

def enhance_data(cites)
  ret = extract_uris_for_lookup(cites)
  ret.empty? and return
  @renderer.urls_exist_concurrent(ret.keys).each do |k, v|
    ret[k].each { |u| add_date_accessed(cites[u], k, v) }
  end
end

#extract_uri_for_lookup(cite) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/relaton/render/general/citations.rb', line 39

def extract_uri_for_lookup(cite)
  t = cite[:renderer].template_raw
  c = cite[:data_liquid]
  t.is_a?(String) or return
  (/\{\{\s*date_accessed\s*\}\}/.match?(t) &&
  /\{\{\s*uri\s*\}\}/.match?(t) &&
  c[:uri_raw] && !c[:date_accessed]) or return
  c[:uri_raw]
end

#extract_uris_for_lookup(cites) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/relaton/render/general/citations.rb', line 31

def extract_uris_for_lookup(cites)
  cites.each_with_object({}) do |(k, v), m|
    u = extract_uri_for_lookup(v) or next
    m[u] ||= []
    m[u] << k
  end
end

#generic_citation(ret) ⇒ Object



76
77
78
79
80
81
# File 'lib/relaton/render/general/citations.rb', line 76

def generic_citation(ret)
  ret.each_with_object({}) do |b, m|
    m[b[:id]] = { data_liquid: b[:data_liquid], type: b[:type],
                  citation: b[:data_liquid][:docidentifier] }
  end
end

#render(ret) ⇒ Object

takes array of { id, type, author, date, ord, data_liquid }



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/relaton/render/general/citations.rb', line 11

def render(ret)
  cites = citations(ret)
  cites.each_value do |v|
    v[:renderer] = @renderer.renderer(v[:type] || "misc")
  end
  enhance_data(cites)
  cites.each_key do |k|
    cites[k] = render1(cites[k])
  end
  cites
end

#render1(cit) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/relaton/render/general/citations.rb', line 59

def render1(cit)
  cit[:formattedref] =
    @renderer.valid_parse(
      @i18n.l10n(cit[:renderer].render(cit[:data_liquid])),
    )
  %i(type data_liquid renderer).each { |x| cit.delete(x) }
  cit
end

#sort_ord(ret) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/relaton/render/general/citations.rb', line 96

def sort_ord(ret)
  ret.each do |author, v|
    v.each_key do |date|
      ret[author][date].sort_by! { |a| a[:ord] }
    end
  end
end

#suffix_date(ret) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/relaton/render/general/citations.rb', line 104

def suffix_date(ret)
  ret.each do |k, v|
    v.each do |k1, v1|
      next if v1.reject { |b| b[:date].nil? }.size < 2

      suffix_date1(ret, k, k1)
    end
  end
  ret
end

#suffix_date1(ret, key1, key2) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/relaton/render/general/citations.rb', line 115

def suffix_date1(ret, key1, key2)
  ret[key1][key2].each_with_index do |b, i|
    next if b[:date].nil?

    b[:date] += ("a".ord + i).chr.to_s
    b[:data_liquid][:date] = b[:date]
  end
end

#to_hash(ret) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/relaton/render/general/citations.rb', line 124

def to_hash(ret)
  ret.each_with_object({}) do |(_k, v), m|
    v.each_value do |v1|
      v1.each do |b|
        m[b[:id]] = { author: b[:author], date: b[:date],
                      citation: "#{b[:author]} #{b[:date]}",
                      data_liquid: b[:data_liquid], type: b[:type] }
      end
    end
  end
end