Class: CocinaDisplay::Notes::NoteValue

Inherits:
Parallel::ParallelValue show all
Defined in:
lib/cocina_display/notes/note_value.rb

Overview

A Note associated with an item in a single language.

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

Constants inherited from Parallel::ParallelValue

Parallel::ParallelValue::PARALLEL_TYPES

Instance Attribute Summary

Attributes inherited from Parallel::ParallelValue

#cocina, #parent, #role

Instance Method Summary collapse

Methods inherited from Parallel::ParallelValue

#display?, #initialize, #language, #main_value, #own_type, #own_typed?, #primary?, #siblings, #status, #translated?, #transliterated?, #type, #typed?, #vernacular?

Constructor Details

This class inherits a constructor from CocinaDisplay::Parallel::ParallelValue

Instance Method Details

#abstract?Boolean

Check if the note is an abstract

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/cocina_display/notes/note_value.rb', line 38

def abstract?
  display_label&.match?(ABSTRACT_DISPLAY_LABEL_REGEX) ||
    ABSTRACT_TYPES.include?(type)
end

#delimited?Boolean

Does this note use a delimiter?

Returns:

  • (Boolean)


32
33
34
# File 'lib/cocina_display/notes/note_value.rb', line 32

def delimited?
  delimiter.present?
end

#delimiterString?

Delimiter used to join multiple values for display.

Returns:

  • (String, nil)


26
27
28
# File 'lib/cocina_display/notes/note_value.rb', line 26

def delimiter
  " -- " if table_of_contents?
end

#flat_valueString?

Single concatenated string value for the note.

Returns:

  • (String, nil)


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

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)


45
46
47
# File 'lib/cocina_display/notes/note_value.rb', line 45

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

#labelString?

Custom label used when displaying the note, if any.

Returns:

  • (String, nil)


14
15
16
# File 'lib/cocina_display/notes/note_value.rb', line 14

def label
  display_label || type_label
end

#part?Boolean

Note:

These are combined with the title and not displayed separately.

Check if the note is a part note

Returns:

  • (Boolean)


66
67
68
# File 'lib/cocina_display/notes/note_value.rb', line 66

def part?
  type == "part"
end

#preferred_citation?Boolean

Check if the note is a preferred citation

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/cocina_display/notes/note_value.rb', line 51

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)


58
59
60
61
# File 'lib/cocina_display/notes/note_value.rb', line 58

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)


20
21
22
# File 'lib/cocina_display/notes/note_value.rb', line 20

def to_s
  flat_value
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>)


80
81
82
83
84
85
86
# File 'lib/cocina_display/notes/note_value.rb', line 80

def values
  Utils.flatten_nested_values(cocina).pluck("value")
    .map { |value| cleaned_value(value) }
    .flat_map { |value| delimited? ? value.split(delimiter.strip) : [value] }
    .map(&:strip)
    .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>})


92
93
94
95
96
97
98
99
100
101
# File 'lib/cocina_display/notes/note_value.rb', line 92

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.strip
    end
  end
end