From 3a8bacd516aa4415db8b864aac175c67e9973ea0 Mon Sep 17 00:00:00 2001 From: Naomi Dushay Date: Thu, 30 Nov 2023 11:27:29 -0800 Subject: [PATCH] split out (imprint) date fields --- config/locales/en.yml | 1 + lib/mods_display.rb | 6 + lib/mods_display/fields/copyright_date.rb | 9 + lib/mods_display/fields/date_captured.rb | 9 + lib/mods_display/fields/date_created.rb | 9 + lib/mods_display/fields/date_issued.rb | 9 + lib/mods_display/fields/date_modified.rb | 9 + lib/mods_display/fields/date_valid.rb | 9 + lib/mods_display/fields/field.rb | 15 + lib/mods_display/html.rb | 6 + spec/fields/copyright_date_spec.rb | 38 +++ spec/fields/date_captured_spec.rb | 38 +++ spec/fields/date_created_spec.rb | 38 +++ spec/fields/date_issued_spec.rb | 38 +++ spec/fields/date_modified_spec.rb | 38 +++ spec/fields/date_valid_spec.rb | 37 +++ spec/fields/field_spec.rb | 318 ++++++++++++++++++++++ spec/fields/imprint_spec.rb | 311 +-------------------- spec/support/origin_info_date_helpers.rb | 67 +++++ 19 files changed, 697 insertions(+), 308 deletions(-) create mode 100644 lib/mods_display/fields/copyright_date.rb create mode 100644 lib/mods_display/fields/date_captured.rb create mode 100644 lib/mods_display/fields/date_created.rb create mode 100644 lib/mods_display/fields/date_issued.rb create mode 100644 lib/mods_display/fields/date_modified.rb create mode 100644 lib/mods_display/fields/date_valid.rb create mode 100644 spec/fields/copyright_date_spec.rb create mode 100644 spec/fields/date_captured_spec.rb create mode 100644 spec/fields/date_created_spec.rb create mode 100644 spec/fields/date_issued_spec.rb create mode 100644 spec/fields/date_modified_spec.rb create mode 100644 spec/fields/date_valid_spec.rb create mode 100644 spec/fields/field_spec.rb create mode 100644 spec/support/origin_info_date_helpers.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 2360498..75947ae 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,6 +17,7 @@ en: creation_production_credits: "Creation/Production credits:" date_captured: "Date captured:" date_created: "Date created:" + date_issued: "Publication date:" date_modified: "Date modified:" date_sequential_designation: "Date/Sequential designation:" date_valid: "Date valid:" diff --git a/lib/mods_display.rb b/lib/mods_display.rb index aea632d..72a1dea 100644 --- a/lib/mods_display.rb +++ b/lib/mods_display.rb @@ -13,7 +13,13 @@ require 'mods_display/fields/collection' require 'mods_display/fields/contact' require 'mods_display/fields/contents' +require 'mods_display/fields/copyright_date' require 'mods_display/fields/cartographics' +require 'mods_display/fields/date_created' +require 'mods_display/fields/date_captured' +require 'mods_display/fields/date_issued' +require 'mods_display/fields/date_modified' +require 'mods_display/fields/date_valid' require 'mods_display/fields/description' require 'mods_display/fields/extent' require 'mods_display/fields/form' diff --git a/lib/mods_display/fields/copyright_date.rb b/lib/mods_display/fields/copyright_date.rb new file mode 100644 index 0000000..a6a9734 --- /dev/null +++ b/lib/mods_display/fields/copyright_date.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ModsDisplay + class CopyrightDate < Field + def fields + date_fields(:copyrightDate) + end + end +end diff --git a/lib/mods_display/fields/date_captured.rb b/lib/mods_display/fields/date_captured.rb new file mode 100644 index 0000000..785b5ec --- /dev/null +++ b/lib/mods_display/fields/date_captured.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ModsDisplay + class DateCaptured < Field + def fields + date_fields(:dateCaptured) + end + end +end diff --git a/lib/mods_display/fields/date_created.rb b/lib/mods_display/fields/date_created.rb new file mode 100644 index 0000000..a51a738 --- /dev/null +++ b/lib/mods_display/fields/date_created.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ModsDisplay + class DateCreated < Field + def fields + date_fields(:dateCreated) + end + end +end diff --git a/lib/mods_display/fields/date_issued.rb b/lib/mods_display/fields/date_issued.rb new file mode 100644 index 0000000..7345866 --- /dev/null +++ b/lib/mods_display/fields/date_issued.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ModsDisplay + class DateIssued < Field + def fields + date_fields(:dateIssued) + end + end +end diff --git a/lib/mods_display/fields/date_modified.rb b/lib/mods_display/fields/date_modified.rb new file mode 100644 index 0000000..49f5274 --- /dev/null +++ b/lib/mods_display/fields/date_modified.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ModsDisplay + class DateModified < Field + def fields + date_fields(:dateModified) + end + end +end diff --git a/lib/mods_display/fields/date_valid.rb b/lib/mods_display/fields/date_valid.rb new file mode 100644 index 0000000..8125fee --- /dev/null +++ b/lib/mods_display/fields/date_valid.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ModsDisplay + class DateValid < Field + def fields + date_fields(:dateValid) + end + end +end diff --git a/lib/mods_display/fields/field.rb b/lib/mods_display/fields/field.rb index d415b03..bcb710a 100644 --- a/lib/mods_display/fields/field.rb +++ b/lib/mods_display/fields/field.rb @@ -58,6 +58,21 @@ def element_text(element) element.xpath('.//text()').to_html.strip end + # used for originInfo date fields, e.g. DateCreated, DateIssued ... + def date_fields(date_symbol) + return_fields = @values.map do |value| + date_values = Stanford::Mods::Imprint.new(value).dates([date_symbol]) + next unless date_values.present? + + ModsDisplay::Values.new( + label: I18n.t("mods_display.#{date_symbol.to_s.underscore}"), + values: select_best_date(date_values), + field: self + ) + end.compact + collapse_fields(return_fields) + end + # used for originInfo dates, e.g. for Imprint, DateCreated, DateIssued, etc. def select_best_date(dates) # ensure dates are unique with respect to their base values diff --git a/lib/mods_display/html.rb b/lib/mods_display/html.rb index 3a87aee..48dd2a1 100644 --- a/lib/mods_display/html.rb +++ b/lib/mods_display/html.rb @@ -11,6 +11,12 @@ class HTML form: :physical_description, extent: :physical_description, geo: :extension, + copyrightDate: :origin_info, + dateCaptured: :origin_info, + dateCreated: :origin_info, + dateIssued: :origin_info, + dateModified: :origin_info, + dateValid: :origin_info, imprint: :origin_info, language: :language, description: :physical_description, diff --git a/spec/fields/copyright_date_spec.rb b/spec/fields/copyright_date_spec.rb new file mode 100644 index 0000000..a8391ac --- /dev/null +++ b/spec/fields/copyright_date_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' + +describe ModsDisplay::CopyrightDate do + let(:many_dates) do + <<~XML + + + created 1725 + captured 1825 + copyright 1925 + issued 2025 + modified 2225 + valid 2125 + + + XML + end + + it 'gets correct label' do + expect(copyright_date_fields(many_dates).first.label).to eq('Copyright date:') + end + + it 'gets contents of copyrightDate field' do + expect(copyright_date_values(many_dates)).to eq(['copyright 1925']) + end + + describe 'to_html' do + it 'has individual dt/dd pairs for date issued' do + html = described_class.new( + Stanford::Mods::Record.new.from_str(many_dates).origin_info + ).to_html + expect(html.scan(%r{
Copyright date
}).length).to eq(1) + expect(html.scan('
').length).to eq(1) + end + end +end diff --git a/spec/fields/date_captured_spec.rb b/spec/fields/date_captured_spec.rb new file mode 100644 index 0000000..1d6ee8d --- /dev/null +++ b/spec/fields/date_captured_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' + +describe ModsDisplay::DateCaptured do + let(:many_dates) do + <<~XML + + + created 1725 + captured 1825 + copyright 1925 + issued 2025 + modified 2225 + valid 2125 + + + XML + end + + it 'gets correct label' do + expect(date_captured_fields(many_dates).first.label).to eq('Date captured:') + end + + it 'gets contents of dateCaptured field' do + expect(date_captured_values(many_dates)).to eq(['captured 1825']) + end + + describe 'to_html' do + it 'has individual dt/dd pairs for capture date' do + html = described_class.new( + Stanford::Mods::Record.new.from_str(many_dates).origin_info + ).to_html + expect(html.scan(%r{
Date captured
}).length).to eq(1) + expect(html.scan('
').length).to eq(1) + end + end +end diff --git a/spec/fields/date_created_spec.rb b/spec/fields/date_created_spec.rb new file mode 100644 index 0000000..e8b009e --- /dev/null +++ b/spec/fields/date_created_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' + +describe ModsDisplay::DateCreated do + let(:many_dates) do + <<~XML + + + created 1725 + captured 1825 + copyright 1925 + issued 2025 + modified 2225 + valid 2125 + + + XML + end + + it 'gets correct label' do + expect(date_created_fields(many_dates).first.label).to eq('Date created:') + end + + it 'gets contents of dateCreated field' do + expect(date_created_values(many_dates)).to eq(['created 1725']) + end + + describe 'to_html' do + it 'has individual dt/dd pairs for create date' do + html = described_class.new( + Stanford::Mods::Record.new.from_str(many_dates).origin_info + ).to_html + expect(html.scan(%r{
Date created
}).length).to eq(1) + expect(html.scan('
').length).to eq(1) + end + end +end diff --git a/spec/fields/date_issued_spec.rb b/spec/fields/date_issued_spec.rb new file mode 100644 index 0000000..cc34b93 --- /dev/null +++ b/spec/fields/date_issued_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' + +describe ModsDisplay::DateIssued do + let(:many_dates) do + <<~XML + + + created 1725 + captured 1825 + copyright 1925 + issued 2025 + modified 2225 + valid 2125 + + + XML + end + + it 'gets correct label' do + expect(date_issued_fields(many_dates).first.label).to eq('Publication date:') + end + + it 'gets contents of dateIssued field' do + expect(date_issued_values(many_dates)).to eq(['issued 2025']) + end + + describe 'to_html' do + it 'has individual dt/dd pairs for date issued' do + html = described_class.new( + Stanford::Mods::Record.new.from_str(many_dates).origin_info + ).to_html + expect(html.scan(%r{
Publication date
}).length).to eq(1) + expect(html.scan('
').length).to eq(1) + end + end +end diff --git a/spec/fields/date_modified_spec.rb b/spec/fields/date_modified_spec.rb new file mode 100644 index 0000000..84edefe --- /dev/null +++ b/spec/fields/date_modified_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' + +describe ModsDisplay::DateModified do + let(:many_dates) do + <<~XML + + + created 1725 + captured 1825 + copyright 1925 + issued 2025 + modified 2225 + valid 2125 + + + XML + end + + it 'gets correct label' do + expect(date_modified_fields(many_dates).first.label).to eq('Date modified:') + end + + it 'gets contents of dateModified field' do + expect(date_modified_values(many_dates)).to eq(['modified 2225']) + end + + describe 'to_html' do + it 'has individual dt/dd pairs for modified date' do + html = described_class.new( + Stanford::Mods::Record.new.from_str(many_dates).origin_info + ).to_html + expect(html.scan(%r{
Date modified
}).length).to eq(1) + expect(html.scan('
').length).to eq(1) + end + end +end diff --git a/spec/fields/date_valid_spec.rb b/spec/fields/date_valid_spec.rb new file mode 100644 index 0000000..0d121ef --- /dev/null +++ b/spec/fields/date_valid_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' + +describe ModsDisplay::DateValid do + let(:many_dates) do + <<~XML + + + created 1725 + captured 1825 + copyright 1925 + issued 2025 + valid 2125 + + + XML + end + + it 'gets correct label' do + expect(date_valid_fields(approximate_date).first.label).to eq('Date valid:') + end + + it 'gets contents of dateValid field' do + expect(date_valid_values(many_dates)).to eq(['valid 2125']) + end + + describe 'to_html' do + it 'has individual dt/dd pairs for valid date' do + html = described_class.new( + Stanford::Mods::Record.new.from_str(many_dates).origin_info + ).to_html + expect(html.scan(%r{
Date valid
}).length).to eq(1) + expect(html.scan('
').length).to eq(1) + end + end +end diff --git a/spec/fields/field_spec.rb b/spec/fields/field_spec.rb new file mode 100644 index 0000000..dab0940 --- /dev/null +++ b/spec/fields/field_spec.rb @@ -0,0 +1,318 @@ +# frozen_string_literal: true + +require 'support/origin_info_date_helpers' +require 'fixtures/imprint_fixtures' + +describe ModsDisplay::Field do + include ImprintFixtures + + describe '#date_fields' do + context 'when the date field name is unrecognized' do + it 'is ignored by mods_display (but likely not stanford-mods)' do + wacko_date = <<~XML + + + 1825 + + + XML + expect(date_created_values(wacko_date)).to be_empty + end + end + + context 'when no originInfo fields' do + it 'is empty' do + no_origin_info = <<~XML + + foo + + XML + expect(date_created_values(no_origin_info)).to be_empty + end + end + + describe 'labels' do + it 'gets the labels for origin info date fields' do + fields = date_created_fields(no_edition_mods) + expect(fields.map(&:label)).to eq(['Date created:']) + fields = date_captured_fields(mixed_mods) + expect(fields.map(&:label)).to eq(['Date captured:']) + end + + it 'looks up and uses the I18n label for the date field' do + expect(I18n).to receive(:t).with('mods_display.date_created').and_call_original + expect(date_created_fields(no_edition_mods).first.label).to eq('Date created:') + # expect(date_issued_fields(imprint_mods).first.label).to eq('Date issued:') + end + end + + context 'when simple date' do + it 'gets the raw value' do + simple_date = <<~XML + + + a raw cold 2934 + + + XML + expect(date_created_values(simple_date)).to eq(['a raw cold 2934']) + end + end + + context 'when qualifier decoration' do + it "prepends 'ca. ' to approximate date and wraps it in square brackets" do + expect(date_valid_values(approximate_date)).to eq(['[ca. 1820]']) + end + + it "appends '?' to questionable date and wraps it in square brackets" do + expect(date_valid_values(questionable_date)).to eq(['[1820?]']) + end + + it 'wraps inferred dates in square-brackets' do + expect(date_valid_values(inferred_date)).to eq(['[1820]']) + end + end + + context 'when multiple values' do + it 'prefer qualified date' do + expect(date_created_values(dup_qualified_date)).to eq(['[1820?]']) + end + + it 'prefer non-encoded date' do + unencoded_and_encoded = <<~XML + + + 1820 + [ca. 1820] + + + XML + expect(date_created_values(unencoded_and_encoded)).to eq(['[ca. 1820]']) + end + + it 'prefer c copyright date' do + expect(date_created_values(dup_copyright_date)).to eq(['c1820']) + end + + it 'only return one when no attributes are present' do + expect(date_created_values(dup_date)).to eq(['1820']) + end + end + + describe 'when encoded' do + context 'when edtf' do + it 'transforms year zero dates to 1 BCE' do + year_zero_date = <<~XML + + + 0 + + + XML + expect(date_created_values(year_zero_date)).to eq ['1 BCE'] + end + end + + context 'when W3CDTF' do + it 'single year' do + single_year_date = <<~XML + + + 2013 + + + XML + expect(date_created_values(single_year_date)).to eq ['2013'] + end + + it 'yyyy-mm value becomes month year' do + month_year = <<~XML + + + 2013-07 + + + XML + expect(date_created_values(month_year)).to eq ['July 2013'] + end + + it 'yyyy-mm-dd value becomes month day, year' do + year_month_day = <<~XML + + + 2013-07-10 + + + XML + expect(date_created_values(year_month_day)).to eq ['July 10, 2013'] + end + + it 'uses raw value for unparsable value' do + unparseable = <<~XML + + + Jul. 22, 2013 + + + XML + expect(date_created_values(unparseable)).to eq ['Jul. 22, 2013'] + end + end + + context 'when iso8601' do + it 'handles full dates properly' do + expect(date_created_values(iso8601_encoded_dates)).to eq(['November 14, 2013']) + end + + it 'uses raw value for unparsable value' do + expect(date_modified_values(iso8601_encoded_dates)).to eq(['Jul. 32, 2013']) + end + end + end + + context 'when bad date' do + it 'empty value is ignored' do + empty_date = <<~XML + + + + + + XML + expect(date_created_fields(empty_date)).to be_empty + end + + it '9999 is ignored' do + expect(date_modified_values(bad_dates)).to be_empty + end + + it 'impossible value returns the raw value' do + expect(date_modified_values(invalid_dates)).to eq(['1920-09-32']) + end + + it 'value 0000 returns 1 BCE' do + zeroes_date = <<~XML + + + 0000 + + + XML + expect(date_created_values(zeroes_date)).to eq ['1 BCE'] + end + + it 'ccxx becomes cc+1(th) century' do + non_integer_date = <<~XML + + + 19xx + + + XML + expect(date_created_values(non_integer_date)).to eq ['20th century'] + end + + it 'renders BC ranges' do + bc_range = <<~XML + + + -0249 + -0099 + + + XML + expect(date_created_values(bc_range)).to eq ['[ca. 250 BCE - 100 BCE]'] + end + end + + describe 'punctuation' do + # dateIssued + it 'trailing period (thank you MARC) is left alone' do + trailing_period = <<~XML + + + 2015. + + + XML + expect(date_created_values(trailing_period)).to eq(['2015.']) + end + end + + context 'when date range' do + it "joins start and end point ranges with a '-'" do + date_range = <<~XML + + + 1825 + 1820 + + + XML + expect(date_created_values(date_range)).to eq(['1820 - 1825']) + end + + it 'are open, handles properly' do + open_range = <<~XML + + + 1820 + + + XML + expect(date_created_values(open_range)).to eq(['1820 - ']) + end + + it '4 elements of the same date within a range' do + four_elements = <<~XML + + + [1820-1825] + 1824 + 1820 + 1825 + + + XML + expect(date_created_values(four_elements)).to eq(['[1820 - 1825?]', '1824']) + end + + it 'with qualifier for a single date' do + expect(date_issued_values(qualified_imprint_date)).to eq(['[1820?]']) + end + + it 'with qualifier on two dates' do + separate_qualifier_range = <<~XML + + + 1880 + 1906 + + + XML + expect(date_created_values(separate_qualifier_range)).to eq ['[ca. 1880] - 1906'] + end + + it 'plus single date in range' do + date_range = <<~XML + + + [1820] + 1820 + 1825 + + + XML + expect(date_created_values(date_range)).to eq ['1820 - 1825'] + end + + it 'with encoded dates' do + expect(date_created_values(encoded_date_range)).to eq ['February 1, 2008 - December 2, 2009'] + end + + it 'with BCE and CE dates' do + expect(date_created_values(bc_ad_imprint_date_fixture)).to eq ['14 BCE - 44 CE'] + end + end + end +end diff --git a/spec/fields/imprint_spec.rb b/spec/fields/imprint_spec.rb index ffd0a96..11afb19 100644 --- a/spec/fields/imprint_spec.rb +++ b/spec/fields/imprint_spec.rb @@ -23,7 +23,7 @@ def mods_display_imprint(mods_text) expect(fields.last.label).to eq('Issuance:') end - it 'gets multiple labels when we have mixed content' do + it 'gets multiple labels when we have multiple imprint statements' do expect(mods_display_imprint(mixed_mods).fields.map(&:label)).to eq(['Imprint:', 'Date captured:', 'Issuance:']) end @@ -51,7 +51,7 @@ def mods_display_imprint(mods_text) expect(fields.last.values).to eq(['The Issuance']) end - it 'handles mixed mods properly' do + it 'handles multiple imprint statements properly' do values = mods_display_imprint(mixed_mods).fields expect(values.length).to eq(3) expect(values.map(&:values)).to include(['A Place : A Publisher']) @@ -60,311 +60,6 @@ def mods_display_imprint(mods_text) end end - describe 'date processing' do - describe 'ranges' do - it "joins start and end point ranges with a '-'" do - date_range = <<-MODS - - - 1825 - 1820 - - - MODS - - fields = mods_display_imprint(date_range).fields - expect(fields.first.values).to eq(['1820 - 1825']) - end - - it 'handles open ranges properly' do - open_date_range = <<-MODS - - - 1820 - - - MODS - - fields = mods_display_imprint(open_date_range).fields - expect(fields.first.values).to eq(['1820 - ']) - end - - it 'handles when there are more than 3 of the same date w/i a range' do - three_imprint_dates = <<-MODS - - - [1820-1825] - 1820 - 1825 - - - MODS - - fields = mods_display_imprint(three_imprint_dates).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[1820 - 1825?]']) - end - - it 'applies the qualifier decoration in the imprints' do - fields = mods_display_imprint(qualified_imprint_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[1820?]']) - end - - it 'applies the qualifier decoration separately to each date' do - separate_qualifier_range = <<-MODS - - - 1880 - 1906 - - - MODS - - fields = mods_display_imprint(separate_qualifier_range).fields - expect(fields.first.values).to eq ['[ca. 1880] - 1906'] - end - - it 'handles date ranges in imprints' do - imprint_date_range = <<-MODS - - - [1820] - 1820 - 1825 - - - MODS - - fields = mods_display_imprint(imprint_date_range).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['1820 - 1825']) - end - - it 'handles encoded dates properly' do - fields = mods_display_imprint(encoded_date_range).fields - expect(fields.length).to eq 1 - expect(fields.first.values).to eq ['February 1, 2008 - December 2, 2009'] - end - - it 'handles BCE and CE dates appropriately' do - fields = mods_display_imprint(bc_ad_imprint_date_fixture).fields - expect(fields.length).to eq 1 - expect(fields.first.values).to eq ['14 BCE - 44 CE'] - end - end - - describe 'duplication' do - it 'onlies return the qualified date when present' do - fields = mods_display_imprint(dup_qualified_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[1820?]']) - end - - it 'uses the non-encoded date when present' do - dup_unencoded_date = <<-MODS - - - 1820 - [ca. 1820] - - - MODS - fields = mods_display_imprint(dup_unencoded_date).fields - - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[ca. 1820]']) - end - - it 'handles copyright dates correctly' do - fields = mods_display_imprint(dup_copyright_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['c1820']) - end - - it 'onlies return one when no attributes are present' do - fields = mods_display_imprint(dup_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['1820']) - end - end - - describe 'qualifier decoration' do - it "prepends a 'ca.' to approximate dates" do - fields = mods_display_imprint(approximate_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[ca. 1820]']) - end - - it "appends a '?' to a questionable dates and wrap them in square-brackets" do - fields = mods_display_imprint(questionable_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[1820?]']) - end - - it 'wraps inferred dates in square-brackets' do - fields = mods_display_imprint(inferred_date).fields - expect(fields.length).to eq(1) - expect(fields.first.values).to eq(['[1820]']) - end - end - - describe 'encoded dates' do - it 'transforms year zero dates to 1 BCE' do - year_zero_date = <<-MODS - - - 0 - - - MODS - - fields = mods_display_imprint(year_zero_date).fields - expect(fields.first.values).to eq ['1 BCE'] - end - - describe 'W3CDTF' do - it 'handles single year dates properly' do - single_year_date = <<-MODS - - - 2013 - - - MODS - fields = mods_display_imprint(single_year_date).fields - expect(fields.first.values).to eq ['2013'] - end - - it 'handles month+year dates properly' do - month_year_date = <<-MODS - - - 2013-07 - - - MODS - - fields = mods_display_imprint(month_year_date).fields - expect(fields.first.values).to eq ['July 2013'] - end - - it 'handles full precision dates properly' do - full_precision_date = <<-MODS - - - 2013-07-10 - - - MODS - - fields = mods_display_imprint(full_precision_date).fields - expect(fields.first.values).to eq ['July 10, 2013'] - end - - it "does not try to handle dates we can't parse" do - unparseable_date = <<-MODS - - - Jul. 22, 2013 - - - MODS - - fields = mods_display_imprint(unparseable_date).fields - expect(fields.first.values).to eq ['Jul. 22, 2013'] - end - end - - describe 'iso8601' do - it 'handles full dates properly' do - fields = mods_display_imprint(iso8601_encoded_dates).fields - expect(fields.length).to eq(2) - expect(fields.find do |field| - field.label == 'Date created:' - end.values).to eq(['November 14, 2013']) - end - - it "does not try to handle dates we can't parse" do - fields = mods_display_imprint(iso8601_encoded_dates).fields - expect(fields.length).to eq(2) - expect(fields.find do |field| - field.label == 'Date modified:' - end.values).to eq(['Jul. 32, 2013']) - end - end - end - - describe 'bad dates' do - it 'ignores date values' do - fields = mods_display_imprint(bad_dates).fields - expect(fields.length).to eq(1) - fields.each do |field| - expect(field.values.join).not_to include '9999' - end - end - - it 'ignores blank date values' do - empty_date = <<-MODS - - - - - - MODS - - fields = mods_display_imprint(empty_date).fields - expect(fields).to be_empty - end - - it 'handles invalid dates by returning the original value' do - fields = mods_display_imprint(invalid_dates).fields - expect(fields.length).to eq(2) - expect(fields.last.values).to eq(['1920-09-32']) - end - - it 'marks dates consisting solely of zeroes as 1 BCE' do - zeroes_date = <<-MODS - - - 0000 - - - MODS - - fields = mods_display_imprint(zeroes_date).fields - expect(fields.first.values).to eq ['1 BCE'] - end - - it 'does not append CE to dates that are not integers' do - non_integer_date = <<-MODS - - - 19xx - - - MODS - - fields = mods_display_imprint(non_integer_date).fields - expect(fields.first.values).to eq ['20th century'] - end - - it 'renders BC ranges' do - bc_range = <<-MODS - - - -0249 - -0099 - - - MODS - - fields = mods_display_imprint(bc_range).fields - expect(fields.first.values).to eq ['[ca. 250 BCE - 100 BCE]'] - end - end - end - describe 'punctuation' do it 'does not duplicate punctuation' do fields = mods_display_imprint(punctuation_imprint_fixture).fields @@ -394,7 +89,7 @@ def mods_display_imprint(mods_text) end describe 'to_html' do - it 'has individual dt/dd pairs for mixed content' do + it 'has individual dt/dd pairs for multiple imprint statements' do html = mods_display_imprint(mixed_mods).to_html expect(html.scan(%r{
Imprint
}).length).to eq(1) expect(html.scan(%r{
Issuance
}).length).to eq(1) diff --git a/spec/support/origin_info_date_helpers.rb b/spec/support/origin_info_date_helpers.rb new file mode 100644 index 0000000..1a0b52b --- /dev/null +++ b/spec/support/origin_info_date_helpers.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +def copyright_date_fields(mods_text) + ModsDisplay::CopyrightDate.new( + Stanford::Mods::Record.new.from_str(mods_text).origin_info + ).fields +end + +def copyright_date_values(mods_text) + # without flatten, this returns an outer array for fields and an inner array for values + copyright_date_fields(mods_text).map(&:values).flatten +end + +def date_captured_fields(mods_text) + ModsDisplay::DateCaptured.new( + Stanford::Mods::Record.new.from_str(mods_text).origin_info + ).fields +end + +def date_captured_values(mods_text) + # without flatten, this returns an outer array for fields and an inner array for values + date_captured_fields(mods_text).map(&:values).flatten +end + +def date_created_fields(mods_text) + ModsDisplay::DateCreated.new( + Stanford::Mods::Record.new.from_str(mods_text).origin_info + ).fields +end + +def date_created_values(mods_text) + # without flatten, this returns an outer array for fields and an inner array for values + date_created_fields(mods_text).map(&:values).flatten +end + +def date_issued_fields(mods_text) + ModsDisplay::DateIssued.new( + Stanford::Mods::Record.new.from_str(mods_text).origin_info + ).fields +end + +def date_issued_values(mods_text) + # without flatten, this returns an outer array for fields and an inner array for values + date_issued_fields(mods_text).map(&:values).flatten +end + +def date_modified_fields(mods_text) + ModsDisplay::DateModified.new( + Stanford::Mods::Record.new.from_str(mods_text).origin_info + ).fields +end + +def date_modified_values(mods_text) + # without flatten, this returns an outer array for fields and an inner array for values + date_modified_fields(mods_text).map(&:values).flatten +end + +def date_valid_fields(mods_text) + ModsDisplay::DateValid.new( + Stanford::Mods::Record.new.from_str(mods_text).origin_info + ).fields +end + +def date_valid_values(mods_text) + # without flatten, this returns an outer array for fields and an inner array for values + date_valid_fields(mods_text).map(&:values).flatten +end