Sha256: 57cf9d6837ec8c6cfe3682eefa5f6ae26c9aec96f3ceb2ebc3a148a105afc68d

Contents?: true

Size: 2 KB

Versions: 31

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

require "time"

module RelatonBib
  # Bibliographic date.
  class BibliographicDate
    TYPES = %w[published accessed created implemented obsoleted confirmed
               updated issued transmitted copied unchanged circulated adapted].freeze

    # @return [String]
    attr_reader :type

    # @return [Date]
    attr_reader :from

    # @return [Date]
    attr_reader :to

    # @return [Date]
    attr_reader :on

    # @param type [String] "published", "accessed", "created", "activated"
    # @param on [String]
    # @param from [String]
    # @param to [String]
    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

    # rubocop:disable Metrics/AbcSize

    # @param builder [Nokogiri::XML::Builder]
    # @return [Nokogiri::XML::Builder]
    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
    # rubocop:enable Metrics/AbcSize

    # @return [Hash]
    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

    private

    # Formats date
    # @param date [Time]
    # @param format [Symbol, nil] :full (yyyy-mm-dd), :short (yyyy-mm) or nil (yyyy)
    # @return [String]
    def date_format(date, format = nil)
      case format
      when :short then date.strftime "%Y-%m"
      when :full then date.strftime "%Y-%m-%d"
      else date.year
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/relaton-bib-1.2.4/lib/relaton_bib/bibliographic_date.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/relaton-bib-1.2.4/lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.2.4 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.2.2 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.2.1 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.2.0 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.1.1 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.1.0 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.0.4 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.0.3 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.0.2 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.0.1 lib/relaton_bib/bibliographic_date.rb
relaton-bib-1.0.0 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.9.2 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.9.1 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.9.0 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.8.1 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.8.0 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.7.0 lib/relaton_bib/bibliographic_date.rb
relaton-bib-0.6.0 lib/relaton_bib/bibliographic_date.rb