Class: CocinaDisplay::Dates::RomanNumeralYearFormat
- Inherits:
-
ExtractorDateFormat
- Object
- Date
- ExtractorDateFormat
- CocinaDisplay::Dates::RomanNumeralYearFormat
- Defined in:
- lib/cocina_display/dates/date.rb
Overview
Extractor for dates encoded as Roman numerals.
Direct Known Subclasses
Constant Summary collapse
- REGEX =
/(?<![A-Za-z\.])(?<year>[MCDLXVI\.]+)(?![A-Za-z])/
Constants inherited from Date
Date::BCE_CHAR_SORT_MAP, Date::UNPARSABLE_VALUES
Instance Attribute Summary
Attributes inherited from Date
Class Method Summary collapse
Methods inherited from ExtractorDateFormat
Methods inherited from Date
#<=>, #approximate?, #as_range, #base_value, #decoded_value, #encoding, #encoding?, #end?, format_date, from_cocina, #inferred?, #initialize, #parsable?, parse_date, #parsed_date?, #precision, #primary?, #qualified?, #qualified_value, #qualifier, #questionable?, #sort_key, #start?, #to_a, #value
Constructor Details
This class inherits a constructor from CocinaDisplay::Dates::Date
Class Method Details
.normalize_to_edtf(text) ⇒ Object
553 554 555 556 |
# File 'lib/cocina_display/dates/date.rb', line 553 def self.normalize_to_edtf(text) matches = text.match(REGEX) roman_to_int(matches[:year].upcase).to_s end |
.roman_to_int(value) ⇒ Object
558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/cocina_display/dates/date.rb', line 558 def self.roman_to_int(value) value = value.tr(".", "") map = {"M" => 1000, "CM" => 900, "D" => 500, "CD" => 400, "C" => 100, "XC" => 90, "L" => 50, "XL" => 40, "X" => 10, "IX" => 9, "V" => 5, "IV" => 4, "I" => 1} result = 0 map.each do |k, v| while value.index(k) == 0 result += v value.slice! k end end result end |