Class: CocinaDisplay::Subjects::SubjectValue

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/subjects/subject_value.rb

Overview

A descriptive value that can be part of a Subject.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ SubjectValue

Initialize a SubjectValue object with Cocina structured data.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the subject value.



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

#cocinaObject (readonly)

Returns the value of attribute cocina.



5
6
7
# File 'lib/cocina_display/subjects/subject_value.rb', line 5

def cocina
  @cocina
end

#typeObject

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_typesArray<String>

All subject value types that should not be further destructured.

Returns:

  • (Array<String>)


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.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the subject.

  • type (String, nil)

    The type, coming from the parent Subject.

Returns:



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.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the subject.

Returns:

  • (Array<Hash>)

    An array of Cocina hashes, one for each split value



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_sString

The display string for the subject value. Subclasses should override this method to provide specific formatting.

Returns:

  • (String)


52
53
54
# File 'lib/cocina_display/subjects/subject_value.rb', line 52

def to_s
  cocina["value"]
end