Class: ModsDisplay::Imprint

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

Defined Under Namespace

Classes: DateRange, DateValue

Instance Method Summary collapse

Methods included from CountryCodes

#country_codes

Methods inherited from Field

#initialize, #label, #render_in, #to_html

Constructor Details

This class inherits a constructor from ModsDisplay::Field

Instance Method Details

#date_values(element) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mods_display/fields/imprint.rb', line 42

def date_values(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

#fieldsObject



7
8
9
# File 'lib/mods_display/fields/imprint.rb', line 7

def fields
  collapse_fields(origin_info_data.flatten)
end

#origin_info_dataObject



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
36
37
38
39
40
# File 'lib/mods_display/fields/imprint.rb', line 11

def origin_info_data
  @values.map do |value|
    return_fields = []

    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(date_values(value))

    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: [element_text(pub_info)]
      )
    end

    return_fields.compact
  end
end

#other_pub_info(element) ⇒ Object



199
200
201
202
203
# File 'lib/mods_display/fields/imprint.rb', line 199

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

#parse_dates(elements) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mods_display/fields/imprint.rb', line 162

def parse_dates(elements)
  # convert to DateValue objects and keep only valid ones
  dates = elements.map(&:as_object).flatten.map { |element| DateValue.new(element) }.select(&:valid?)

  # join any date ranges into DateRange objects
  point, nonpoint = dates.partition(&:point)
  if point.any?
    range = DateRange.new(start: point.find { |date| date.point == 'start' },
                          stop: point.find { |date| date.point == 'end' })
    nonpoint.unshift(range)
  end
  dates = nonpoint

  # ensure dates are unique with respect to their base values
  dates = dates.group_by(&:base_value).map do |_value, group|
    group.first if group.one?

    # if one of the duplicates wasn't encoded, use that one. see:
    # https://consul.stanford.edu/display/chimera/MODS+display+rules#MODSdisplayrules-3b.%3CoriginInfo%3E
    if group.reject(&:encoding).any?
      group.reject(&:encoding).first

    # otherwise just randomly pick the last in the group
    else
      group.last
    end
  end

  # if any single dates are already part of a range, discard them
  range_base_values = dates.select { |date| date.is_a?(DateRange) }
                           .map(&:base_values).flatten
  dates = dates.reject { |date| range_base_values.include?(date.base_value) }

  # output formatted dates with qualifiers, A.D./B.C., etc.
  dates.map(&:qualified_value)
end

#place_terms(element) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/mods_display/fields/imprint.rb', line 205

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

#unencoded_place_terms?(element) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
# File 'lib/mods_display/fields/imprint.rb', line 229

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