Class: Relaton::Render::Fields
- Inherits:
-
Object
- Object
- Relaton::Render::Fields
- Defined in:
- lib/relaton/render/fields/fields.rb
Instance Method Summary collapse
- #authorciteformat(names, hash) ⇒ Object
- #comma(hash) ⇒ Object
-
#compound_fields_format(hash) ⇒ Object
the @i18n.select choices here need to rely on fields already present, they can’t rely on fields they are yet to build.
-
#date_fields_format(hash) ⇒ Object
date => disambiguated_date, the disambiguation happens in citations/.
- #date_range(hash) ⇒ Object
- #dateformat(date, hash, type) ⇒ Object
- #draftformat(num, hash) ⇒ Object
- #edition_fields_format(hash) ⇒ Object
- #editionformat(edn, num, hash) ⇒ Object
- #extentformat(extent, hash) ⇒ Object
- #extentformat1(key, val, hash, norm_hash, context) ⇒ Object
-
#initialize(options) ⇒ Fields
constructor
A new instance of Fields.
- #mediumformat(medium, hash) ⇒ Object
- #misc_fields_format(hash) ⇒ Object
- #name_fields_format(hash) ⇒ Object
- #nameformat(names, hash) ⇒ Object
- #otheridentifierformat(otherids, _hash) ⇒ Object
- #pagevolformat(value, value_raw, type, is_size, hash) ⇒ Object
- #place_fields_format(hash) ⇒ Object
- #range(hash) ⇒ Object
- #role_fields_format(hash) ⇒ Object
- #role_inflect(contribs, role, hash) ⇒ Object
- #series_fields_format(hash) ⇒ Object
- #sizeformat(size, hash) ⇒ Object
- #sizeformat1(key, val, hash, context) ⇒ Object
- #statusformat(status, hash) ⇒ Object
- #uriformat(uri) ⇒ Object
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() @r = [:renderer] end |
Instance Method Details
#authorciteformat(names, hash) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/relaton/render/fields/fields.rb', line 131 def (names, hash) names.nil? || @r..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..render(names_out, hash) end |
#comma(hash) ⇒ Object
22 23 24 |
# File 'lib/relaton/render/fields/fields.rb', line 22 def comma(hash) "#{@r.i18n.select(hash).get.dig('punct', 'comma') || ','} " end |
#compound_fields_format(hash) ⇒ Object
the @i18n.select choices here need to rely on fields already present, they can’t rely on fields they are yet to build
12 13 14 15 16 17 18 19 20 |
# File 'lib/relaton/render/fields/fields.rb', line 12 def compound_fields_format(hash) name_fields_format(hash) role_fields_format(hash) date_fields_format(hash) edition_fields_format(hash) place_fields_format(hash) series_fields_format(hash) misc_fields_format(hash) end |
#date_fields_format(hash) ⇒ Object
date => disambiguated_date, the disambiguation happens in citations/
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/relaton/render/fields/fields.rb', line 86 def date_fields_format(hash) [%i(date date), %i(disambiguated_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[1] == :date && !%w(standard webresource website).include?(hash[:type]) and hash[k[0]] ||= @r.i18n.select(nil).get["no_date"] end end |
#date_range(hash) ⇒ Object
235 236 237 238 |
# File 'lib/relaton/render/fields/fields.rb', line 235 def date_range(hash) hash[:from] && !hash[:to] and return "#{hash[:from]}–" range(hash) end |
#dateformat(date, hash, type) ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'lib/relaton/render/fields/fields.rb', line 240 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
162 163 164 165 166 167 |
# File 'lib/relaton/render/fields/fields.rb', line 162 def draftformat(num, hash) num.nil? || (num.is_a?(Hash) && num[:status].nil? && num[:iteration].nil?) and return nil @r.i18n.select(hash).draft.sub("%", num) end |
#edition_fields_format(hash) ⇒ Object
55 56 57 58 59 |
# File 'lib/relaton/render/fields/fields.rb', line 55 def edition_fields_format(hash) hash[:edition] = editionformat(hash[:edition_raw], hash[:edition_num], hash) hash[:draft] = draftformat(hash[:draft_raw], hash) end |
#editionformat(edn, num, hash) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/relaton/render/fields/fields.rb', line 153 def editionformat(edn, num, hash) num || edn && !edn.empty? or return edn_num = edn&.gsub(/<\/?esc>/, "") num || /^\d+$/.match?(edn_num) and return @r.i18n.select(hash) .populate("edition_ordinal", { "var1" => num || edn_num.to_i }) @r.i18n.select(hash).populate("edition_cardinal", { "var1" => edn }) end |
#extentformat(extent, hash) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/relaton/render/fields/fields.rb', line 169 def extentformat(extent, hash) extent.map do |stack| stack.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, hash) m end @r.extenttemplate.render(hash.merge(ret), hash) end.join(" ") end.join("; ") end |
#extentformat1(key, val, hash, norm_hash, context) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/relaton/render/fields/fields.rb', line 182 def extentformat1(key, val, hash, norm_hash, context) 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, context) end end |
#mediumformat(medium, hash) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/relaton/render/fields/fields.rb', line 97 def mediumformat(medium, hash) medium.nil? and return nil ret = %w(content genre form carrier size scale).each_with_object([]) do |i, m| m << "<esc>#{medium[i]}</esc>" if medium[i] m end.compact i = @r.i18n.select(hash) i.l10n(ret.join(comma(hash))) end |
#misc_fields_format(hash) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/relaton/render/fields/fields.rb', line 61 def misc_fields_format(hash) hash[:medium] = mediumformat(hash[:medium_raw], hash) hash[:extent] = extentformat(hash[:extent_raw], hash) hash[:size] = sizeformat(hash[:size_raw], hash) hash[:uri] = uriformat(hash[:uri_raw]) hash[:status] = statusformat(hash[:status_raw], hash) hash[:other_identifier] = otheridentifierformat(hash[:other_identifier_raw], hash) hash end |
#name_fields_format(hash) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/relaton/render/fields/fields.rb', line 38 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]], hash) end hash[:publisher_abbrev] = hash[:publisher_abbrev_raw]&.join(comma(hash)) hash[:authorcite] = (hash[:creators], hash) end |
#nameformat(names, hash) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/relaton/render/fields/fields.rb', line 119 def nameformat(names, hash) 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, hash) end |
#otheridentifierformat(otherids, _hash) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/relaton/render/fields/fields.rb', line 72 def otheridentifierformat(otherids, _hash) otherids&.map do |i| # ret = "#{i[0]}: <esc>#{i[1]}</esc>" # @r.i18n.select(hash).l10n(ret) # no i18n! "#{i[0]}: #{i[1]}" end end |
#pagevolformat(value, value_raw, type, is_size, hash) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/relaton/render/fields/fields.rb', line 222 def pagevolformat(value, value_raw, type, is_size, hash) value.nil? and return nil num = "pl" if is_size value == "1" and num = "sg" else value_raw[:to] or num = "sg" end i = @r.i18n.select(hash) i.l10n(i.get[is_size ? "size" : "extent"][type][num] .sub("%", value)) end |
#place_fields_format(hash) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/relaton/render/fields/fields.rb', line 26 def place_fields_format(hash) hash[:place_raw]&.map! do |p| # TODO use enum-comma? if p.is_a?(Array) p.join(comma(hash)) else p end end hash[:place] = nameformat(hash[:place_raw].map { |x| { nonpersonal: x } }, hash) end |
#range(hash) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/relaton/render/fields/fields.rb', line 190 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]}–#{hash[:to]}" hash[:from] end |
#role_fields_format(hash) ⇒ Object
49 50 51 52 53 |
# File 'lib/relaton/render/fields/fields.rb', line 49 def role_fields_format(hash) hash[:role] = role_inflect(hash[:creators], hash[:role_raw], hash) hash[:host_role] = role_inflect(hash[:host_creators], hash[:host_role_raw], hash) end |
#role_inflect(contribs, role, hash) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/relaton/render/fields/fields.rb', line 143 def role_inflect(contribs, role, hash) role.nil? || contribs.empty? || %w(author publisher distributor authorizer).include?(role) and return nil number = contribs.size > 1 ? "pl" : "sg" x = @r.i18n.select(hash).get[role] x.is_a?(Hash) or return role x[number] || role end |
#series_fields_format(hash) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/relaton/render/fields/fields.rb', line 108 def series_fields_format(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 hash[:series] = t.render(series_out, hash) end |
#sizeformat(size, hash) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/relaton/render/fields/fields.rb', line 199 def sizeformat(size, hash) return nil unless size ret = size.transform_values do |v| @r.i18n.select(hash).l10n(v.join(" + ")) end .each_with_object({}) do |(k, v), m| sizeformat1(k, v, m, hash) m end @r.sizetemplate.render(ret.merge(type: hash[:type]), hash) end |
#sizeformat1(key, val, hash, context) ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'lib/relaton/render/fields/fields.rb', line 212 def sizeformat1(key, val, hash, context) case key when "volume", "issue", "page" hash["#{key}_raw".to_sym] = val hash[key.to_sym] = pagevolformat(val, nil, key, true, context) when "data" then hash[:data] = val when "duration" then hash[:duration] = val end end |
#statusformat(status, hash) ⇒ Object
80 81 82 83 |
# File 'lib/relaton/render/fields/fields.rb', line 80 def statusformat(status, hash) status.nil? and return @r.i18n.select(hash).get.dig("stage", status) || status end |
#uriformat(uri) ⇒ Object
250 251 252 253 254 |
# File 'lib/relaton/render/fields/fields.rb', line 250 def uriformat(uri) uri.nil? || uri.empty? and return nil # do not process uri contents in l10n "<fmt-link target='#{uri}'><esc>#{uri}</esc></fmt-link>" end |