class IsoBibItem::BibliographicDate

Bibliographic date.

Attributes

from[R]

@return [Time]

on[R]

@return [Time]

to[R]

@return [Time]

type[R]

@return [String]

Public Class Methods

new(type:, on: nil, from: nil, to: nil) click to toggle source

@param type [String] “published”, “accessed”, “created”, “activated” @param from [String] @param to [String]

# File lib/iso_bib_item/bibliographic_date.rb, line 23
def initialize(type:, on: nil, from: nil, to: nil)
  raise ArgumentError, 'expected :on or :form argument' unless on || from
  @type = type
  @on   = parse_date on
  @from = parse_date from
  @to   = parse_date to
end

Public Instance Methods

to_xml(builder, **opts) click to toggle source

@param builder [Nokogiri::XML::Builder] @return [Nokogiri::XML::Builder]

# File lib/iso_bib_item/bibliographic_date.rb, line 35
def to_xml(builder, **opts)
  builder.date(type: type) do
    if on
      date = opts[:full_date] ? on.strftime("%Y-%m") : on.year
      builder.on(opts[:no_year] ? '--' : date)
    elsif from
      if opts[:full_date]
        date_form = from.strftime("%Y-%m")
        date_to = to.strftime("%Y-%m") if to
      else
        date_form = from.year
        date_to = to.year if to
      end
      builder.from(opts[:no_year] ? '--' : date_form)
      builder.to date_to if to
    end
  end
end

Private Instance Methods

parse_date(date) click to toggle source

@params date [String] 'yyyy' or 'yyyy-mm'

# File lib/iso_bib_item/bibliographic_date.rb, line 58
def parse_date(date)
  return unless date
  if date =~ /^\d{4}$/
    Time.strptime date, '%Y'
  else
    Time.strptime date, '%Y-%m'
  end
end