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.



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

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (readonly)

Returns the value of attribute cocina.



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

def cocina
  @cocina
end

Instance Method Details

#display_strString

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

Returns:

  • (String)


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

def display_str
  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>)


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

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

#subject_valuesArray<SubjectValue>

Individual values composing this subject. Can be multiple if the Cocina featured nested data. If no type was specified on a value, uses the top-level subject type.

Returns:



40
41
42
43
44
45
46
# File 'lib/cocina_display/subjects/subject.rb', line 40

def subject_values
  @subject_values ||= Utils.flatten_nested_values(cocina, atomic_types: SubjectValue.atomic_types).map do |value|
    subject_value = SubjectValue.from_cocina(value)
    subject_value.type ||= type
    subject_value
  end
end

#typeString?

The top-level type of the subject.



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

def type
  cocina["type"]
end