Class: CocinaDisplay::Subjects::Subject
- Inherits:
-
Object
- Object
- CocinaDisplay::Subjects::Subject
- Defined in:
- lib/cocina_display/subjects/subject.rb
Overview
Base class for subjects in Cocina structured data.
Instance Attribute Summary collapse
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
Instance Method Summary collapse
-
#display_value ⇒ String
A string representation of the entire subject, concatenated for display.
-
#display_values ⇒ Array<String>
Array of display strings for each value in the subject.
-
#initialize(cocina) ⇒ Subject
constructor
Initialize a Subject object with Cocina structured data.
-
#label ⇒ String
Label used to render the subject for display.
-
#subject_values ⇒ Array<SubjectValue>
Individual values composing this subject.
-
#to_s ⇒ String
The value to use for display.
-
#type ⇒ String?
The top-level type of the subject.
Constructor Details
#initialize(cocina) ⇒ Subject
Initialize a Subject object with Cocina structured data.
9 10 11 |
# File 'lib/cocina_display/subjects/subject.rb', line 9 def initialize(cocina) @cocina = cocina end |
Instance Attribute Details
#cocina ⇒ Object (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_value ⇒ String
A string representation of the entire subject, concatenated for display.
36 37 38 |
# File 'lib/cocina_display/subjects/subject.rb', line 36 def display_value Utils.compact_and_join(display_values, delimiter: " > ") end |
#display_values ⇒ Array<String>
Array of display strings for each value in the subject. Used for search, where each value should be indexed separately.
23 24 25 |
# File 'lib/cocina_display/subjects/subject.rb', line 23 def display_values subject_values.map(&:to_s).compact_blank end |
#label ⇒ String
Label used to render the subject for display. Uses a displayLabel if available, otherwise looks up via type.
43 44 45 |
# File 'lib/cocina_display/subjects/subject.rb', line 43 def label cocina["displayLabel"].presence || type_label end |
#subject_values ⇒ Array<SubjectValue>
Individual values composing this subject. Can be multiple if the Cocina featured nested data. All SubjectValues inherit the type of their parent Subject.
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_s ⇒ String
The value to use for display. Genre values are capitalized; other subject values are not.
30 31 32 |
# File 'lib/cocina_display/subjects/subject.rb', line 30 def to_s (type == "genre") ? display_value&.upcase_first : display_value end |
#type ⇒ String?
The top-level type of the subject.
16 17 18 |
# File 'lib/cocina_display/subjects/subject.rb', line 16 def type cocina["type"] end |