Class: CocinaDisplay::Subjects::SubjectPart
- Inherits:
-
Object
- Object
- CocinaDisplay::Subjects::SubjectPart
- Defined in:
- lib/cocina_display/subjects/subject_part.rb
Overview
A descriptive value that can be part of a structured Subject.
Direct Known Subclasses
CoordinatesSubjectPart, NameSubjectPart, PlaceSubjectPart, TemporalSubjectPart, TitleSubjectPart
Instance Attribute Summary collapse
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
-
#type ⇒ Object
The type of the subject part, like “person”, “title”, or “time”.
Class Method Summary collapse
-
.atomic_types ⇒ Array<String>
All subject part types that should not be further destructured.
-
.from_cocina(cocina, type:) ⇒ Array<SubjectPart>
Create SubjectParts from Cocina structured data.
-
.split_pre_coordinated_values(cocina, type:) ⇒ Array<Hash>
Split a pre-coordinated subject value joined with “–” into multiple values.
Instance Method Summary collapse
-
#initialize(cocina) ⇒ SubjectPart
constructor
Initialize a SubjectPart object with Cocina structured data.
-
#to_s ⇒ String
The display string for the subject part.
Constructor Details
#initialize(cocina) ⇒ SubjectPart
Initialize a SubjectPart object with Cocina structured data.
44 45 46 47 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 44 def initialize(cocina) @cocina = cocina @type = cocina["type"] end |
Instance Attribute Details
#cocina ⇒ Object (readonly)
Returns the value of attribute cocina.
5 6 7 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 5 def cocina @cocina end |
#type ⇒ Object
The type of the subject part, like “person”, “title”, or “time”.
9 10 11 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 9 def type @type end |
Class Method Details
.atomic_types ⇒ Array<String>
All subject part types that should not be further destructured.
38 39 40 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 38 def self.atomic_types SUBJECT_PART_TYPES.keys - ["place"] end |
.from_cocina(cocina, type:) ⇒ Array<SubjectPart>
Create SubjectParts from Cocina structured data. Pre-coordinated string values will be split into multiple SubjectParts.
16 17 18 19 20 21 22 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 16 def self.from_cocina(cocina, type:) split_pre_coordinated_values(cocina, type: type).map do |value| SUBJECT_PART_TYPES.fetch(type, SubjectPart).new(value).tap do |obj| obj.type ||= type end end end |
.split_pre_coordinated_values(cocina, type:) ⇒ Array<Hash>
Split a pre-coordinated subject value joined with “–” into multiple values. Ignores the “–” string for coordinate subject types, which use it differently.
28 29 30 31 32 33 34 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 28 def self.split_pre_coordinated_values(cocina, type:) if cocina["value"].is_a?(String) && cocina["value"].include?("--") && !type&.include?("coordinates") cocina["value"].split("--").map { |v| cocina.merge("value" => v.strip) } else [cocina] end end |
Instance Method Details
#to_s ⇒ String
The display string for the subject part. Subclasses should override this method to provide specific formatting.
52 53 54 |
# File 'lib/cocina_display/subjects/subject_part.rb', line 52 def to_s cocina["value"] end |