module Krikri::Enrichments ## # Splits edm:TimeSpan labels and populates `#begin` and `#end`. Any values # generated from string values are left as strings to be normalized by # another enrichment. # # Ignores values that are neither a String or an edm:TimeSpan # # Converts string values to a timespan with the value as providedLabel # and enriches as below. # # Populates the `#providedLabel`, `#begin`, and `#end` properties of TimeSpan # objects, and: # - If a `#providedLabel` is present, but begin and end are missing: # - Attempts to parse `#providedLabel` as an EDTF interval and populates # begin and end with their respective values. # - Splits dates # - If no # - Returns the TimeSpan unaltered if all three fields are present. class TimespanSplit include Krikri::FieldEnrichment def enrich_value(value) value = timespan_from_string(value) if value.is_a? String return value unless value.is_a? DPLA::MAP::TimeSpan populate_timespan(value) end def timespan_from_string(value) timespan = DPLA::MAP::TimeSpan.new timespan.providedLabel = value timespan end def populate_timespan(timespan) return timespan unless timespan.providedLabel.empty? || timespan.begin.empty? || timespan.end.empty? parse_label(timespan.providedLabel) return timespan end ## # e.g. 1970-08-01/02 or 1970-12/10 def partial_edtf(str) /^(\d{4}(-\d{2})*)-(\d{2})\/(\d{2})$/.match(str) do |m| Date.edtf("#{m[1]}-#{m[3]}/#{m[1]}-#{m[4]}") end end ## # e.g. 01-2045 def month_year(str) /\d{2}-(\d{4})/.match(str) do |m| Date.edtf("#{m[2]}-#{m[1]}") end end ## # e.g. 1990-92 def hyphenated_partial_range(str) /^(\d{2})(\d{2})-(\d{2})$/.match(str) do |m| Date.edtf("#{m[1]}#{m[2]}/#{m[1]}#{m[3]}") end end ## # e.g. 1990s def decade_s(str) /^(\d{3})0s)$/.match(str) do |m| Date.edtf("#{m}x") end end ## # e.g. 199- def decade_hyphen /^(\d{3})-)$/.match(str) do |m| Date.edtf("#{m}x") end end end end