Class: CocinaDisplay::Note
- Inherits:
-
Object
- Object
- CocinaDisplay::Note
- Defined in:
- lib/cocina_display/note.rb
Overview
A note associated with a cocina record
Constant Summary collapse
- ABSTRACT_TYPES =
["summary", "abstract", "scope and content"].freeze
- ABSTRACT_DISPLAY_LABEL_REGEX =
/Abstract|Summary|Scope and content/i
- PREFERRED_CITATION_TYPES =
["preferred citation"].freeze
- PREFERRED_CITATION_DISPLAY_LABEL_REGEX =
/Preferred citation/i
- TOC_TYPES =
["table of contents"].freeze
- TOC_DISPLAY_LABEL_REGEX =
/Table of contents/i
Instance Attribute Summary collapse
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
Instance Method Summary collapse
-
#abstract? ⇒ Boolean
Check if the note is an abstract.
-
#display_label ⇒ String?
The display label set in Cocina.
-
#general_note? ⇒ Boolean
Check if the note is a general note (not a table of contents, abstract, preferred citation, or part).
-
#initialize(cocina) ⇒ Note
constructor
Initialize a Note from Cocina structured data.
-
#label ⇒ String
Label used to render the note for display.
-
#part? ⇒ Boolean
Check if the note is a part note.
-
#preferred_citation? ⇒ Boolean
Check if the note is a preferred citation.
-
#table_of_contents? ⇒ Boolean
Check if the note is a table of contents.
-
#to_s ⇒ String?
String representation of the note.
-
#type ⇒ String?
The type of the note, e.g.
-
#values ⇒ String
The raw values from the Cocina data, flattened if nested.
-
#values_by_type ⇒ Hash{String => String}
The raw values from the Cocina data as a hash with type as key.
Constructor Details
#initialize(cocina) ⇒ Note
Initialize a Note from Cocina structured data.
15 16 17 |
# File 'lib/cocina_display/note.rb', line 15 def initialize(cocina) @cocina = cocina end |
Instance Attribute Details
#cocina ⇒ Object (readonly)
Returns the value of attribute cocina.
11 12 13 |
# File 'lib/cocina_display/note.rb', line 11 def cocina @cocina end |
Instance Method Details
#abstract? ⇒ Boolean
Check if the note is an abstract
65 66 67 68 |
# File 'lib/cocina_display/note.rb', line 65 def abstract? display_label&.match?(ABSTRACT_DISPLAY_LABEL_REGEX) || ABSTRACT_TYPES.include?(type) end |
#display_label ⇒ String?
The display label set in Cocina
49 50 51 |
# File 'lib/cocina_display/note.rb', line 49 def display_label cocina["displayLabel"].presence end |
#general_note? ⇒ Boolean
Check if the note is a general note (not a table of contents, abstract, preferred citation, or part)
72 73 74 |
# File 'lib/cocina_display/note.rb', line 72 def general_note? !table_of_contents? && !abstract? && !preferred_citation? && !part? end |
#label ⇒ String
Label used to render the note for display. Uses a displayLabel if available, otherwise tries to look up via type. Falls back to a default label derived from the type or a generic note label if no type is set.
58 59 60 61 |
# File 'lib/cocina_display/note.rb', line 58 def label display_label || I18n.t(type&.parameterize&.underscore, default: default_label, scope: "cocina_display.field_label.note") end |
#part? ⇒ Boolean
These are combined with the title and not displayed separately.
Check if the note is a part note
93 94 95 |
# File 'lib/cocina_display/note.rb', line 93 def part? type == "part" end |
#preferred_citation? ⇒ Boolean
Check if the note is a preferred citation
78 79 80 81 |
# File 'lib/cocina_display/note.rb', line 78 def preferred_citation? display_label&.match?(PREFERRED_CITATION_DISPLAY_LABEL_REGEX) || PREFERRED_CITATION_TYPES.include?(type) end |
#table_of_contents? ⇒ Boolean
Check if the note is a table of contents
85 86 87 88 |
# File 'lib/cocina_display/note.rb', line 85 def table_of_contents? display_label&.match?(TOC_DISPLAY_LABEL_REGEX) || TOC_TYPES.include?(type) end |
#to_s ⇒ String?
String representation of the note.
21 22 23 |
# File 'lib/cocina_display/note.rb', line 21 def to_s Utils.compact_and_join(values, delimiter: " -- ").presence end |
#type ⇒ String?
The type of the note, e.g. “abstract”.
43 44 45 |
# File 'lib/cocina_display/note.rb', line 43 def type cocina["type"].presence end |
#values ⇒ String
The raw values from the Cocina data, flattened if nested.
27 28 29 |
# File 'lib/cocina_display/note.rb', line 27 def values Utils.flatten_nested_values(cocina).pluck("value").compact_blank end |
#values_by_type ⇒ Hash{String => String}
The raw values from the Cocina data as a hash with type as key.
33 34 35 36 37 38 39 |
# File 'lib/cocina_display/note.rb', line 33 def values_by_type Utils.flatten_nested_values(cocina).each_with_object({}) do |node, hash| type = node["type"] hash[type] ||= [] hash[type] << node["value"] end end |