Class: Relaton::Render::Fields

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Fields

Returns a new instance of Fields.



6
7
8
# File 'lib/relaton/render/fields/fields.rb', line 6

def initialize(options)
  @r = options[:renderer]
end

Instance Method Details

#authorciteformat(names) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/relaton/render/fields/fields.rb', line 96

def authorciteformat(names)
  names.nil? || @r.authorcitetemplate.nil? and return names
  parts = %i(surname initials given middle nonpersonal)
  names_out = names.each_with_object({}) do |n, m|
    parts.each do |i|
      m[i] ||= []
      m[i] << n[i]
    end
  end
  @r.authorcitetemplate.render(names_out)
end

#compound_fields_format(hash) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/relaton/render/fields/fields.rb', line 10

def compound_fields_format(hash)
  name_fields_format(hash)
  role_fields_format(hash)
  date_fields_format(hash)
  edition_fields_format(hash)
  misc_fields_format(hash)
end

#date_fields_format(hash) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/relaton/render/fields/fields.rb', line 55

def date_fields_format(hash)
  [%i(date date), %i(date_updated date_updated),
   %i(date_accessed date_accessed)].each do |k|
    hash[k[0]] = dateformat(hash[k[1]], hash, k)
    k[0] == :date &&
      !%w(standard webresource website).include?(hash[:type]) and
      hash[k[0]] ||= @r.i18n.get["no_date"]
  end
end

#date_range(hash) ⇒ Object



190
191
192
193
# File 'lib/relaton/render/fields/fields.rb', line 190

def date_range(hash)
  hash[:from] && !hash[:to] and return "#{hash[:from]}&#x2013;"
  range(hash)
end

#dateformat(date, hash, type) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/relaton/render/fields/fields.rb', line 195

def dateformat(date, hash, type)
  date.nil? and return nil
  date.is_a?(String) and return date
  %i(from to on).each do |k|
    date[k] = @r.dateklass
      .new(date[k], renderer: @r, bibitem: hash, type: type).render
  end
  date_range(date)
end

#draftformat(num, _hash) ⇒ Object



123
124
125
126
127
128
# File 'lib/relaton/render/fields/fields.rb', line 123

def draftformat(num, _hash)
  num.nil? ||
    (num.is_a?(Hash) && num[:status].nil? &&
      num[:iteration].nil?) and return nil
  @r.i18n.draft.sub("%", num)
end

#edition_fields_format(hash) ⇒ Object



41
42
43
44
# File 'lib/relaton/render/fields/fields.rb', line 41

def edition_fields_format(hash)
  hash[:edition] = editionformat(hash[:edition_raw], hash[:edition_num])
  hash[:draft] = draftformat(hash[:draft_raw], hash)
end

#editionformat(edn, num) ⇒ Object



118
119
120
121
# File 'lib/relaton/render/fields/fields.rb', line 118

def editionformat(edn, num)
  num || /^\d+$/.match?(edn) or return edn
  @r.i18n.populate("edition_ordinal", { "var1" => num || edn.to_i })
end

#extentformat(extent, hash) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/relaton/render/fields/fields.rb', line 130

def extentformat(extent, hash)
  extent.map do |e|
    e1 = e.transform_values { |v| v.is_a?(Hash) ? range(v) : v }
    ret = e.each_with_object({}) do |(k, v), m|
      extentformat1(k, v, m, e1)
      m
    end
    @r.extenttemplate.render(hash.merge(ret))
  end.join("; ")
end

#extentformat1(key, val, hash, norm_hash) ⇒ Object



141
142
143
144
145
146
# File 'lib/relaton/render/fields/fields.rb', line 141

def extentformat1(key, val, hash, norm_hash)
  if %i(volume issue page).include?(key)
    hash["#{key}_raw".to_sym] = norm_hash[key]
    hash[key] = pagevolformat(norm_hash[key], val, key.to_s, false)
  end
end

#mediumformat(hash) ⇒ Object

TODO is not being i18n-alised



66
67
68
69
70
71
72
# File 'lib/relaton/render/fields/fields.rb', line 66

def mediumformat(hash)
  hash.nil? and return nil
  %w(content genre form carrier size scale).each_with_object([]) do |i, m|
    m << hash[i] if hash[i]
    m
  end.compact.join(", ")
end

#misc_fields_format(hash) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/relaton/render/fields/fields.rb', line 46

