Class: RelatonBib::BibliographicDate

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

Overview

Bibliographic date.

Constant Summary collapse

TYPES =
%w[published accessed created implemented obsoleted confirmed
updated corrected issued transmitted copied unchanged circulated adapted
vote-started vote-ended announced stable-until].freeze

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”

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

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/relaton_bib/bibliographic_date.rb', line 19

def initialize(type:, on: nil, from: nil, to: nil)
  raise ArgumentError, "expected :on or :from argument" unless on || from

  # raise ArgumentError, "invalid type: #{type}" unless TYPES.include? type

  @type = type
  @on   = RelatonBib.parse_date on
  @from = RelatonBib.parse_date from
  @to   = RelatonBib.parse_date to
end

Instance Attribute Details

#typeString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/relaton_bib/bibliographic_date.rb', line 13

def type
  @type
end

Instance Method Details

#from(part = nil) ⇒ String, ... Also known as: to, on

Parameters:

  • part (Symbol) (defaults to: nil)

    :year, :month, :day, :date

Returns:

  • (String, Date, nil)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/relaton_bib/bibliographic_date.rb', line 32

def from(part = nil) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  d = instance_variable_get "@#{__callee__}".to_sym
  return d unless part && d

  # date = parse_date(d)
  # return date if part == :date

  parts = d.split "-"
  case part
  when :year then parts[0]&.to_i
  when :month then parts[1]&.to_i
  when :day then parts[2]&.to_i
  when :date then parse_date(d)
  else d
  end

  # date.is_a?(Date) ? date.send(part) : date
end

#to_asciibib(prefix = "", count = 1) ⇒ String

Parameters:

  • prefix (String) (defaults to: "")
  • count (Integer) (defaults to: 1)

    number of dates

Returns:

  • (String)


82
83
84
85
86
87
88
89
90
# File 'lib/relaton_bib/bibliographic_date.rb', line 82

def to_asciibib(prefix = "", count = 1)
  pref = prefix.empty? ? prefix : "#{prefix}."
  out = count > 1 ? "#{pref}date::\n" : ""
  out += "#{pref}date.type:: #{type}\n"
  out += "#{pref}date.on:: #{on}\n" if on
  out += "#{pref}date.from:: #{from}\n" if from
  out += "#{pref}date.to:: #{to}\n" if to
  out
end

#to_hashHash

Returns:

  • (Hash)


71
72
73
74
75
76
77
# File 'lib/relaton_bib/bibliographic_date.rb', line 71

def to_hash
  hash = { "type" => type }
  hash["value"] = on.to_s if on
  hash["from"] = from.to_s if from
  hash["to"] = to.to_s if to
  hash
end

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

Parameters:

  • builder (Nokogiri::XML::Builder)

Returns:

  • (Nokogiri::XML::Builder)


58
59
60
61
62
63
64
65
66
67
# File 'lib/relaton_bib/bibliographic_date.rb', line 58

def to_xml(builder, **opts)
  builder.date(type: type) do
    if on
      builder.on(opts[:no_year] ? "--" : date_format(on, opts[:date_format]))
    elsif from
      builder.from(opts[:no_year] ? "--" : date_format(from, opts[:date_format]))
      builder.to date_format(to, opts[:date_format]) if to
    end
  end
end