Module: CocinaDisplay::Concerns::Events

Included in:
CocinaDisplay::CocinaRecord, RelatedResource
Defined in:
lib/cocina_display/concerns/events.rb

Instance Method Summary collapse

Instance Method Details

#admin_creation_eventCocinaDisplay::Events::Event

The adminMetadata creation event (When was it was deposited?)



137
138
139
# File 'lib/cocina_display/concerns/events.rb', line 137

def admin_creation_event
  @admin_events ||= path("$.description.adminMetadata.event[?(@.type==\"creation\")]").map { |event| CocinaDisplay::Events::Event.new(event) }.first
end

#earliest_preferred_date(dates, ignore_qualified: false) ⇒ CocinaDisplay::Dates::Date?

Choose the earliest, best date from a provided list of event dates. Rules to consider:

  1. Reject any dates that were not parsed.

  2. If ignore_qualified is true, reject any qualified dates.

  3. If there are any primary dates, prefer those dates.

  4. If there are any encoded dates, prefer those dates.

  5. From whatever is left, choose the earliest date.

Parameters:

  • dates (Array<CocinaDisplay::Dates::Date>)

    The list of dates

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:



200
201
202
203
204
205
206
207
208
209
# File 'lib/cocina_display/concerns/events.rb', line 200

def earliest_preferred_date(dates, ignore_qualified: false)
  return nil if dates.empty?

  dates.filter!(&:parsed_date?)
  dates.reject!(&:approximate?) if ignore_qualified
  dates = dates.filter(&:primary?).presence || dates
  dates = dates.filter(&:encoding?).presence || dates

  dates.min
end

#event_datesArray<CocinaDisplay::Dates::Date>

All dates associated with the object via an event.

Returns:



157
158
159
# File 'lib/cocina_display/concerns/events.rb', line 157

def event_dates
  @event_dates ||= events.flat_map(&:dates)
end

#event_display_dataArray<CocinaDisplay::DisplayData>

DisplayData for all events associated with the object.

Returns:



163
164
165
# File 'lib/cocina_display/concerns/events.rb', line 163

def event_display_data
  CocinaDisplay::DisplayData.from_objects(events)
end

#event_note_display_dataArray<CocinaDisplay::DisplayData>

DisplayData for issuance, copyright, and other notes associated with events.

Returns:



169
170
171
# File 'lib/cocina_display/concerns/events.rb', line 169

def event_note_display_data
  CocinaDisplay::DisplayData.from_objects(events.flat_map(&:notes))
end

#eventsArray<CocinaDisplay::Events::Event>

All root level events associated with the object.

Returns:



128
129
130
131
132
133
# File 'lib/cocina_display/concerns/events.rb', line 128

def events
  @events ||= path("$.description.event.*").map do |cocina|
    event = CocinaDisplay::Events::Event.new(cocina)
    event.imprint? ? CocinaDisplay::Events::Imprint.new(cocina) : event
  end
end

#imprint_eventsArray<CocinaDisplay::Imprint>

Array of CocinaDisplay::Imprint objects for all relevant Cocina events.

Returns:

  • (Array<CocinaDisplay::Imprint>)


151
152
153
# File 'lib/cocina_display/concerns/events.rb', line 151

def imprint_events
  events.filter { |event| event.is_a? CocinaDisplay::Events::Imprint }
end

#imprint_strString?

String for displaying the imprint statement(s). “New York : Meridian Book, 1993, c1967”

Examples:

bt553vr2845

Returns:

  • (String, nil)

See Also:

  • Imprint#to_s


108
109
110
# File 'lib/cocina_display/concerns/events.rb', line 108

def imprint_str
  imprint_events.map(&:to_s).compact_blank.join("; ")
end

#pub_date(ignore_qualified: false) ⇒ CocinaDisplay::Dates::Date?

The earliest preferred publication date as a CocinaDisplay::Dates::Date object. Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding.

Parameters:

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:



179
180
181
182
183
184
185
186
187
# File 'lib/cocina_display/concerns/events.rb', line 179

def pub_date(ignore_qualified: false)
  pub_event_dates = event_dates.filter { |date| date.type == "publication" }
  creation_event_dates = event_dates.filter { |date| date.type == "creation" }
  capture_event_dates = event_dates.filter { |date| date.type == "capture" }

  [pub_event_dates, creation_event_dates, capture_event_dates].flat_map do |dates|
    earliest_preferred_date(dates, ignore_qualified: ignore_qualified)
  end.compact.first
end

#pub_date_edtf(ignore_qualified: false) ⇒ Date?

The earliest preferred publication date as a Date object. If the date was a range or interval, uses the start (or end if no start). Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding.

Parameters:

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:

  • (Date, nil)

See Also:



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cocina_display/concerns/events.rb', line 11

