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.
-
#delimited? ⇒ Boolean
Does this note use a delimiter?.
-
#delimiter ⇒ String?
Delimiter used to join multiple values for display.
-
#display_label ⇒ String?
The display label set in Cocina.
-
#flat_value ⇒ String?
Single concatenated string value for the note.
-
#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?
The value to use for display.
-
#type ⇒ String?
The type of the note, e.g.
-
#values ⇒ Array<String>
The raw values from the Cocina data, flattened if nested.
-
#values_by_type ⇒ Hash{String => Array<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
93 94 95 96 |
# File 'lib/cocina_display/note.rb', line 93 def abstract? display_label&.match?(ABSTRACT_DISPLAY_LABEL_REGEX) || ABSTRACT_TYPES.include?(type) end |
#delimited? ⇒ Boolean
Does this note use a delimiter?
33 34 35 |
# File 'lib/cocina_display/note.rb', line 33 def delimited? delimiter.present? end |
#delimiter ⇒ String?
Delimiter used to join multiple values for display.
27 28 29 |
# File 'lib/cocina_display/note.rb', line 27 def delimiter " -- " if table_of_contents? end |
#display_label ⇒ String?
The display label set in Cocina
77 78 79 |
# File 'lib/cocina_display/note.rb', line 77 def display_label cocina["displayLabel"].presence end |
#flat_value ⇒ String?
Single concatenated string value for the note.
39 40 41 |
# File 'lib/cocina_display/note.rb', line 39 def flat_value Utils.compact_and_join(values, delimiter: delimiter || "").presence end |
#general_note? ⇒ Boolean
Check if the note is a general note (not a table of contents, abstract, preferred citation, or part)
100 101 102 |
# File 'lib/cocina_display/note.rb', line 100 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.
86 87 88 89 |
# File 'lib/cocina_display/note.rb', line 86 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
121 122 123 |
# File 'lib/cocina_display/note.rb', line 121 def part? type == "part" end |
#preferred_citation? ⇒ Boolean
Check if the note is a preferred citation
106 107 108 109 |
# File 'lib/cocina_display/note.rb', line 106 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
113 114 115 116 |
# File 'lib/cocina_display/note.rb', line 113 def table_of_contents? display_label&.match?(TOC_DISPLAY_LABEL_REGEX) || TOC_TYPES.include?(type) end |
#to_s ⇒ String?
The value to use for display.
21 22 23 |
# File 'lib/cocina_display/note.rb', line 21 def to_s flat_value end |
#type ⇒ String?
The type of the note, e.g. “abstract”.
71 72 73 |
# File 'lib/cocina_display/note.rb', line 71 def type cocina["type"].presence end |
#values ⇒ Array<String>
The raw values from the Cocina data, flattened if nested. Strips excess whitespace and the delimiter if present. Splits on the delimiter if it was already included in the values(s).
47 48 49 50 51 52 |
# File 'lib/cocina_display/note.rb', line 47 def values Utils.flatten_nested_values(cocina).pluck("value") .map { |value| cleaned_value(value) } .flat_map { |value| delimited? ? value.split(delimiter.strip) : [value] } .compact_blank end |
#values_by_type ⇒ Hash{String => Array<String>}
The raw values from the Cocina data as a hash with type as key. Strips excess whitespace and the delimiter if present. Splits on the delimiter if it was already included in the values(s).
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cocina_display/note.rb', line 58 def values_by_type Utils.flatten_nested_values(cocina).each_with_object({}) do |node, hash| value = cleaned_value(node["value"]) (delimited? ? value.split(delimiter.strip) : [value]).each do |part| type = node["type"] hash[type] ||= [] hash[type] << part end end end |