def misc_fields_format(hash)
  hash[:series] = seriesformat(hash)
  hash[:medium] = mediumformat(hash[:medium_raw])
  hash[:extent] = extentformat(hash[:extent_raw], hash)
  hash[:size] = sizeformat(hash[:size_raw], hash)
  hash[:uri] = uriformat(hash[:uri_raw])
  hash
end

#name_fields_format(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/relaton/render/fields/fields.rb', line 18

def name_fields_format(hash)
  [%i(creatornames creators), %i(host_creatornames host_creators),
   %i(publisher publisher_raw), %i(distributor distributor_raw),
   %i(authorizer authorizer_raw)]
    .each do |k|
    hash[k[0]] = nameformat(hash[k[1]])
  end
  hash[:publisher_abbrev] = hash[:publisher_abbrev_raw]&.join(", ")
  hash[:authorcite] = authorciteformat(hash[:creators])
  place_format(hash)
end

#nameformat(names) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/relaton/render/fields/fields.rb', line 84

def nameformat(names)
  names.nil? and return names
  parts = %i(surname initials given middle nonpersonal)
  names_out = names.each_with_object({}) do |n, m|
    parts.each do |i|
      m[i] ||= []
      m[i] << n[i]
    end
  end
  @r.nametemplate.render(names_out)
end

#pagevolformat(value, value_raw, type, is_size) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/relaton/render/fields/fields.rb', line 178

def pagevolformat(value, value_raw, type, is_size)
  value.nil? and return nil
  num = "pl"
  if is_size
    value == "1" and num = "sg"
  else
    value_raw[:to] or num = "sg"
  end
  @r.i18n.l10n(@r.i18n.get[is_size ? "size" : "extent"][type][num]
    .sub("%", value))
end

#place_format(hash) ⇒ Object



30
31
32
33
# File 'lib/relaton/render/fields/fields.rb', line 30

def place_format(hash)
  hash[:place] =
    nameformat(hash[:place_raw].map { |x| { nonpersonal: x } })
end

#range(hash) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/relaton/render/fields/fields.rb', line 148

def range(hash)
  hash[:on] and return hash[:on]
  hash.has_key?(:from) && hash[:from].nil? and return nil
  !hash[:from] and return hash
  hash[:to] && hash[:to] != hash[:from] and
    return "#{hash[:from]}&#x2013;#{hash[:to]}"
  hash[:from]
end

#role_fields_format(hash) ⇒ Object



35
36
37
38
39
# File 'lib/relaton/render/fields/fields.rb', line 35

def role_fields_format(hash)
  hash[:role] = role_inflect(hash[:creators], hash[:role_raw])
  hash[:host_role] =
    role_inflect(hash[:host_creators], hash[:host_role_raw])
end

#role_inflect(contribs, role) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/relaton/render/fields/fields.rb', line 108

def role_inflect(contribs, role)
  role.nil? || contribs.empty? ||
    %w(author publisher distributor
       authorizer).include?(role) and return nil
  number = contribs.size > 1 ? "pl" : "sg"
  x = @r.i18n.get[role]
  x.is_a?(Hash) or return role
  x[number] || role
end

#seriesformat(hash) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/relaton/render/fields/fields.rb', line 74

def seriesformat(hash)
  parts = %i(series_title series_abbr series_num series_partnumber
             series_run series_formatted series_dates series_place series_org)
  series_out = parts.each_with_object({}) do |i, m|
    m[i] = hash[i]
  end
  t = hash[:type] == "article" ? @r.journaltemplate : @r.seriestemplate
  t.render(series_out)
end

#sizeformat(size, hash) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/relaton/render/fields/fields.rb', line 157

def sizeformat(size, hash)
  return nil unless size

  ret = size.transform_values { |v| @r.i18n.l10n(v.join(" + ")) }
    .each_with_object({}) do |(k, v), m|
      sizeformat1(k, v, m)
      m
    end
  @r.sizetemplate.render(ret.merge(type: hash[:type]))
end

#sizeformat1(key, val, hash) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/relaton/render/fields/fields.rb', line 168

def sizeformat1(key, val, hash)
  case key
  when "volume", "issue", "page"
    hash["#{key}_raw".to_sym] = val
    hash[key.to_sym] = pagevolformat(val, nil, key, true)
  when "data" then hash[:data] = val
  when "duration" then hash[:duration] = val
  end
end

#uriformat(uri) ⇒ Object



205
206
207
208
# File 'lib/relaton/render/fields/fields.rb', line 205

def uriformat(uri)
  uri.nil? || uri.empty? and return nil
  "<link target='#{uri}'>#{uri}</link>"
end