Class: Stanford::Mods::DateParsing

Inherits:
Object
  • Object
show all
Defined in:
lib/stanford-mods/date_parsing.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ DateParsing

Returns a new instance of DateParsing.



14
15
16
# File 'lib/stanford-mods/date_parsing.rb', line 14

def initialize(xml)
  @xml = xml
end

Instance Attribute Details

#xmlObject (readonly)

Returns the value of attribute xml.



12
13
14
# File 'lib/stanford-mods/date_parsing.rb', line 12

def xml
  @xml
end

Class Method Details

.year_int_valid?(year) ⇒ Boolean

true if the year is between -9999 and (current year + 1)

Returns:

  • (Boolean)

    true if the year is between -9999 and (current year + 1); false otherwise



6
7
8
9
10
# File 'lib/stanford-mods/date_parsing.rb', line 6

def self.year_int_valid?(year)
  return false unless year.is_a? Integer

  (year < Date.today.year + 2)
end

Instance Method Details

#date_str_for_displayString?

get display value for year, generally an explicit year or “17th century” or “5 B.C.” or “1950s” or ‘845 A.D.’

Returns:

  • (String, nil)

    String value for year if we could parse one, nil otherwise



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stanford-mods/date_parsing.rb', line 20

def date_str_for_display
  date = xml&.as_object&.date
  date = date.min || date.max if date.is_a?(EDTF::Epoch) || date.is_a?(EDTF::Interval)

  return case xml.as_object.precision
  when :century
    return "#{(date.to_s[0..1].to_i + 1).ordinalize} century"
  when :decade
    return "#{date.year}s"
  when :unknown
    xml.text
  else
    if !self.class.year_int_valid? date.year
      xml.text
    elsif date.year < 1
      "#{date.year.abs + 1} B.C."
    elsif date.year < 1000
      "#{date.year} A.D."
    else
      date.year.to_s
    end
  end
end

#sortable_year_string_from_date_strString?

get String sortable value year if we can parse date_str to get a year.

SearchWorks currently uses a string field for pub date sorting; thus so does Spotlight.
The values returned must *lexically* sort in chronological order, so the B.C. dates are tricky

Returns:

  • (String, nil)

    String sortable year if we could parse one, nil otherwise note that these values must lexically sort to create a chronological sort.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stanford-mods/date_parsing.rb', line 55

def sortable_year_string_from_date_str
  return unless xml&.as_object&.date

  date = xml.as_object.date

  if date.is_a?(EDTF::Interval) && date.from.year < 1
    (-1 * date.from.year - 1000).to_s
  elsif date.is_a?(Date) && date.year < 1
    (-1 * date.year - 1000).to_s
  else
    date.to_s[0..3]&.gsub('X', '-')
  end
end

#year_int_from_date_strInteger?

get Integer year if we can parse date_str to get a year.

Returns:

  • (Integer, nil)

    Integer year if we could parse one, nil otherwise



46
47
48
# File 'lib/stanford-mods/date_parsing.rb', line 46

def year_int_from_date_str
  xml&.as_object&.as_range&.first&.year
end