Sha256: a1888a71a26927a49776bcdd7707ccf962a4d8650bc6e9e6d79e0837fc261e5f

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

# (c) Copyright 2020 Ribose Inc.
#

module IEV
  # Parses information from the spreadsheet's REPLACES column.
  #
  # @example
  #   SupersessionParser.new(cell_data_string).supersessions
  class SupersessionParser
    include CLI::UI
    using DataConversions

    attr_reader :raw_str, :src_str

    attr_reader :supersessions

    # Regular expression which describes IEV relation, for example
    # +881-01-23:1983-01+ or +845-03-55:1987+.
    IEV_SUPERSESSION_RX = %r{
      \A
      (?:IEV\s+)? # some are prefixed with IEV, it is unnecessary though
      (?<ref>\d{3}-\d{2}-\d{2})
      \s* # some have whitespaces around the separator
      : # separator
      \s* # some have whitespaces around the separator
      (?<version>[-0-9]+)
      \Z
    }x.freeze

    def initialize(source_str)
      @raw_str = source_str.dup.freeze
      @src_str = raw_str.sanitize.freeze
      @supersessions = parse
    end

    private

    def parse
      return if empty_source?

      if IEV_SUPERSESSION_RX =~ src_str
        [relation_from_match($~)]
      else
        warn "Incorrect supersession: '#{src_str}'"
        nil
      end
    end

    def empty_source?
      /\w/ !~ src_str
    end

    def relation_from_match(match_data)
      {
        "type" => "supersedes",
        "ref" => iev_ref_from_match(match_data),
      }
    end

    def iev_ref_from_match(match_data)
      {
        "source" => "IEV",
        "id" => match_data[:ref],
        "version" => match_data[:version],
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iev-0.3.4 lib/iev/supersession_parser.rb
iev-0.3.3 lib/iev/supersession_parser.rb