Class: CocinaDisplay::Subjects::Subject

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/subjects/subject.rb

Overview

Base class for subjects in Cocina structured data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Subject

Initialize a Subject object with Cocina structured data.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the subject.



9
10
11
# File 'lib/cocina_display/subjects/subject.rb', line 9

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (readonly)

Returns the value of attribute cocina.



5
6
7
# File 'lib/cocina_display/subjects/subject.rb', line 5

def cocina
  @cocina
end

Instance Method Details

#display_valueString

A string representation of the entire subject, concatenated for display.

Returns:

  • (String)


36
37
38
# File 'lib/cocina_display/subjects/subject.rb', line 36

def display_value
  Utils.compact_and_join(display_values, delimiter: " > ")
end

#display_valuesArray<String>

Array of display strings for each value in the subject. Used for search, where each value should be indexed separately.

Returns:

  • (Array<String>)


23
24
25
# File 'lib/cocina_display/subjects/subject.rb', line 23

def display_values
  subject_values.map(&:to_s).compact_blank
end

#labelString

Label used to render the subject for display. Uses a displayLabel if available, otherwise looks up via type.

Returns:

  • (String)


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

def label
  cocina["displayLabel"].presence || type_label
end

#subject_valuesArray<SubjectValue>

Individual values composing this subject. Can be multiple if the Cocina featured nested data. All SubjectValues inherit the type of their parent Subject.

Returns:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cocina_display/subjects/subject.rb', line 51

def subject_values
  @subject_values ||= (Array(cocina["parallelValue"]).presence || [cocina]).flat_map do |node|
    if SubjectValue.atomic_types.include?(type)
      SubjectValue.from_cocina(node, type: type)
    else
      Utils.flatten_nested_values(node, atomic_types: SubjectValue.atomic_types).flat_map do |value|
        SubjectValue.from_cocina(value, type: value["type"] || type)
      end
    end
  end
end

#to_sString

The value to use for display. Genre values are capitalized; other subject values are not.

Returns:

  • (String)


30
31
32
# File 'lib/cocina_display/subjects/subject.rb', line 30

def to_s
  (type == "genre") ? display_value&.upcase_first : display_value
end

#typeString?

The top-level type of the subject.



16
17
18
# File 'lib/cocina_display/subjects/subject.rb', line 16

def type
  cocina["type"]
end