Class: Stanford::Mods::Imprint::DateValue

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ DateValue

Returns a new instance of DateValue.



141
142
143
# File 'lib/stanford-mods/imprint.rb', line 141

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



138
139
140
# File 'lib/stanford-mods/imprint.rb', line 138

def value
  @value
end

Instance Method Details

#base_valueObject

Element text reduced to digits and hyphen. Captures date ranges and negative (BCE) dates. Used for comparison/deduping.



187
188
189
190
191
192
193
# File 'lib/stanford-mods/imprint.rb', line 187

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.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/stanford-mods/imprint.rb', line 196

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.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/stanford-mods/imprint.rb', line 227

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

Returns:

  • (Boolean)


150
151
152
# File 'lib/stanford-mods/imprint.rb', line 150

def key_date?
  value.key?
end

#parsed_date?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/stanford-mods/imprint.rb', line 158

def parsed_date?
  date.present?
end

#qualified?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/stanford-mods/imprint.rb', line 154

def qualified?
  qualifier.present?
end

#qualified_valueObject

Decoded date with “BCE” or “CE” and qualifier markers. See (outdated): consul.stanford.edu/display/chimera/MODS+display+rules#MODSdisplayrules-3b.%3CoriginInfo%3E



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/stanford-mods/imprint.rb', line 258

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_keyObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/stanford-mods/imprint.rb', line 162

def sort_key
  year = if date.is_a?(EDTF::Interval)
    date.from.year
  else
    date.year
  end

  str = if year < 1
    (-1 * year - 1000).to_s
  else
    year.to_s
  end

  case value.precision
  when :decade
    str[0..2] + "-"
  when :century
    str[0..1] + "--"
  else
    str.rjust(4, "0")
  end
end

#valid?Boolean

True if the element text isn’t blank or the placeholder “9999”.

Returns:

  • (Boolean)


146
147
148
# File 'lib/stanford-mods/imprint.rb', line 146

def valid?
  text.present? && !['9999', '0000-00-00', 'uuuu'].include?(text.strip)
end