Class: CocinaDisplay::Events::Event

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cocina_display/events/event.rb

Overview

An event associated with an object, like publication.

Direct Known Subclasses

Imprint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Event

Initialize the event with Cocina event data.

Parameters:

  • cocina (Hash)

    Cocina structured data for a single event



11
12
13
# File 'lib/cocina_display/events/event.rb', line 11

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (readonly)

Returns the value of attribute cocina.



7
8
9
# File 'lib/cocina_display/events/event.rb', line 7

def cocina
  @cocina
end

Instance Method Details

#<=>(other) ⇒ Integer?

Note:

Also supports ‘event1.between?(event2, event3)` via Comparable.

Compare this CocinaDisplay::Events::Event to another CocinaDisplay::Events::Event using their Dates.

Returns:

  • (Integer, nil)


18
19
20
# File 'lib/cocina_display/events/event.rb', line 18

def <=>(other)
  [dates] <=> [other.dates] if other.is_a?(Event)
end

#contributorsArray<CocinaDisplay::Contributor>

All contributors associated with this event.

Returns:

  • (Array<CocinaDisplay::Contributor>)


90
91
92
93
94
# File 'lib/cocina_display/events/event.rb', line 90

def contributors
  @contributors ||= Array(cocina["contributor"]).map do |contributor|
    CocinaDisplay::Contributors::Contributor.new(contributor)
  end
end

#date_typesArray<String>

Note:

This can differ from the top-level event type.

All types of dates associated with this event.



46
47
48
# File 'lib/cocina_display/events/event.rb', line 46

def date_types
  dates.map(&:type).uniq
end

#datesArray<CocinaDisplay::Dates::Date>

Note:

The date types may differ from the underlying event type.

All dates associated with this event. Ignores known unparsable date values like “9999”. If the date is untyped, uses this event’s type as the date type.

Returns:



69
70
71
72
73
74
75
76
# File 'lib/cocina_display/events/event.rb', line 69

def dates
  @dates ||= Array(cocina["date"]).filter_map do |date|
    CocinaDisplay::Dates::Date.from_cocina(date)
  end.filter(&:parsable?).map do |date|
    date.type ||= type
    date
  end
end

#has_any_type?(*match_types) ⇒ Boolean

True if the event or its dates have any of the provided types.

Parameters:

  • match_types (Array<String>)

    The types to check against

Returns:

  • (Boolean)


60
61
62
# File 'lib/cocina_display/events/event.rb', line 60

def has_any_type?(*match_types)
  match_types.any? { |type| has_type?(type) }
end

#has_type?(match_type) ⇒ Boolean

True if either the event type or any date type matches the given type.

Parameters:

  • match_type (String)

    The type to check against

Returns:

  • (Boolean)


53
54
55
# File 'lib/cocina_display/events/event.rb', line 53

def has_type?(match_type)
  types.include?(match_type)
end

#imprint?Boolean

Note:

Unencoded dates or no dates often indicate an imprint statement.

True if this event is likely to represent an imprint.

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/cocina_display/events/event.rb', line 81

def imprint?
  contributors.present? &&
    locations.present? &&
    (has_type?("publication") || types.empty?) &&
    (dates.any? { |date| !date.encoding? } || dates.none?)
end

#labelString

The display label for the event. Uses “Imprint” if the event is likely to represent an imprint statement. If the event consists solely of a date, uses the date’s label. Capitalizes the event’s type, or its first date’s type if untyped.

Returns:

  • (String)


27
28
29
30
31
32
# File 'lib/cocina_display/events/event.rb', line 27

def label
  return cocina["displayLabel"] if cocina["displayLabel"].present?
  return dates.map(&:label).first if date_only?

  type&.capitalize || date_types.first&.capitalize || "Event"
end

#locationsArray<CocinaDisplay::Events::Location>

All locations associated with this event.



98
99
100
101
102
# File 'lib/cocina_display/events/event.rb', line 98

def locations
  @locations ||= Array(cocina["location"]).map do |location|
    CocinaDisplay::Events::Location.new(location)
  end
end

#notesArray<CocinaDisplay::Events::Note>

All notes associated with this event.

Returns:



106
107
108
109
110
# File 'lib/cocina_display/events/event.rb', line 106

def notes
  @notes ||= Array(cocina["note"]).map do |note|
    CocinaDisplay::Events::Note.new(note)
  end
end

#to_sString

String representation of the event using date and location.

Examples:

“John Doe, New York (State), 1999”

Returns:

  • (String)


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

def to_s
  Utils.compact_and_join([place_str, date_str], delimiter: ", ")
end

#typeString?

Note:

This can differ from the contained date types.

The declared type of the event, like “publication” or “creation”.



38
39
40
# File 'lib/cocina_display/events/event.rb', line 38

def type
  cocina["type"]
end

#typesArray<String>

Union of event’s type and its date types. Used for imprint detection and display decisions.

Returns:

  • (Array<String>)


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

def types
  [type, *date_types].compact
end