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

#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



50
51
52
53
54
55
# File 'lib/relaton/render/fields/fields.rb', line 50

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)
  end
end

#date_range(hash) ⇒ Object



191
192
193
194
195
196
# File 'lib/relaton/render/fields/fields.rb', line 191

def date_range(hash)
  if hash[:from]
    "#{hash[:from]}–#{hash[:to]}"
  else range(hash)
  end
end

#dateformat(date, _hash) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/relaton/render/fields/fields.rb', line 198

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



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

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



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

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



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

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



98
99
100
101
102
103
# File 'lib/relaton/render/fields/fields.rb', line 98

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



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

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



135
136
137
138
139
140
# File 'lib/relaton/render/fields/fields.rb', line 135

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



58
59
60
61
62
63
64
65
# File 'lib/relaton/render/fields/fields.rb', line 58

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



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

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)
  hash[:place] = nameformat(hash[:place_raw]&.map do |x|
                              { nonpersonal: x }
                            end)
  [%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(", ")
end

#nameformat(names) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/relaton/render/fields/fields.rb', line 77

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



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

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

#range(hash) ⇒ Object



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

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]}&#x2013;#{hash[:to]}" : hash[:from]
  else hash
  end
end

#role_fields_format(hash) ⇒ Object



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

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



90
91
92
93
94
95
96
# File 'lib/relaton/render/fields/fields.rb', line 90

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



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

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



151
152
153
154
155
156
157
158
159
160
# File 'lib/relaton/render/fields/fields.rb', line 151

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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/relaton/render/fields/fields.rb', line 162

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



207
208
209
210
211
# File 'lib/relaton/render/fields/fields.rb', line 207

def uriformat(uri)
  return nil if uri.nil? || uri.empty?

  "<link target='#{uri}'>#{uri}</link>"
end