Class: CocinaDisplay::Note

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Note

Initialize a Note from Cocina structured data.

Parameters:

  • cocina (Hash)


15
16
17
# File 'lib/cocina_display/note.rb', line 15

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (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

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


33
34
35
# File 'lib/cocina_display/note.rb', line 33

def delimited?
  delimiter.present?
end

#delimiterString?

Delimiter used to join multiple values for display.

Returns:

  • (String, nil)


27
28
29
# File 'lib/cocina_display/note.rb', line 27

def delimiter
  " -- " if table_of_contents?
end

#display_labelString?

The display label set in Cocina

Returns:

  • (String, nil)


77
78
79
# File 'lib/cocina_display/note.rb', line 77

def display_label
  cocina["displayLabel"].presence
end

#flat_valueString?

Single concatenated string value for the note.

Returns:

  • (String, nil)


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)

Returns:

  • (Boolean)


100
101
102
# File 'lib/cocina_display/note.rb', line 100

def general_note?
  !table_of_contents? && !abstract? && !preferred_citation? && !part?
end

#labelString

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.

Returns:

  • (String)


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

Note:

These are combined with the title and not displayed separately.

Check if the note is a part note

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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_sString?

The value to use for display.

Returns:

  • (String, nil)


21
22
23
# File 'lib/cocina_display/note.rb', line 21

def to_s
  flat_value
end

#typeString?

The type of the note, e.g. “abstract”.

Returns:

  • (String, nil)


71
72
73
# File 'lib/cocina_display/note.rb', line 71

def type
  cocina["type"].presence
end

#valuesArray<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).

Returns:

  • (Array<String>)


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_typeHash{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).

Returns:

  • (Hash{String => Array<String>})


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