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, delimiter: " > ") ⇒ Subject

Initialize a Subject object with Cocina structured data.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the subject.

  • delimiter (String) (defaults to: " > ")

    The delimiter to use when flattening for display



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

def initialize(cocina, delimiter: " > ")
  @cocina = cocina
  @delimiter = delimiter
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

#delimiterObject (readonly)

Returns the value of attribute delimiter.



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

def delimiter
  @delimiter
end

Instance Method Details

#flat_valueString

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

Returns:

  • (String)


38
39
40
# File 'lib/cocina_display/subjects/subject.rb', line 38

def flat_value
  Utils.compact_and_join(values, delimiter: delimiter)
end

#labelString

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

Returns:

  • (String)


45
46
47
# File 'lib/cocina_display/subjects/subject.rb', line 45

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:



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

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)


32
33
34
# File 'lib/cocina_display/subjects/subject.rb', line 32

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

#typeString?

The top-level type of the subject.



18
19
20
# File 'lib/cocina_display/subjects/subject.rb', line 18

def type
  cocina["type"]
end

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


25
26
27
# File 'lib/cocina_display/subjects/subject.rb', line 25

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