Class: Mods::Date
- Inherits:
-
Object
- Object
- Mods::Date
- Defined in:
- lib/mods/date.rb
Direct Known Subclasses
EdtfFormat, ExtractorDateFormat, Iso8601Format, W3cdtfFormat
Defined Under Namespace
Classes: CenturyFormat, EdtfFormat, EmbeddedYearFormat, ExtractorDateFormat, Iso8601Format, MMDDYYFormat, MMDDYYYYFormat, MarcFormat, MysteryCenturyFormat, RomanNumeralCenturyFormat, RomanNumeralYearFormat, W3cdtfFormat
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Class Method Summary collapse
-
.cleanup(text) ⇒ String
Apply any encoding-specific munging or text extraction logic.
-
.from_element(xml) ⇒ Mods::Date
Ugly date factory that tries to pick an appropriate parser for the type of data.
-
.parse_date(text) ⇒ Date
Parse a string to a Date or EDTF::Date using rules appropriate to the given encoding.
Instance Method Summary collapse
-
#approximate? ⇒ Boolean
Is the date declared as an approximate date?.
-
#as_range ⇒ Object
Return a range, with the min point as the earliest possible date and the max as the latest possible date (useful particularly for ranges and uncertainty).
-
#encoding ⇒ String
The declared encoding of date (from the MODS @encoding attribute).
-
#encoding? ⇒ Boolean
Was an encoding provided?.
-
#end? ⇒ Boolean
Is this date the end point of a MODS-encoded range?.
-
#inferred? ⇒ Boolean
Is the date declared as an inferred date?.
-
#initialize(xml) ⇒ Date
constructor
A new instance of Date.
-
#point ⇒ String
The declared point of date (from the MODS @point attribute).
-
#qualifier ⇒ String
The declared qualifier of date (from the MODS @qualifier attribute).
-
#questionable? ⇒ Boolean
Is the date declared as a questionable date?.
-
#single? ⇒ Boolean
Is this date stand-alone, or part of a MODS-encoded range?.
-
#start? ⇒ Boolean
Is this date the start of a MODS-encoded range?.
-
#text ⇒ String
The text as encoded in the MODS.
-
#to_a ⇒ Array
Return an array of all years that fall into the range of possible dates covered by the data.
-
#type ⇒ String
The declared type of date (from the MODS @type attribute).
Constructor Details
#initialize(xml) ⇒ Date
Returns a new instance of Date.
213 214 215 216 |
# File 'lib/mods/date.rb', line 213 def initialize(xml) @xml = xml @date = self.class.parse_date(xml.text) end |
Instance Attribute Details
#date ⇒ Object (readonly)
Returns the value of attribute date.
194 195 196 |
# File 'lib/mods/date.rb', line 194 def date @date end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
5 6 7 |
# File 'lib/mods/date.rb', line 5 def xml @xml end |
Class Method Details
.cleanup(text) ⇒ String
Apply any encoding-specific munging or text extraction logic
209 210 211 |
# File 'lib/mods/date.rb', line 209 def self.cleanup(text) text.gsub(/^[\[]+/, '').gsub(/[\.\]]+$/, '') end |
.from_element(xml) ⇒ Mods::Date
Ugly date factory that tries to pick an appropriate parser for the type of data.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mods/date.rb', line 13 def self.from_element(xml) case xml.attr(:encoding) when 'w3cdtf' Mods::Date::W3cdtfFormat.new(xml) when 'iso8601' Mods::Date::Iso8601Format.new(xml) when 'marc' Mods::Date::MarcFormat.new(xml) when 'edtf' Mods::Date::EdtfFormat.new(xml) # when 'temper' # Mods::Date::TemperFormat.new(xml) else date_class = [ MMDDYYYYFormat, MMDDYYFormat, EmbeddedYearFormat, RomanNumeralCenturyFormat, RomanNumeralYearFormat, MysteryCenturyFormat, CenturyFormat ].select { |klass| klass.supports? xml.text }.first (date_class || Mods::Date).new(xml) end rescue Mods::Date.new(xml) end |
.parse_date(text) ⇒ Date
Parse a string to a Date or EDTF::Date using rules appropriate to the given encoding
201 202 203 |
# File 'lib/mods/date.rb', line 201 def self.parse_date(text) ::Date.edtf(cleanup(text)) end |
Instance Method Details
#approximate? ⇒ Boolean
Is the date declared as an approximate date?
320 321 322 |
# File 'lib/mods/date.rb', line 320 def approximate? qualifier == 'approximate' end |
#as_range ⇒ Object
Return a range, with the min point as the earliest possible date and the max as the latest possible date (useful particularly for ranges and uncertainty)
223 224 225 226 227 |
# File 'lib/mods/date.rb', line 223 def as_range return unless earliest_date && latest_date earliest_date..latest_date end |
#encoding ⇒ String
The declared encoding of date (from the MODS @encoding attribute)
264 265 266 |
# File 'lib/mods/date.rb', line 264 def encoding xml.attr(:encoding) end |
#encoding? ⇒ Boolean
Was an encoding provided?
272 273 274 |
# File 'lib/mods/date.rb', line 272 def encoding? !encoding.nil? end |
#end? ⇒ Boolean
Is this date the end point of a MODS-encoded range?
304 305 306 |
# File 'lib/mods/date.rb', line 304 def end? point == 'end' end |
#inferred? ⇒ Boolean
Is the date declared as an inferred date?
328 329 330 |
# File 'lib/mods/date.rb', line 328 def inferred? qualifier == 'inferred' end |
#point ⇒ String
The declared point of date (from the MODS @point attribute)
280 281 282 |
# File 'lib/mods/date.rb', line 280 def point xml.attr(:point) end |
#qualifier ⇒ String
The declared qualifier of date (from the MODS @qualifier attribute)
312 313 314 |
# File 'lib/mods/date.rb', line 312 def qualifier xml.attr(:qualifier) end |
#questionable? ⇒ Boolean
Is the date declared as a questionable date?
336 337 338 |
# File 'lib/mods/date.rb', line 336 def questionable? qualifier == 'questionable' end |
#single? ⇒ Boolean
Is this date stand-alone, or part of a MODS-encoded range?
288 289 290 |
# File 'lib/mods/date.rb', line 288 def single? point.nil? end |
#start? ⇒ Boolean
Is this date the start of a MODS-encoded range?
296 297 298 |
# File 'lib/mods/date.rb', line 296 def start? point == 'start' end |
#text ⇒ String
The text as encoded in the MODS
248 249 250 |
# File 'lib/mods/date.rb', line 248 def text xml.text end |
#to_a ⇒ Array
Return an array of all years that fall into the range of possible dates covered by the data. Note that some encodings support disjoint sets of ranges so this method could provide more accuracy than #as_range (although potentially) include a really big list of dates
236 237 238 239 240 241 242 243 |
# File 'lib/mods/date.rb', line 236 def to_a case date when EDTF::Set date.to_a else as_range.to_a end end |
#type ⇒ String
The declared type of date (from the MODS @type attribute)
256 257 258 |
# File 'lib/mods/date.rb', line 256 def type xml.attr(:type) end |