Class: Stanford::Mods::Imprint::DateValue
- Inherits:
-
Object
- Object
- Stanford::Mods::Imprint::DateValue
- Defined in:
- lib/stanford-mods/imprint.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#base_value ⇒ Object
Element text reduced to digits and hyphen.
-
#decoded_value(allowed_precisions: [:day, :month, :year, :decade, :century], ignore_unparseable: false, display_original_text: true) ⇒ Object
Decoded version of the date, if it was encoded.
-
#format_date(date, precision, allowed_precisions) ⇒ Object
Returns the date in the format specified by the precision.
-
#initialize(value) ⇒ DateValue
constructor
A new instance of DateValue.
- #key_date? ⇒ Boolean
-
#parseable? ⇒ Boolean
True if the element text isn’t blank or one of a set of unparseable values.
- #parsed_date? ⇒ Boolean
- #qualified? ⇒ Boolean
-
#qualified_value ⇒ Object
Decoded date with “BCE” or “CE” and qualifier markers.
- #sort_key ⇒ Object
Constructor Details
#initialize(value) ⇒ DateValue
Returns a new instance of DateValue.
145 146 147 |
# File 'lib/stanford-mods/imprint.rb', line 145 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
142 143 144 |
# File 'lib/stanford-mods/imprint.rb', line 142 def value @value end |
Instance Method Details
#base_value ⇒ Object
Element text reduced to digits and hyphen. Captures date ranges and negative (BCE) dates. Used for comparison/deduping.
199 200 201 202 203 204 205 |
# File 'lib/stanford-mods/imprint.rb', line 199 def base_value if text =~ /^\[?1\d{3}-\d{2}\??\]?$/ return text.sub(/(\d{2})(\d{2})-(\d{2})/, '\1\2-\1\3') end text.gsub(/(?<![\d])(\d{1,3})([xu-]{1,3})/i) { "#{Regexp.last_match(1)}#{'0' * Regexp.last_match(2).length}" }.scan(/[\d-]/).join end |
#decoded_value(allowed_precisions: [:day, :month, :year, :decade, :century], ignore_unparseable: false, display_original_text: true) ⇒ Object
Decoded version of the date, if it was encoded. Strips leading zeroes.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/stanford-mods/imprint.rb', line 208 def decoded_value(allowed_precisions: [:day, :month, :year, :decade, :century], ignore_unparseable: false, display_original_text: true) return if ignore_unparseable && !date return text.strip unless date if display_original_text unless encoding.present? return text.strip unless text =~ /^-?\d+$/ || text =~ /^[\dXxu?-]{4}$/ end end if date.is_a?(EDTF::Interval) if value.precision == :century || value.precision == :decade return format_date(date, value.precision, allowed_precisions) end range = [ format_date(date.min, date.min.precision, allowed_precisions), format_date(date.max, date.max.precision, allowed_precisions) ].uniq.compact return text.strip if range.empty? range.join(' - ') else format_date(date, value.precision, allowed_precisions) || text.strip end end |
#format_date(date, precision, allowed_precisions) ⇒ Object
Returns the date in the format specified by the precision. Allowed_precisions should be ordered by granularity and supports e.g. getting a year precision when the actual date is more precise.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/stanford-mods/imprint.rb', line 239 def format_date(date, precision, allowed_precisions) precision = allowed_precisions.first unless allowed_precisions.include?(precision) case precision when :day date.strftime('%B %e, %Y') when :month date.strftime('%B %Y') when :year year = date.year if year < 1 "#{year.abs + 1} BCE" # Any dates before the year 1000 are explicitly marked CE elsif year > 1 && year < 1000 "#{year} CE" else year.to_s end when :decade "#{date.year}s" when :century if date.year.negative? "#{((date.year / 100).abs + 1).ordinalize} century BCE" else "#{((date.year / 100) + 1).ordinalize} century" end end end |
#key_date? ⇒ Boolean
154 155 156 |
# File 'lib/stanford-mods/imprint.rb', line 154 def key_date? value.key? end |
#parseable? ⇒ Boolean
True if the element text isn’t blank or one of a set of unparseable values.
150 151 152 |
# File 'lib/stanford-mods/imprint.rb', line 150 def parseable? text.present? && !['9999', '0000-00-00', 'uuuu', '[uuuu]'].include?(text.strip) end |
#parsed_date? ⇒ Boolean
162 163 164 |
# File 'lib/stanford-mods/imprint.rb', line 162 def parsed_date? date.present? end |
#qualified? ⇒ Boolean
158 159 160 |
# File 'lib/stanford-mods/imprint.rb', line 158 def qualified? qualifier.present? end |
#qualified_value ⇒ Object
Decoded date with “BCE” or “CE” and qualifier markers. See (outdated): consul.stanford.edu/display/chimera/MODS+display+rules#MODSdisplayrules-3b.%3CoriginInfo%3E
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/stanford-mods/imprint.rb', line 270 def qualified_value qualified_format = case qualifier when 'approximate' '[ca. %s]' when 'questionable' '[%s?]' when 'inferred' '[%s]' else '%s' end format(qualified_format, decoded_value) end |
#sort_key ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/stanford-mods/imprint.rb', line 166 def sort_key return unless date year = if date.is_a?(EDTF::Interval) date.from.year else date.year end str = if year > 0 # for CE dates, we can just pad them out to 4 digits and sort normally... year.to_s.rjust(4, "0") else # ... but for BCE, because we're sorting lexically, we need to invert the digits (replacing 0 with 9, 1 with 8, etc.), # we prefix it with a hyphen (which will sort before any digit) and the number of digits (also inverted) to get # it to sort correctly. inverted_year = year.abs.to_s.chars.map { |c| BCE_CHAR_SORT_MAP[c] }.join length_prefix = BCE_CHAR_SORT_MAP[inverted_year.to_s.length.to_s] "-#{length_prefix}#{inverted_year}" end case value.precision when :decade str[0...-1] + "-" when :century str[0...-2] + "--" else str end end |