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)


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

The display label set in Cocina

Returns:

  • (String, nil)


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)

Returns:

  • (Boolean)


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

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)


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

Note:

These are combined with the title and not displayed separately.

Check if the note is a part note

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

String representation of the note.

Returns:

  • (String, nil)


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

def to_s
  Utils.compact_and_join(values, delimiter: " -- ").presence
end

#typeString?

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

Returns:

  • (String, nil)


43
44
45
# File 'lib/cocina_display/note.rb', line 43

def type
  cocina["type"].presence
end

#valuesString

The raw values from the Cocina data, flattened if nested.

Returns:

  • (String)


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_typeHash{String => String}

The raw values from the Cocina data as a hash with type as key.

Returns:

  • (Hash{String => String})


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