Class: ModsDisplay::Imprint

Inherits:
Field
  • Object
show all
Includes:
CountryCodes
Defined in:
lib/mods_display/fields/imprint.rb

Instance Method Summary collapse

Methods included from CountryCodes

#country_codes

Methods inherited from Field

#initialize, #label, #to_html

Constructor Details

This class inherits a constructor from ModsDisplay::Field

Instance Method Details

#apply_date_qualifier_decoration(date_fields) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mods_display/fields/imprint.rb', line 104

def apply_date_qualifier_decoration(date_fields)
  return_fields = date_fields.map do |date|
    date = date.clone
    if date_is_approximate?(date)
      date.content = "[ca. #{date.text}]"
    elsif date_is_questionable?(date)
      date.content = "[#{date.text}?]"
    elsif date_is_inferred?(date)
      date.content = "[#{date.text}]"
    end
    date
  end
  return_fields.map(&:text)
end

#date_is_approximate?(date_field) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# File 'lib/mods_display/fields/imprint.rb', line 119

def date_is_approximate?(date_field)
  date_field.attributes['qualifier'] &&
    date_field.attributes['qualifier'].respond_to?(:value) &&
    date_field.attributes['qualifier'].value == 'approximate'
end

#date_is_inferred?(date_field) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
# File 'lib/mods_display/fields/imprint.rb', line 131

def date_is_inferred?(date_field)
  date_field.attributes['qualifier'] &&
    date_field.attributes['qualifier'].respond_to?(:value) &&
    date_field.attributes['qualifier'].value == 'inferred'
end

#date_is_iso8601?(date_field) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/mods_display/fields/imprint.rb', line 163

def date_is_iso8601?(date_field)
  field_is_encoded?(date_field, 'iso8601')
end

#date_is_questionable?(date_field) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/mods_display/fields/imprint.rb', line 125

def date_is_questionable?(date_field)
  date_field.attributes['qualifier'] &&
    date_field.attributes['qualifier'].respond_to?(:value) &&
    date_field.attributes['qualifier'].value == 'questionable'
end

#date_is_w3cdtf?(date_field) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/mods_display/fields/imprint.rb', line 159

def date_is_w3cdtf?(date_field)
  field_is_encoded?(date_field, 'w3cdtf')
end

#dates(element) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mods_display/fields/imprint.rb', line 37

def dates(element)
  date_field_keys.map do |date_field|
    next unless element.respond_to?(date_field)
    elements = element.send(date_field)
    next if elements.empty?
    ModsDisplay::Values.new(
      label: displayLabel(element) || pub_info_labels[elements.first.name.to_sym],
      values: parse_dates(elements)
    )
  end.compact
end

#dates_are_open_range?(date_fields) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
147
# File 'lib/mods_display/fields/imprint.rb', line 137

def dates_are_open_range?(date_fields)
  date_fields.any? do |field|
    field.attributes['point'] &&
      field.attributes['point'].respond_to?(:value) &&
      field.attributes['point'].value == 'start'
  end && !date_fields.any? do |field|
    field.attributes['point'] &&
      field.attributes['point'].respond_to?(:value) &&
      field.attributes['point'].value == 'end'
  end
end

#dates_are_range?(date_fields) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
# File 'lib/mods_display/fields/imprint.rb', line 149

def dates_are_range?(date_fields)
  attributes = date_fields.map do |date|
    if date.attributes['point'].respond_to?(:value)
      date.attributes['point'].value
    end
  end
  attributes.include?('start') &&
    attributes.include?('end')
end

#dedup_dates(date_fields) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/mods_display/fields/imprint.rb', line 193

def dedup_dates(date_fields)
  date_text = date_fields.map { |d| normalize_date(d.text) }
  if date_text != date_text.uniq
    if date_fields.find { |d| d.attributes['qualifier'].respond_to?(:value) }
      [date_fields.find { |d| d.attributes['qualifier'].respond_to?(:value) }]
    elsif date_fields.find { |d| !d.attributes['encoding'] }
      [date_fields.find { |d| !d.attributes['encoding'] }]
    else
      [date_fields.first]
    end
  else
    date_fields
  end
end

#fieldsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mods_display/fields/imprint.rb', line 4

def fields
  return_fields = []
  @values.each do |value|
    if imprint_display_form(value)
      return_fields << imprint_display_form(value)
    else
      edition = edition_element(value)
      place = place_element(value)
      publisher = publisher_element(value)
      parts = parts_element(value)
      place_pub = compact_and_join_with_delimiter([place, publisher], ' : ')
      edition_place = compact_and_join_with_delimiter([edition, place_pub], ' - ')
      joined_place_parts = compact_and_join_with_delimiter([edition_place, parts], ', ')
      unless joined_place_parts.empty?
        return_fields << ModsDisplay::Values.new(
          label: displayLabel(value) || I18n.t('mods_display.imprint'),
          values: [joined_place_parts]
        )
      end
      return_fields.concat(dates(value)) if dates(value).length > 0
      if other_pub_info(value).length > 0
        other_pub_info(value).each do |pub_info|
          return_fields << ModsDisplay::Values.new(
            label: displayLabel(value) || pub_info_labels[pub_info.name.to_sym],
            values: [pub_info.text.strip]
          )
        end
      end
    end
  end
  collapse_fields(return_fields)