def pub_date_edtf(ignore_qualified: false)
  return unless (date = pub_date(ignore_qualified: ignore_qualified))

  if date.is_a? CocinaDisplay::Dates::DateRange
    date = date.start&.known? ? date.start : date.stop
  end

  return unless date&.known?
  return date.date.from if date.date.is_a?(EDTF::Interval)

  date.date
end

#pub_date_sort_str(ignore_qualified: false) ⇒ String?

Note:

BCE dates have special handling; see Date#sort_key for details.

String for sorting lexicographically by publication date. Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding.

Examples:

Year with month and day, 2024-08-21 (vc109xd3118)

"20240821"

Approximate year range, [ca. 1932 - 2012] (bb099mt5053)

"1932000020120000"

BCE year range, [ca. 3500 BCE] - 3101 BCE (yv690gn5376)

"-564990000-568980000"

Parameters:

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:

  • (String, nil)


82
83
84
# File 'lib/cocina_display/concerns/events.rb', line 82

def pub_date_sort_str(ignore_qualified: false)
  pub_date(ignore_qualified: ignore_qualified)&.sort_key
end

#pub_date_strString?

String for displaying the publication date. Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding. If not encoded, returns the original string value from the Cocina.

Examples:

w3cdtf encoded year with month and day (vc109xd3118)

"August 21, 2024"

w3cdtf encoded approximate year range (bb099mt5053)

"[ca. 1932 - 2012]"

Unencoded string, ‘about 933’

"about 933"

Not parsable, ‘invalid-date’

"invalid-date"

Returns:

  • (String, nil)


99
100
101
# File 'lib/cocina_display/concerns/events.rb', line 99

def pub_date_str
  pub_date&.to_s
end

#pub_year_int(ignore_qualified: false) ⇒ Integer?

Note:

6 BCE will return -5; 4 CE will return 4.

The earliest preferred publication year as an integer. If the date was a range or interval, uses the start (or end if no start). Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding.

Parameters:

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:

  • (Integer, nil)


31
32
33
# File 'lib/cocina_display/concerns/events.rb', line 31

def pub_year_int(ignore_qualified: false)
  pub_date_edtf(ignore_qualified: ignore_qualified)&.year
end

#pub_year_ints(ignore_qualified: false) ⇒ Array<Integer>?

Note:

6 BCE will appear as -5; 4 CE will appear as 4.

The range of preferred publication years as an array of integers. Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding.

Parameters:

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:

  • (Array<Integer>, nil)


41
42
43
44
45
46
# File 'lib/cocina_display/concerns/events.rb', line 41

def pub_year_ints(ignore_qualified: false)
  date = pub_date(ignore_qualified: ignore_qualified)
  return unless date

  date.to_a.map(&:year).compact.uniq.sort
end

#pub_year_str(ignore_qualified: false) ⇒ String?

String for displaying the earliest preferred publication year or range. Considers publication, creation, and capture dates in that order. Prefers dates marked as primary and those with a declared encoding.

Examples:

Year with month and day, 2024-08-21 (vc109xd3118)

"2024"

Approximate year range, [ca. 1932 - 2012] (bb099mt5053)

"1932 - 2012"

BCE year range, [ca. 3500 BCE] - 3101 BCE (yv690gn5376)

"3500 BCE - 3101 BCE"

Unencoded string, ‘about 933’

"933 CE"

Not parsable, ‘invalid-date’

nil

Parameters:

  • ignore_qualified (Boolean) (defaults to: false)

    Reject qualified dates (e.g. approximate)

Returns:

  • (String, nil)


63
64
65
66
67
68
# File 'lib/cocina_display/concerns/events.rb', line 63

def pub_year_str(ignore_qualified: false)
  date = pub_date(ignore_qualified: ignore_qualified)
  return unless date&.parsed_date?

  date.decoded_value(allowed_precisions: [:year, :decade, :century])
end

#publication_countriesArray<String>

List of countries of publication as strings. Considers locations for all publication, creation, and capture events.

Returns:

  • (Array<String>)


122
123
124
# File 'lib/cocina_display/concerns/events.rb', line 122

def publication_countries
  publication_events.flat_map { |event| event.locations.map(&:country_name) }.compact_blank.uniq
end

#publication_eventsArray<CocinaDisplay::Events::Event>

All events that could be used to select a publication date. Includes publication, creation, and capture events. Considers event types as well as date types if the event is untyped.

Returns:



145
146
147
# File 'lib/cocina_display/concerns/events.rb', line 145

def publication_events
  events.filter { |event| event.has_any_type?("publication", "creation", "capture") }
end

#publication_placesArray<String>

List of places of publication as strings. Considers locations for all publication, creation, and capture events.

Returns:

  • (Array<String>)


115
116
117
# File 'lib/cocina_display/concerns/events.rb', line 115

def publication_places
  publication_events.flat_map { |event| event.locations.map(&:to_s) }.compact_blank.uniq
end