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.



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

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#base_valueObject

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



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

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) { "#{$1}#{'0' * $2.length}"}.scan(/[\d-]/).join
end

#decoded_valueObject

Decoded version of the date, if it was encoded. Strips leading zeroes.



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

def decoded_value
  return text.strip unless date

  unless encoding.present?
    return text.strip unless text =~ /^-?\d+$/ || text =~ /^[\dXxu?-]{4}$/
  end

  if date.is_a?(EDTF::Interval)
    if value.precision == :century || value.precision == :decade
      return format_date(date, value.precision)
    end

    range = [
      format_date(date.min, date.min.precision),
      format_date(date.max, date.max.precision)
    ].uniq.compact

    return text.strip if range.empty?

    range.join(' - ')
  else
    format_date(date, value.precision) || text.strip
  end
end

#format_date(date, precision) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/stanford-mods/imprint.rb', line 187

def format_date(date, 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} B.C."
    # Any dates before the year 1000 are explicitly marked A.D.
    elsif year > 1 && year < 1000
      "#{year} A.D."
    else
      year.to_s
    end
  when :century
    if date.year.negative?
      "#{((date.year / 100).abs + 1).ordinalize} century B.C."
    else
      "#{((date.year / 100) + 1).ordinalize} century"
    end
  when :decade
    "#{date.year}s"
  end
end

#qualified_valueObject

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/stanford-mods/imprint.rb', line 216

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

#valid?Boolean

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

Returns:

  • (Boolean)


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

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