Class: CocinaDisplay::Subjects::SubjectValue
- Inherits:
-
Object
- Object
- CocinaDisplay::Subjects::SubjectValue
- Defined in:
- lib/cocina_display/subjects/subject_value.rb
Overview
A descriptive value that can be part of a Subject.
Direct Known Subclasses
CoordinatesSubjectValue, NameSubjectValue, PlaceSubjectValue, TemporalSubjectValue, TitleSubjectValue
Instance Attribute Summary collapse
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
-
#type ⇒ Object
The type of the subject value, like “person”, “title”, or “time”.
Class Method Summary collapse
-
.atomic_types ⇒ Array<String>
All subject value types that should not be further destructured.
-
.from_cocina(cocina, type:) ⇒ Array<SubjectValue>
Create SubjectValues 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) ⇒ SubjectValue
constructor
Initialize a SubjectValue object with Cocina structured data.
-
#to_s ⇒ String
The display string for the subject value.
Constructor Details
#initialize(cocina) ⇒ SubjectValue
Initialize a SubjectValue object with Cocina structured data.
44 45 46 47 |
# File 'lib/cocina_display/subjects/subject_value.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_value.rb', line 5 def cocina @cocina end |
#type ⇒ Object
The type of the subject value, like “person”, “title”, or “time”.
9 10 11 |
# File 'lib/cocina_display/subjects/subject_value.rb', line 9 def type @type end |
Class Method Details
.atomic_types ⇒ Array<String>
All subject value types that should not be further destructured.
38 39 40 |
# File 'lib/cocina_display/subjects/subject_value.rb', line 38 def self.atomic_types SUBJECT_VALUE_TYPES.keys - ["place"] end |
.from_cocina(cocina, type:) ⇒ Array<SubjectValue>
Create SubjectValues from Cocina structured data. Pre-coordinated string values will be split into multiple SubjectValues.
16 17 18 19 20 21 22 |
# File 'lib/cocina_display/subjects/subject_value.rb', line 16 def self.from_cocina(cocina, type:) split_pre_coordinated_values(cocina, type: type).map do |value| SUBJECT_VALUE_TYPES.fetch(type, SubjectValue).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_value.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 value. Subclasses should override this method to provide specific formatting.
52 53 54 |
# File 'lib/cocina_display/subjects/subject_value.rb', line 52 def to_s cocina["value"] end |