cocina_display 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,127 +0,0 @@
1
- require_relative "utils"
2
- require_relative "contributor"
3
- require_relative "title_builder"
4
- require_relative "dates/date"
5
-
6
- module CocinaDisplay
7
- # Base class for subjects in Cocina structured data.
8
- class Subject
9
- attr_reader :cocina
10
-
11
- # Extract the type of the subject from the Cocina structured data.
12
- # If no top-level type, uses the first structuredValue type.
13
- # @param cocina [Hash] The Cocina structured data for the subject.
14
- # @return [String, nil] The type of the subject, or nil if none
15
- # @see https://github.com/sul-dlss/cocina-models/blob/main/docs/description_types.md#subject-types
16
- def self.detect_type(cocina)
17
- cocina["type"] || Utils.flatten_nested_values(cocina).pick("type")
18
- end
19
-
20
- # Choose and create the appropriate Subject subclass based on type.
21
- # @param cocina [Hash] The Cocina structured data for the subject.
22
- # @return [Subject]
23
- # @see detect_type
24
- def self.from_cocina(cocina)
25
- case detect_type(cocina)
26
- when "person", "family", "organization", "conference", "event", "name"
27
- NameSubject.new(cocina)
28
- when "title"
29
- TitleSubject.new(cocina)
30
- when "time"
31
- TemporalSubject.new(cocina)
32
- # TODO: special handling for geospatial subjects
33
- # when "map coordinates", "bounding box coordinates", "point coordinates"
34
- else
35
- Subject.new(cocina)
36
- end
37
- end
38
-
39
- # Initialize a Subject object with Cocina structured data.
40
- # @param cocina [Hash] The Cocina structured data for the subject.
41
- def initialize(cocina)
42
- @cocina = cocina
43
- end
44
-
45
- # The type of the subject.
46
- # If no top-level type, uses the first structuredValue type.
47
- # @return [String, nil] The type of the subject, or nil if none
48
- # @see detect_type
49
- def type
50
- self.class.detect_type(cocina)
51
- end
52
-
53
- # A string representation of the subject, formatted for display.
54
- # Concatenates any structured values with an appropriate delimiter.
55
- # Subclasses may override this for more specific formatting.
56
- # @return [String]
57
- def display_str
58
- Utils.compact_and_join(descriptive_values, delimiter: delimiter)
59
- end
60
-
61
- private
62
-
63
- # Flatten any structured values into an array of Hashes with "value" keys.
64
- # If no structured values, will return the top-level cocina data.
65
- # @see Utils.flatten_nested_values
66
- # @return [Array<Hash>] An array of Hashes representing all values.
67
- def descriptive_values
68
- Utils.flatten_nested_values(cocina).pluck("value")
69
- end
70
-
71
- # Delimiter to use for joining structured subject values.
72
- # LCSH uses a comma (the default); catalog headings use " > ".
73
- # @return [String]
74
- def delimiter
75
- if cocina["displayLabel"]&.downcase == "catalog heading"
76
- " > "
77
- else
78
- ", "
79
- end
80
- end
81
- end
82
-
83
- # A subject representing a named entity.
84
- class NameSubject < Subject
85
- attr_reader :name
86
-
87
- # Initialize a NameSubject object with Cocina structured data.
88
- # @param cocina [Hash] The Cocina structured data for the subject.
89
- def initialize(cocina)
90
- super
91
- @name = Contributor::Name.new(cocina)
92
- end
93
-
94
- # Use the contributor name formatting rules for display.
95
- # @return [String] The formatted name string, including life dates
96
- # @see CocinaDisplay::Contributor::Name#display_str
97
- def display_str
98
- @name.display_str(with_date: true)
99
- end
100
- end
101
-
102
- # A subject representing an entity with a title.
103
- class TitleSubject < Subject
104
- # Construct a title string to use for display.
105
- # @see CocinaDisplay::TitleBuilder.build
106
- # @note Unclear how often structured title subjects occur "in the wild".
107
- # @return [String]
108
- def display_str
109
- TitleBuilder.build([cocina])
110
- end
111
- end
112
-
113
- # A subject representing a date and/or time.
114
- class TemporalSubject < Subject
115
- attr_reader :date
116
-
117
- def initialize(cocina)
118
- super
119
- @date = Dates::Date.from_cocina(cocina)
120
- end
121
-
122
- # @return [String] The formatted date/time string for display
123
- def display_str
124
- @date.qualified_value
125
- end
126
- end
127
- end