Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the explicit superclass from monkey-patch #1472

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions lib/active_fedora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@
# Monkey patching RDF::Literal::DateTime to support fractional seconds.
# See https://github.com/samvera/active_fedora/issues/497
# Also monkey patches in a fix for timezones to be stored properly.
#
# RDF 3.2.5 changes the superclass of DateTime to RDF::Temporal
# TODO: Determine if this monkey-patch is needed even with RDF pre-3.2.5 (All the tests pass without it)
if RDF::Literal::DateTime.superclass == RDF::Literal
module RDF
class Literal
class DateTime < Literal
ALTERNATIVE_FORMAT = '%Y-%m-%dT%H:%M:%S'.freeze
DOT = '.'.freeze
EMPTY = ''.freeze
TIMEZONE_FORMAT = '%:z'.freeze

def to_s
@string ||= begin
# Show nanoseconds but remove trailing zeros
nano = @object.strftime('%N').sub(/0+\Z/, EMPTY)
nano = DOT + nano unless nano.blank?
@object.strftime(ALTERNATIVE_FORMAT) + nano + @object.strftime(TIMEZONE_FORMAT)
end
end
# This is needed in both RDF <= 3.2.4 and RDF >= 3.2.5
# TODO: Figure out how to contribute something upstream to avoid monkey-patching
module RDF
class Literal
class DateTime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see the suggestion to remove < Literal worked.

ALTERNATIVE_FORMAT = '%Y-%m-%dT%H:%M:%S'.freeze
DOT = '.'.freeze
EMPTY = ''.freeze
TIMEZONE_FORMAT = '%:z'.freeze

def to_s
@string ||= begin
# Show nanoseconds but remove trailing zeros
nano = @object.strftime('%N').sub(/0+\Z/, EMPTY)
nano = DOT + nano unless nano.blank?
@object.strftime(ALTERNATIVE_FORMAT) + nano + @object.strftime(TIMEZONE_FORMAT)
end
end
end
end
Expand Down
26 changes: 25 additions & 1 deletion spec/integration/date_time_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,33 @@ class Foo < ActiveFedora::Base

describe 'serializing' do
let(:object) { Foo.new(date: [date]) }
let(:triple) { object.resource.query(predicate: ::RDF::Vocab::DC.date).to_a.first }
let(:triple) { object.resource.query(predicate: ::RDF::Vocab::DC.date).to_a.first.object }
it 'time zone must have semicolin to be a cannonical XMLSchema#dateTime' do
expect(triple.to_s).to match(/\+01:00/)
end

context 'nanoseconds' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice coverage of possible conditions.

let(:date) { DateTime.parse("2015-10-22T10:20:03.653991025+01:00") }

it 'includes nanosecond precision' do
expect(triple.to_s).to eq "2015-10-22T10:20:03.653991025+01:00"
end

context 'with trailing zeros' do
let(:date) { DateTime.parse("2015-10-22T15:34:20.97800000-11:00") }

it 'trims trailing zeros' do
expect(triple.to_s).to eq "2015-10-22T15:34:20.978-11:00"
end

context 'and only zero nanoseconds' do
let(:date) { DateTime.parse("2015-10-22T15:34:20.000000000-11:00") }

it 'trims trailing zeros and dot if nanoseconds are all 0' do
expect(triple.to_s).to eq "2015-10-22T15:34:20-11:00"
end
end
end
end
end
end