Class: Mods::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/mods/date.rb

Defined Under Namespace

Classes: CenturyFormat, EdtfFormat, EmbeddedYearFormat, ExtractorDateFormat, Iso8601Format, MMDDYYFormat, MMDDYYYYFormat, MarcFormat, MysteryCenturyFormat, RomanNumeralCenturyFormat, RomanNumeralYearFormat, W3cdtfFormat

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dateObject (readonly)

Returns the value of attribute date.



194
195
196
# File 'lib/mods/date.rb', line 194

def date
  @date
end

#xmlObject (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

Parameters:

  • text (String)

Returns:

  • (String)


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.

Parameters:

  • xml (Nokogiri::XML::Element)

    A date-flavored MODS field from the XML

Returns:



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

Parameters:

  • text (String)

Returns:



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?

Returns:

  • (Boolean)


320
321
322
# File 'lib/mods/date.rb', line 320

def approximate?
  qualifier == 'approximate'
end

#as_rangeObject

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)

Parameters:

  • (Range)


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

#encodingString

The declared encoding of date (from the MODS @encoding attribute)

Returns:

  • (String)


264
265
266
# File 'lib/mods/date.rb', line 264

def encoding
  xml.attr(:encoding)
end

#encoding?Boolean

Was an encoding provided?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


328
329
330
# File 'lib/mods/date.rb', line 328

def inferred?
  qualifier == 'inferred'
end

#pointString

The declared point of date (from the MODS @point attribute)

Returns:

  • (String)


280
281
282
# File 'lib/mods/date.rb', line 280

def point
  xml.attr(:point)
end

#qualifierString

The declared qualifier of date (from the MODS @qualifier attribute)

Returns:

  • (String)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


296
297
298
# File 'lib/mods/date.rb', line 296

def start?
  point == 'start'
end

#textString

The text as encoded in the MODS

Returns:

  • (String)


248
249
250
# File 'lib/mods/date.rb', line 248

def text
  xml.text
end

#to_aArray

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

Returns:

  • (Array)


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

#typeString

The declared type of date (from the MODS @type attribute)

Returns:

  • (String)


256
257
258
# File 'lib/mods/date.rb', line 256

def type
  xml.attr(:type)
end