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.
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
Instance Method Summary collapse
-
#flat_value ⇒ String
A string representation of the entire subject, concatenated for display.
-
#initialize(cocina, delimiter: " > ") ⇒ 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.
-
#values ⇒ Array<String>
Array of display strings for each value in the subject.
Constructor Details
#initialize(cocina, delimiter: " > ") ⇒ Subject
Initialize a Subject object with Cocina structured data.
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
#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 |
#delimiter ⇒ Object (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_value ⇒ String
A string representation of the entire subject, concatenated for display.
38 39 40 |
# File 'lib/cocina_display/subjects/subject.rb', line 38 def flat_value Utils.compact_and_join(values, delimiter: delimiter) end |
#label ⇒ String
Label used to render the subject for display. Uses a displayLabel if available, otherwise looks up via type.
45 46 47 |
# File 'lib/cocina_display/subjects/subject.rb', line 45 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.
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_s ⇒ String
The value to use for display. Genre values are capitalized; other subject values are not.
32 33 34 |
# File 'lib/cocina_display/subjects/subject.rb', line 32 def to_s (type == "genre") ? flat_value&.upcase_first : flat_value end |
#type ⇒ String?
The top-level type of the subject.
18 19 20 |
# File 'lib/cocina_display/subjects/subject.rb', line 18 def type cocina["type"] end |
#values ⇒ Array<String>
Array of display strings for each value in the subject. Used for search, where each value should be indexed separately.
25 26 27 |
# File 'lib/cocina_display/subjects/subject.rb', line 25 def values subject_values.map(&:to_s).compact_blank end |