Class: IsoBibItem::BibliographicDate

Inherits:
Object
  • Object
show all
Defined in:
lib/iso_bib_item/bibliographic_date.rb

Overview

Bibliographic date.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, on: nil, from: nil, to: nil) ⇒ BibliographicDate

Returns a new instance of BibliographicDate.

Parameters:

  • type (String)

    “published”, “accessed”, “created”, “activated”

  • from (String) (defaults to: nil)
  • to (String) (defaults to: nil)

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# 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

Instance Attribute Details

#fromTime (readonly)

Returns:

  • (Time)


12
13
14
# File 'lib/iso_bib_item/bibliographic_date.rb', line 12

def from
  @from
end

#onTime (readonly)

Returns:

  • (Time)


18
19
20
# File 'lib/iso_bib_item/bibliographic_date.rb', line 18

def on
  @on
end

#toTime (readonly)

Returns:

  • (Time)


15
16
17
# File 'lib/iso_bib_item/bibliographic_date.rb', line 15

def to
  @to
end

#typeString (readonly)

Returns:

  • (String)


9
10
11
# File 'lib/iso_bib_item/bibliographic_date.rb', line 9

def type
  @type
end

Instance Method Details

#to_xml(builder, **opts) ⇒ Nokogiri::XML::Builder

Parameters:

  • builder (Nokogiri::XML::Builder)

Returns:

  • (Nokogiri::XML::Builder)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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