-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds deposit date into cocina description.
closes #2368
- Loading branch information
1 parent
dadb3de
commit 387080f
Showing
8 changed files
with
713 additions
and
543 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
app/services/cocina_generator/description/date_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module CocinaGenerator | ||
module Description | ||
# Generator for Cocina dates encoded as EDTF | ||
class DateGenerator | ||
# @param [EDTF::*|ActiveSupport::TimeWithZone] date | ||
# @param [type] type for date | ||
# @param [boolean] primary - whether status is primary | ||
# @return [Hash] the props for the date | ||
def self.generate(date:, type: nil, primary: false) | ||
new(date: date, type: type, primary: primary).generate | ||
end | ||
|
||
def initialize(date:, type:, primary:) | ||
@date = date | ||
@type = type | ||
@primary = primary | ||
end | ||
|
||
def generate | ||
{ | ||
encoding: { code: 'edtf' }, | ||
type: type, | ||
status: primary ? 'primary' : nil | ||
}.compact.merge(date_props) | ||
end | ||
|
||
private | ||
|
||
attr_reader :date, :type, :primary | ||
|
||
def date_props | ||
case @date | ||
when EDTF::Interval | ||
interval_props(date) | ||
when ActiveSupport::TimeWithZone | ||
time_with_zone_props(date) | ||
else | ||
edtf_date_props(date) | ||
end | ||
end | ||
|
||
def interval_props(interval_date) | ||
{ | ||
structuredValue: interval_structured_values(interval_date) | ||
}.tap do |props| | ||
if interval_date.from&.uncertain? || interval_date.to&.uncertain? | ||
props[:qualifier] = 'approximate' | ||
props[:structuredValue].each { |struct_date_val| struct_date_val.delete(:qualifier) } | ||
end | ||
end.compact | ||
end | ||
|
||
def interval_structured_values(interval_date) | ||
[].tap do |structured_values| | ||
structured_values << edtf_date_props(interval_date.from, type: 'start') if interval_date.from | ||
structured_values << edtf_date_props(interval_date.to, type: 'end') if interval_date.to | ||
end | ||
end | ||
|
||
def edtf_date_props(edtf_date, type: nil) | ||
{ | ||
qualifier: edtf_date.uncertain? ? 'approximate' : nil, | ||
value: edtf_date.edtf.chomp('?'), | ||
type: type | ||
}.compact | ||
end | ||
|
||
def time_with_zone_props(zone_date) | ||
{ | ||
value: zone_date.strftime('%Y-%m-%d') | ||
} | ||
end | ||
end | ||
end | ||
end |
86 changes: 86 additions & 0 deletions
86
app/services/cocina_generator/description/events_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
module CocinaGenerator | ||
module Description | ||
# This generates Events for a work | ||
class EventsGenerator | ||
def self.generate(work_version:) | ||
new(work_version: work_version).generate | ||
end | ||
|
||
def initialize(work_version:) | ||
@work_version = work_version | ||
end | ||
|
||
def generate | ||
events = deposit_events + Array(created_date_event) | ||
events += publisher_events.presence || Array(published_date_event) | ||
events | ||
end | ||
|
||
private | ||
|
||
attr_reader :work_version | ||
|
||
def publisher_events | ||
@publisher_events ||= ContributorsGenerator.events_from_publisher_contributors(work_version: work_version, | ||
pub_date: published_date_event) | ||
end | ||
|
||
def published_date_event | ||
event_for_date(date: work_version.published_edtf, event_type: 'publication', date_type: 'publication', | ||
primary: true) | ||
end | ||
|
||
def created_date_event | ||
event_for_date(date: work_version.created_edtf, event_type: 'creation', date_type: 'creation') | ||
end | ||
|
||
def deposit_events | ||
return [] if deposit_versions.blank? | ||
|
||
Array(deposit_publication_event) + deposit_modification_events | ||
end | ||
|
||
def deposit_publication_event | ||
event_for_work_version(work_version: deposit_publication_version, event_type: 'deposit', | ||
date_type: 'publication') | ||
end | ||
|
||
def deposit_modification_events | ||
deposit_modification_versions.map do |deposit_version| | ||
event_for_work_version(work_version: deposit_version, event_type: 'deposit', date_type: 'modification') | ||
end | ||
end | ||
|
||
def deposit_versions | ||
# Treating this version as a deposit version | ||
@deposit_versions ||= work_version.work.work_versions.filter do |check_work_version| | ||
check_work_version.deposited? || check_work_version == work_version | ||
end | ||
end | ||
|
||
def deposit_publication_version | ||
deposit_versions.first | ||
end | ||
|
||
def deposit_modification_versions | ||
deposit_versions.slice(1..-1) | ||
end | ||
|
||
def event_for_date(date:, event_type:, date_type:, primary: false) | ||
return unless date | ||
|
||
Cocina::Models::Event.new({ | ||
type: event_type, | ||
date: [DateGenerator.generate(date: date, type: date_type, primary: primary)] | ||
}) | ||
end | ||
|
||
def event_for_work_version(work_version:, event_type:, date_type:) | ||
date = work_version.published_at || work_version.updated_at | ||
event_for_date(date: date, event_type: event_type, date_type: date_type) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.