Class: Relaton::Render::Fields
- Inherits:
-
Object
- Object
- Relaton::Render::Fields
- Defined in:
- lib/relaton/render/fields/fields.rb
Instance Method Summary collapse
- #authorciteformat(names) ⇒ Object
- #compound_fields_format(hash) ⇒ Object
- #date_fields_format(hash) ⇒ Object
- #date_range(hash) ⇒ Object
- #dateformat(date, _hash) ⇒ Object
- #draftformat(num, _hash) ⇒ Object
- #edition_fields_format(hash) ⇒ Object
- #edition_translate1(num) ⇒ Object
- #editionformat(edn, num) ⇒ Object
- #extentformat(extent, hash) ⇒ Object
- #extentformat1(key, val, hash, norm_hash) ⇒ Object
-
#initialize(options) ⇒ Fields
constructor
A new instance of Fields.
-
#mediumformat(hash) ⇒ Object
TODO is not being i18n-alised.
- #misc_fields_format(hash) ⇒ Object
- #name_fields_format(hash) ⇒ Object
- #nameformat(names) ⇒ Object
- #pagevolformat(value, value_raw, type, is_size) ⇒ Object
- #place_format(hash) ⇒ Object
- #range(hash) ⇒ Object
- #role_fields_format(hash) ⇒ Object
- #role_inflect(contribs, role) ⇒ Object
- #seriesformat(hash) ⇒ Object
- #sizeformat(size, hash) ⇒ Object
- #sizeformat1(key, val, 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) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/relaton/render/fields/fields.rb', line 96 def (names) return names if names.nil? 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) 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
54 55 56 57 58 59 60 61 |
# File 'lib/relaton/render/fields/fields.rb', line 54 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[0] == :date && hash[:type] != "standard" and hash[k[0]] ||= @r.i18n.get["no_date"] end end |
#date_range(hash) ⇒ Object
210 211 212 213 214 215 |
# File 'lib/relaton/render/fields/fields.rb', line 210 def date_range(hash) if hash[:from] "#{hash[:from]}–#{hash[:to]}" else range(hash) end end |
#dateformat(date, _hash) ⇒ Object
217 218 219 220 221 222 223 224 |
# File 'lib/relaton/render/fields/fields.rb', line 217 def dateformat(date, _hash) return nil if date.nil? i(from to on).each do |k| date[k] = ::Relaton::Render::Date.new(date[k], renderer: @r).render end date_range(date) end |
#draftformat(num, _hash) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/relaton/render/fields/fields.rb', line 136 def draftformat(num, _hash) return nil if num.nil? return nil if num.is_a?(Hash) && num[:status].nil? && num[:iteration].nil? @r.i18n.draft.sub(/%/, num) end |
#edition_fields_format(hash) ⇒ Object
40 41 42 43 |
# File 'lib/relaton/render/fields/fields.rb', line 40 def edition_fields_format(hash) hash[:edition] = editionformat(hash[:edition_raw], hash[:edition_num]) hash[:draft] = draftformat(hash[:draft_raw], hash) end |
#edition_translate1(num) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/relaton/render/fields/fields.rb', line 124 def edition_translate1(num) ruleset = case @r.i18n.edition_ordinal when /%Spellout/ then "SpelloutRules" when /%Ordinal/ then "OrdinalRules" else "Digit" end ruleset == "Digit" and return num.to_s ed = HTMLEntities.new.decode(@r.i18n.edition) @r.i18n.inflect_ordinal(num, @r.i18n.get["inflection"]&.dig(ed) || {}, ruleset) end |
#editionformat(edn, num) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/relaton/render/fields/fields.rb', line 117 def editionformat(edn, num) return edn unless num || /^\d+$/.match?(edn) ret = edition_translate1(num || edn.to_i) @r.edition_ordinal.sub(/%(Spellout|Ordinal)?/, ret) end |
#extentformat(extent, hash) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/relaton/render/fields/fields.rb', line 143 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(ret.merge(type: hash[:type])) end.join("; ") end |
#extentformat1(key, val, hash, norm_hash) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/relaton/render/fields/fields.rb', line 154 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
64 65 66 67 68 69 70 71 |
# File 'lib/relaton/render/fields/fields.rb', line 64 def mediumformat(hash) return nil if hash.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
45 46 47 48 49 50 51 52 |
# File 'lib/relaton/render/fields/fields.rb', line 45 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 |
# 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)] .each do |k| hash[k[0]] = nameformat(hash[k[1]]) end hash[:publisher_abbrev] = hash[:publisher_abbrev_raw]&.join(", ") hash[:authorcite] = (hash[:creators]) place_format(hash) end |
#nameformat(names) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/relaton/render/fields/fields.rb', line 83 def nameformat(names) return names if names.nil? 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
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/relaton/render/fields/fields.rb', line 197 def pagevolformat(value, value_raw, type, is_size) return nil if value.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
29 30 31 32 |
# File 'lib/relaton/render/fields/fields.rb', line 29 def place_format(hash) hash[:place] = nameformat(hash[:place_raw].map { |x| { nonpersonal: x } }) end |
#range(hash) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/relaton/render/fields/fields.rb', line 161 def range(hash) if hash[:on] then hash[:on] elsif hash.has_key?(:from) && hash[:from].nil? then nil elsif hash[:from] hash[:to] ? "#{hash[:from]}–#{hash[:to]}" : hash[:from] else hash end end |
#role_fields_format(hash) ⇒ Object
34 35 36 37 38 |
# File 'lib/relaton/render/fields/fields.rb', line 34 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
109 110 111 112 113 114 115 |
# File 'lib/relaton/render/fields/fields.rb', line 109 def role_inflect(contribs, role) return nil if role.nil? || contribs.size.zero? || %w(author publisher).include?(role) number = contribs.size > 1 ? "pl" : "sg" @r.i18n.get[role][number] || role end |
#seriesformat(hash) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/relaton/render/fields/fields.rb', line 73 def seriesformat(hash) parts = i(series_title series_abbr series_num series_partnumber series_run series_formatted) 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
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/relaton/render/fields/fields.rb', line 170 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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/relaton/render/fields/fields.rb', line 181 def sizeformat1(key, val, hash) case key when "volume" hash[:volume_raw] = val hash[:volume] = pagevolformat(val, nil, "volume", true) when "issue" hash[:issue_raw] = val hash[:issue] = pagevolformat(val, nil, "issue", true) when "page" hash[:page_raw] = val hash[:page] = pagevolformat(val, nil, "page", true) when "data" then hash[:data] = val when "duration" then hash[:duration] = val end end |
#uriformat(uri) ⇒ Object
226 227 228 229 230 |
# File 'lib/relaton/render/fields/fields.rb', line 226 def uriformat(uri) return nil if uri.nil? || uri.empty? "<link target='#{uri}'>#{uri}</link>" end |