end

#ignore_bad_dates(date_fields) ⇒ Object



61
62
63
64
65
# File 'lib/mods_display/fields/imprint.rb', line 61

def ignore_bad_dates(date_fields)
  date_fields.select do |date_field|
    date_field.text.strip != '9999'
  end
end

#imprint_display_form(element) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/mods_display/fields/imprint.rb', line 247

def imprint_display_form(element)
  display_form = element.children.find do |child|
    child.name == 'displayForm'
  end
  ModsDisplay::Values.new(
    label: displayLabel(element) || I18n.t('mods_display.imprint'),
    values: [display_form.text]
  ) if display_form
end

#join_date_ranges(date_fields) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mods_display/fields/imprint.rb', line 79

def join_date_ranges(date_fields)
  if dates_are_range?(date_fields)
    start_date = date_fields.find { |d| d.attributes['point'] && d.attributes['point'].value == 'start' }
    end_date = date_fields.find { |d| d.attributes['point'] && d.attributes['point'].value == 'end' }
    date_fields.map do |date|
      date = date.clone # clone the date object so we don't append the same one
      if normalize_date(date.text) == normalize_date(start_date.text)
        date.content = [start_date.text, end_date.text].join('-')
        date
      elsif normalize_date(date.text) != normalize_date(end_date.text)
        date
      end
    end.compact
  elsif dates_are_open_range?(date_fields)
    start_date = date_fields.find { |d| d.attributes['point'] && d.attributes['point'].value == 'start' }
    date_fields.map do |date|
      date = date.clone # clone the date object so we don't append the same one
      date.content = "#{start_date.text}-" if date.text == start_date.text
      date
    end
  else
    date_fields
  end
end

#normalize_date(date) ⇒ Object



208
209
210
# File 'lib/mods_display/fields/imprint.rb', line 208

def normalize_date(date)
  date.strip.gsub(/^\[*ca\.\s*|c|\[|\]|\?/, '')
end

#other_pub_info(element) ⇒ Object



212
213
214
215
216
# File 'lib/mods_display/fields/imprint.rb', line 212

def other_pub_info(element)
  element.children.select do |child|
    pub_info_parts.include?(child.name.to_sym)
  end
end

#parse_dates(date_field) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mods_display/fields/imprint.rb', line 49

def parse_dates(date_field)
  apply_date_qualifier_decoration(
    dedup_dates(
      join_date_ranges(
        process_bc_ad_dates(
          process_encoded_dates(ignore_bad_dates(date_field))
        )
      )
    )
  )
end

#place_terms(element) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/mods_display/fields/imprint.rb', line 218

def place_terms(element)
  return [] unless element.respond_to?(:place) &&
                   element.place.respond_to?(:placeTerm)
  if unencoded_place_terms?(element)
    element.place.placeTerm.select do |term|
      !term.attributes['type'].respond_to?(:value) ||
        term.attributes['type'].value == 'text'
    end.compact
  else
    element.place.placeTerm.map do |term|
      next unless term.attributes['type'].respond_to?(:value) &&
                  term.attributes['type'].value == 'code' &&
                  term.attributes['authority'].respond_to?(:value) &&
                  term.attributes['authority'].value == 'marccountry' &&
                  country_codes.include?(term.text.strip)
      term = term.clone
      term.content = country_codes[term.text.strip]
      term
    end.compact
  end
end

#process_encoded_dates(date_fields) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mods_display/fields/imprint.rb', line 67

def process_encoded_dates(date_fields)
  date_fields.map do |date_field|
    if date_is_w3cdtf?(date_field)
      process_w3cdtf_date(date_field)
    elsif date_is_iso8601?(date_field)
      process_iso8601_date(date_field)
    else
      date_field
    end
  end
end

#process_iso8601_date(date_field) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/mods_display/fields/imprint.rb', line 183

def process_iso8601_date(date_field)
  date_field = date_field.clone
  date_field.content = begin
    Date.iso8601(date_field.text).strftime(@config.full_date_format)
  rescue
    date_field.content
  end
  date_field
end

#process_w3cdtf_date(date_field) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/mods_display/fields/imprint.rb', line 167

def process_w3cdtf_date(date_field)
  date_field = date_field.clone
  date_field.content = begin
    if date_field.text.strip =~ /^\d{4}-\d{2}-\d{2}$/
      Date.parse(date_field.text).strftime(@config.full_date_format)
    elsif date_field.text.strip =~ /^\d{4}-\d{2}$/
      Date.parse("#{date_field.text}-01").strftime(@config.short_date_format)
    else
      date_field.content
    end
  rescue
    date_field.content
  end
  date_field
end

#unencoded_place_terms?(element) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
243
244
245
# File 'lib/mods_display/fields/imprint.rb', line 240

def unencoded_place_terms?(element)
  element.place.placeTerm.any? do |term|
    !term.attributes['type'].respond_to?(:value) ||
      term.attributes['type'].value == 'text'
  end
end