cocina_display 0.6.0 → 1.0.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,234 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_support"
4
- require "active_support/core_ext/object/blank"
5
- require "active_support/core_ext/array/conversions"
6
-
7
- require_relative "utils"
8
- require_relative "marc_relator_codes"
9
-
10
- module CocinaDisplay
11
- # A contributor to a work, such as an author or publisher.
12
- class Contributor
13
- attr_reader :cocina
14
-
15
- # Initialize a Contributor object with Cocina structured data.
16
- # @param cocina [Hash] The Cocina structured data for the contributor.
17
- def initialize(cocina)
18
- @cocina = cocina
19
- end
20
-
21
- # String representation of the contributor, including name and role.
22
- # Used for debugging and logging.
23
- # @return [String]
24
- def to_s
25
- Utils.compact_and_join([display_name, display_role], delimiter: ": ")
26
- end
27
-
28
- # Is this contributor a human?
29
- # @return [Boolean]
30
- def person?
31
- cocina["type"] == "person"
32
- end
33
-
34
- # Is this contributor an organization?
35
- # @return [Boolean]
36
- def organization?
37
- cocina["type"] == "organization"
38
- end
39
-
40
- # Is this contributor a conference?
41
- # @return [Boolean]
42
- def conference?
43
- cocina["type"] == "conference"
44
- end
45
-
46
- # Is this contributor marked as primary?
47
- # @return [Boolean]
48
- def primary?
49
- cocina["status"] == "primary"
50
- end
51
-
52
- # Does this contributor have a role that indicates they are an author?
53
- # @return [Boolean]
54
- def author?
55
- roles.any?(&:author?)
56
- end
57
-
58
- # Does this contributor have a role that indicates they are a publisher?
59
- # @return [Boolean]
60
- def publisher?
61
- roles.any?(&:publisher?)
62
- end
63
-
64
- # Does this contributor have any roles defined?
65
- # @return [Boolean]
66
- def role?
67
- roles.any?
68
- end
69
-
70
- # The display name for the contributor as a string.
71
- # Uses the first name if multiple names are present.
72
- # @param with_date [Boolean] Include life dates, if present
73
- # @return [String]
74
- def display_name(with_date: false)
75
- names.map { |name| name.display_str(with_date: with_date) }.first
76
- end
77
-
78
- # A string representation of the contributor's roles, formatted for display.
79
- # If there are multiple roles, they are joined with commas.
80
- # @return [String]
81
- def display_role
82
- roles.map(&:display_str).to_sentence
83
- end
84
-
85
- # All names in the Cocina as Name objects.
86
- # @return [Array<Name>]
87
- def names
88
- @names ||= Array(cocina["name"]).map { |name| Name.new(name) }
89
- end
90
-
91
- # All roles in the Cocina structured data.
92
- # @return [Array<Hash>]
93
- def roles
94
- @roles ||= Array(cocina["role"]).map { |role| Role.new(role) }
95
- end
96
-
97
- # A name associated with a contributor.
98
- class Name
99
- attr_reader :cocina
100
-
101
- # Initialize a Name object with Cocina structured data.
102
- # @param cocina [Hash] The Cocina structured data for the name.
103
- def initialize(cocina)
104
- @cocina = cocina
105
- end
106
-
107
- # The display string for the name, optionally including life dates.
108
- # Uses these values in order, if present:
109
- # 1. Unstructured value
110
- # 2. Any structured/parallel values marked as "display"
111
- # 3. Joined structured values, optionally with life dates
112
- # @param with_date [Boolean] Include life dates, if present
113
- # @return [String]
114
- # @example no dates
115
- # name.display_name # => "King, Martin Luther, Jr."
116
- # @example with dates
117
- # name.display_name(with_date: true) # => "King, Martin Luther, Jr., 1929-1968"
118
- def display_str(with_date: false)
119
- if cocina["value"].present?
120
- cocina["value"]
121
- elsif display_name_str.present?
122
- display_name_str
123
- elsif dates_str.present? && with_date
124
- Utils.compact_and_join([full_name_str, dates_str], delimiter: ", ")
125
- else
126
- full_name_str
127
- end
128
- end
129
-
130
- private
131
-
132
- # The full name as a string, combining all name components.
133
- # @return [String]
134
- def full_name_str
135
- Utils.compact_and_join(name_components, delimiter: ", ")
136
- end
137
-
138
- # Flattened form of any names explicitly marked as "display name".
139
- # @return [String]
140
- def display_name_str
141
- Utils.compact_and_join(Array(name_values["display"]), delimiter: ", ")
142
- end
143
-
144
- # List of all name components.
145
- # If any of forename, surname, or term of address are present, those are used.
146
- # Otherwise, fall back to any names explicitly marked as "name" or untyped.
147
- # @return [Array<String>]
148
- def name_components
149
- [surname_str, forename_ordinal_str, terms_of_address_str].compact_blank.presence || Array(name_values["name"])
150
- end
151
-
152
- # Flatten all forenames and ordinals into a single string.
153
- # @return [String]
154
- def forename_ordinal_str
155
- Utils.compact_and_join(Array(name_values["forename"]) + Array(name_values["ordinal"]), delimiter: " ")
156
- end
157
-
158
- # Flatten all terms of address into a single string.
159
- # @return [String]
160
- def terms_of_address_str
161
- Utils.compact_and_join(Array(name_values["term of address"]), delimiter: ", ")
162
- end
163
-
164
- # Flatten all surnames into a single string.
165
- # @return [String]
166
- def surname_str
167
- Utils.compact_and_join(Array(name_values["surname"]), delimiter: " ")
168
- end
169
-
170
- # Flatten all life and activity dates into a single string.
171
- # @return [String]
172
- def dates_str
173
- Utils.compact_and_join(Array(name_values["life dates"]) + Array(name_values["activity dates"]), delimiter: ", ")
174
- end
175
-
176
- # A hash mapping destructured name types to their values.
177
- # Name values with no type are grouped under "name".
178
- # @return [Hash<String, Array<String>>]
179
- # @see https://github.com/sul-dlss/cocina-models/blob/main/docs/description_types.md#contributor-name-part-types-for-structured-value
180
- # @note Currently we do nothing with "alternative", "inverted full name", "pseudonym", and "transliteration" types.
181
- def name_values
182
- Utils.flatten_nested_values(cocina).each_with_object({}) do |node, hash|
183
- type = node["type"] || "name"
184
- hash[type] ||= []
185
- hash[type] << node["value"]
186
- end.compact_blank
187
- end
188
- end
189
-
190
- # A role associated with a contributor.
191
- class Role
192
- attr_reader :cocina
193
-
194
- # Initialize a Role object with Cocina structured data.
195
- # @param cocina [Hash] The Cocina structured data for the role.
196
- def initialize(cocina)
197
- @cocina = cocina
198
- end
199
-
200
- # The name of the role.
201
- # Translates the MARC relator code if no value was present.
202
- # @return [String, nil]
203
- def display_str
204
- cocina["value"] || (MARC_RELATOR[code] if marc_relator?)
205
- end
206
-
207
- # A code associated with the role, e.g. a MARC relator code.
208
- # @return [String, nil]
209
- def code
210
- cocina["code"]
211
- end
212
-
213
- # Does this role indicate the contributor is an author?
214
- # @return [Boolean]
215
- def author?
216
- display_str =~ /^(author|creator)/i
217
- end
218
-
219
- # Does this role indicate the contributor is a publisher?
220
- # @return [Boolean]
221
- def publisher?
222
- display_str =~ /^publisher/i
223
- end
224
-
225
- private
226
-
227
- # Does this role have a MARC relator code?
228
- # @return [Boolean]
229
- def marc_relator?
230
- cocina.dig("source", "code") == "marcrelator"
231
- end
232
- end
233
- end
234
- end
@@ -1,394 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Represents the Marc Country Codes mapped to names, from http://www.loc.gov/marc/countries/countries_code.html 2013-01-03
4
- # key - Marc Country code
5
- # value - Marc Country term
6
-
7
- # Ported from sul-dlss/mods gem
8
-
9
- module CocinaDisplay
10
- MARC_COUNTRY = {
11
- "aa" => "Albania",
12
- "abc" => "Alberta",
13
- "ac" => "Ashmore and Cartier Islands", # discontinued
14
- "aca" => "Australian Capital Territory",
15
- "ae" => "Algeria",
16
- "af" => "Afghanistan",
17
- "ag" => "Argentina",
18
- "ai" => "Armenia (Republic)",
19
- "air" => "Armenian S.S.R.", # discontinued
20
- "aj" => "Azerbaijan",
21
- "ajr" => "Azerbaijan S.S.R.", # discontinued
22
- "aku" => "Alaska",
23
- "alu" => "Alabama",
24
- "am" => "Anguilla",
25
- "an" => "Andorra",
26
- "ao" => "Angola",
27
- "aq" => "Antigua and Barbuda",
28
- "aru" => "Arkansas",
29
- "as" => "American Samoa",
30
- "at" => "Australia",
31
- "au" => "Austria",
32
- "aw" => "Aruba",
33
- "ay" => "Antarctica",
34
- "azu" => "Arizona",
35
- "ba" => "Bahrain",
36
- "bb" => "Barbados",
37
- "bcc" => "British Columbia",
38
- "bd" => "Burundi",
39
- "be" => "Belgium",
40
- "bf" => "Bahamas",
41
- "bg" => "Bangladesh",
42
- "bh" => "Belize",
43
- "bi" => "British Indian Ocean Territory",
44
- "bl" => "Brazil",
45
- "bm" => "Bermuda Islands",
46
- "bn" => "Bosnia and Herzegovina",
47
- "bo" => "Bolivia",
48
- "bp" => "Solomon Islands",
49
- "br" => "Burma",
50
- "bs" => "Botswana",
51
- "bt" => "Bhutan",
52
- "bu" => "Bulgaria",
53
- "bv" => "Bouvet Island",
54
- "bw" => "Belarus",
55
- "bwr" => "Byelorussian S.S.R.", # discontinued
56
- "bx" => "Brunei",
57
- "ca" => "Caribbean Netherlands",
58
- "cau" => "California",
59
- "cb" => "Cambodia",
60
- "cc" => "China",
61
- "cd" => "Chad",
62
- "ce" => "Sri Lanka",
63
- "cf" => "Congo (Brazzaville)",
64
- "cg" => "Congo (Democratic Republic)",
65
- "ch" => "China (Republic : 1949- )",
66
- "ci" => "Croatia",
67
- "cj" => "Cayman Islands",
68
- "ck" => "Colombia",
69
- "cl" => "Chile",
70
- "cm" => "Cameroon",
71
- "cn" => "Canada", # discontinued
72
- "co" => "Curaçao",
73
- "cou" => "Colorado",
74
- "cp" => "Canton and Enderbury Islands", # discontinued
75
- "cq" => "Comoros",
76
- "cr" => "Costa Rica",
77
- "cs" => "Czechoslovakia", # discontinued
78
- "ctu" => "Connecticut",
79
- "cu" => "Cuba",
80
- "cv" => "Cabo Verde",
81
- "cw" => "Cook Islands",
82
- "cx" => "Central African Republic",
83
- "cy" => "Cyprus",
84
- "cz" => "Canal Zone", # discontinued
85
- "dcu" => "District of Columbia",
86
- "deu" => "Delaware",
87
- "dk" => "Denmark",
88
- "dm" => "Benin",
89
- "dq" => "Dominica",
90
- "dr" => "Dominican Republic",
91
- "ea" => "Eritrea",
92
- "ec" => "Ecuador",
93
- "eg" => "Equatorial Guinea",
94
- "em" => "Timor-Leste",
95
- "enk" => "England",
96
- "er" => "Estonia",
97
- "err" => "Estonia", # discontinued
98
- "es" => "El Salvador",
99
- "et" => "Ethiopia",
100
- "fa" => "Faroe Islands",
101
- "fg" => "French Guiana",
102
- "fi" => "Finland",
103
- "fj" => "Fiji",
104
- "fk" => "Falkland Islands",
105
- "flu" => "Florida",
106
- "fm" => "Micronesia (Federated States)",
107
- "fp" => "French Polynesia",
108
- "fr" => "France",
109
- "fs" => "Terres australes et antarctiques françaises",
110
- "ft" => "Djibouti",
111
- "gau" => "Georgia",
112
- "gb" => "Kiribati",
113
- "gd" => "Grenada",
114
- "ge" => "Germany (East)", # discontinued
115
- "gg" => "Guernsey",
116
- "gh" => "Ghana",
117
- "gi" => "Gibraltar",
118
- "gl" => "Greenland",
119
- "gm" => "Gambia",
120
- "gn" => "Gilbert and Ellice Islands", # discontinued
121
- "go" => "Gabon",
122
- "gp" => "Guadeloupe",
123
- "gr" => "Greece",
124
- "gs" => "Georgia (Republic)",
125
- "gsr" => "Georgian S.S.R.", # discontinued
126
- "gt" => "Guatemala",
127
- "gu" => "Guam",
128
- "gv" => "Guinea",
129
- "gw" => "Germany",
130
- "gy" => "Guyana",
131
- "gz" => "Gaza Strip",
132
- "hiu" => "Hawaii",
133
- "hk" => "Hong Kong", # discontinued
134
- "hm" => "Heard and McDonald Islands",
135
- "ho" => "Honduras",
136
- "ht" => "Haiti",
137
- "hu" => "Hungary",
138
- "iau" => "Iowa",
139
- "ic" => "Iceland",
140
- "idu" => "Idaho",
141
- "ie" => "Ireland",
142
- "ii" => "India",
143
- "ilu" => "Illinois",
144
- "im" => "Isle of Man",
145
- "inu" => "Indiana",
146
- "io" => "Indonesia",
147
- "iq" => "Iraq",
148
- "ir" => "Iran",
149
- "is" => "Israel",
150
- "it" => "Italy",
151
- "iu" => "Israel-Syria Demilitarized Zones", # discontinued
152
- "iv" => "Côte d'Ivoire",
153
- "iw" => "Israel-Jordan Demilitarized Zones", # discontinued
154
- "iy" => "Iraq-Saudi Arabia Neutral Zone",
155
- "ja" => "Japan",
156
- "je" => "Jersey",
157
- "ji" => "Johnston Atoll",
158
- "jm" => "Jamaica",
159
- "jn" => "Jan Mayen", # discontinued
160
- "jo" => "Jordan",
161
- "ke" => "Kenya",
162
- "kg" => "Kyrgyzstan",
163
- "kgr" => "Kirghiz S.S.R.", # discontinued
164
- "kn" => "Korea (North)",
165
- "ko" => "Korea (South)",
166
- "ksu" => "Kansas",
167
- "ku" => "Kuwait",
168
- "kv" => "Kosovo",
169
- "kyu" => "Kentucky",
170
- "kz" => "Kazakhstan",
171
- "kzr" => "Kazakh S.S.R.", # discontinued
172
- "lau" => "Louisiana",
173
- "lb" => "Liberia",
174
- "le" => "Lebanon",
175
- "lh" => "Liechtenstein",
176
- "li" => "Lithuania",
177
- "lir" => "Lithuania", # discontinued
178
- "ln" => "Central and Southern Line Islands", # discontinued
179
- "lo" => "Lesotho",
180
- "ls" => "Laos",
181
- "lu" => "Luxembourg",
182
- "lv" => "Latvia",
183
- "lvr" => "Latvia", # discontinued
184
- "ly" => "Libya",
185
- "mau" => "Massachusetts",
186
- "mbc" => "Manitoba",
187
- "mc" => "Monaco",
188
- "mdu" => "Maryland",
189
- "meu" => "Maine",
190
- "mf" => "Mauritius",
191
- "mg" => "Madagascar",
192
- "mh" => "Macao", # discontinued
193
- "miu" => "Michigan",
194
- "mj" => "Montserrat",
195
- "mk" => "Oman",
196
- "ml" => "Mali",
197
- "mm" => "Malta",
198
- "mnu" => "Minnesota",
199
- "mo" => "Montenegro",
200
- "mou" => "Missouri",
201
- "mp" => "Mongolia",
202
- "mq" => "Martinique",
203
- "mr" => "Morocco",
204
- "msu" => "Mississippi",
205
- "mtu" => "Montana",
206
- "mu" => "Mauritania",
207
- "mv" => "Moldova",
208
- "mvr" => "Moldavian S.S.R.", # discontinued
209
- "mw" => "Malawi",
210
- "mx" => "Mexico",
211
- "my" => "Malaysia",
212
- "mz" => "Mozambique",
213
- "na" => "Netherlands Antilles", # discontinued
214
- "nbu" => "Nebraska",
215
- "ncu" => "North Carolina",
216
- "ndu" => "North Dakota",
217
- "ne" => "Netherlands",
218
- "nfc" => "Newfoundland and Labrador",
219
- "ng" => "Niger",
220
- "nhu" => "New Hampshire",
221
- "nik" => "Northern Ireland",
222
- "nju" => "New Jersey",
223
- "nkc" => "New Brunswick",
224
- "nl" => "New Caledonia",
225
- "nm" => "Northern Mariana Islands", # discontinued
226
- "nmu" => "New Mexico",
227
- "nn" => "Vanuatu",
228
- "no" => "Norway",
229
- "np" => "Nepal",
230
- "nq" => "Nicaragua",
231
- "nr" => "Nigeria",
232
- "nsc" => "Nova Scotia",
233
- "ntc" => "Northwest Territories",
234
- "nu" => "Nauru",
235
- "nuc" => "Nunavut",
236
- "nvu" => "Nevada",
237
- "nw" => "Northern Mariana Islands",
238
- "nx" => "Norfolk Island",
239
- "nyu" => "New York (State)",
240
- "nz" => "New Zealand",
241
- "ohu" => "Ohio",
242
- "oku" => "Oklahoma",
243
- "onc" => "Ontario",
244
- "oru" => "Oregon",
245
- "ot" => "Mayotte",
246
- "pau" => "Pennsylvania",
247
- "pc" => "Pitcairn Island",
248
- "pe" => "Peru",
249
- "pf" => "Paracel Islands",
250
- "pg" => "Guinea-Bissau",
251
- "ph" => "Philippines",
252
- "pic" => "Prince Edward Island",
253
- "pk" => "Pakistan",
254
- "pl" => "Poland",
255
- "pn" => "Panama",
256
- "po" => "Portugal",
257
- "pp" => "Papua New Guinea",
258
- "pr" => "Puerto Rico",
259
- "pt" => "Portuguese Timor", # discontinued
260
- "pw" => "Palau",
261
- "py" => "Paraguay",
262
- "qa" => "Qatar",
263
- "qea" => "Queensland",
264
- "quc" => "Québec (Province)",
265
- "rb" => "Serbia",
266
- "re" => "Réunion",
267
- "rh" => "Zimbabwe",
268
- "riu" => "Rhode Island",
269
- "rm" => "Romania",
270
- "ru" => "Russia (Federation)",
271
- "rur" => "Russian S.F.S.R.", # discontinued
272
- "rw" => "Rwanda",
273
- "ry" => "Ryukyu Islands, Southern", # discontinued
274
- "sa" => "South Africa",
275
- "sb" => "Svalbard", # discontinued
276
- "sc" => "Saint-Barthélemy",
277
- "scu" => "South Carolina",
278
- "sd" => "South Sudan",
279
- "sdu" => "South Dakota",
280
- "se" => "Seychelles",
281
- "sf" => "Sao Tome and Principe",
282
- "sg" => "Senegal",
283
- "sh" => "Spanish North Africa",
284
- "si" => "Singapore",
285
- "sj" => "Sudan",
286
- "sk" => "Sikkim", # discontinued
287
- "sl" => "Sierra Leone",
288
- "sm" => "San Marino",
289
- "sn" => "Sint Maarten",
290
- "snc" => "Saskatchewan",
291
- "so" => "Somalia",
292
- "sp" => "Spain",
293
- "sq" => "Eswatini",
294
- "sr" => "Surinam",
295
- "ss" => "Western Sahara",
296
- "st" => "Saint-Martin",
297
- "stk" => "Scotland",
298
- "su" => "Saudi Arabia",
299
- "sv" => "Swan Islands", # discontinued
300
- "sw" => "Sweden",
301
- "sx" => "Namibia",
302
- "sy" => "Syria",
303
- "sz" => "Switzerland",
304
- "ta" => "Tajikistan",
305
- "tar" => "Tajik S.S.R.", # discontinued
306
- "tc" => "Turks and Caicos Islands",
307
- "tg" => "Togo",
308
- "th" => "Thailand",
309
- "ti" => "Tunisia",
310
- "tk" => "Turkmenistan",
311
- "tkr" => "Turkmen S.S.R.", # discontinued
312
- "tl" => "Tokelau",
313
- "tma" => "Tasmania",
314
- "tnu" => "Tennessee",
315
- "to" => "Tonga",
316
- "tr" => "Trinidad and Tobago",
317
- "ts" => "United Arab Emirates",
318
- "tt" => "Trust Territory of the Pacific Islands", # discontinued
319
- "tu" => "Turkey",
320
- "tv" => "Tuvalu",
321
- "txu" => "Texas",
322
- "tz" => "Tanzania",
323
- "ua" => "Egypt",
324
- "uc" => "United States Misc. Caribbean Islands",
325
- "ug" => "Uganda",
326
- "ui" => "United Kingdom Misc. Islands", # discontinued
327
- "uik" => "United Kingdom Misc. Islands",
328
- "uk" => "United Kingdom", # discontinued
329
- "un" => "Ukraine",
330
- "unr" => "Ukraine", # discontinued
331
- "up" => "United States Misc. Pacific Islands",
332
- "ur" => "Soviet Union", # discontinued
333
- "us" => "United States", # discontinued
334
- "utu" => "Utah",
335
- "uv" => "Burkina Faso",
336
- "uy" => "Uruguay",
337
- "uz" => "Uzbekistan",
338
- "uzr" => "Uzbek S.S.R.", # discontinued
339
- "vau" => "Virginia",
340
- "vb" => "British Virgin Islands",
341
- "vc" => "Vatican City",
342
- "ve" => "Venezuela",
343
- "vi" => "Virgin Islands of the United States",
344
- "vm" => "Vietnam",
345
- "vn" => "Vietnam, North", # discontinued
346
- "vp" => "Various places",
347
- "vra" => "Victoria",
348
- "vs" => "Vietnam, South", # discontinued
349
- "vtu" => "Vermont",
350
- "wau" => "Washington (State)",
351
- "wb" => "West Berlin", # discontinued
352
- "wea" => "Western Australia",
353
- "wf" => "Wallis and Futuna",
354
- "wiu" => "Wisconsin",
355
- "wj" => "West Bank of the Jordan River",
356
- "wk" => "Wake Island",
357
- "wlk" => "Wales",
358
- "ws" => "Samoa",
359
- "wvu" => "West Virginia",
360
- "wyu" => "Wyoming",
361
- "xa" => "Christmas Island (Indian Ocean)",
362
- "xb" => "Cocos (Keeling) Islands",
363
- "xc" => "Maldives",
364
- "xd" => "Saint Kitts-Nevis",
365
- "xe" => "Marshall Islands",
366
- "xf" => "Midway Islands",
367
- "xga" => "Coral Sea Islands Territory",
368
- "xh" => "Niue",
369
- "xi" => "Saint Kitts-Nevis-Anguilla", # discontinued
370
- "xj" => "Saint Helena",
371
- "xk" => "Saint Lucia",
372
- "xl" => "Saint Pierre and Miquelon",
373
- "xm" => "Saint Vincent and the Grenadines",
374
- "xn" => "North Macedonia",
375
- "xna" => "New South Wales",
376
- "xo" => "Slovakia",
377
- "xoa" => "Northern Territory",
378
- "xp" => "Spratly Island",
379
- "xr" => "Czech Republic",
380
- "xra" => "South Australia",
381
- "xs" => "South Georgia and the South Sandwich Islands",
382
- "xv" => "Slovenia",
383
- "xx" => "No place, unknown, or undetermined",
384
- "xxc" => "Canada",
385
- "xxk" => "United Kingdom",
386
- "xxr" => "Soviet Union", # discontinued
387
- "xxu" => "United States",
388
- "ye" => "Yemen",
389
- "ykc" => "Yukon Territory",
390
- "ys" => "Yemen (People's Democratic Republic)", # discontinued
391
- "yu" => "Serbia and Montenegro", # discontinued
392
- "za" => "Zambia"
393
- }.freeze
394
- end