<commit_before># frozen_string_literal: true

# The Version is a non-persistent object that represents thet version of the
# backend
class Version < ActiveModelSerializers::Model
  # The exception thrown, when the version of the backend could not be
  # determined; be it by using the VERSION file or by git command
  class CouldNotDetermineVersion < StandardError
    def initialize(msg = 'Could not determine backend version.')
      super(msg)
    end
  end

  attr_accessor :commit, :tag, :full, :commits_since_tag

  def initialize(version_string)
    match = /(.+)-(\d+)-g([a-z0-9]{7,40})/.match(version_string)
    super(full: version_string,
          tag: match[1],
          commits_since_tag: match[2].to_i,
          commit: match[3])
  end

  class << self
    def load_version
      version =
        if production?
          read_version_file
        else
          read_version_from_git
        end
      raise CouldNotDetermineVersion, exception_message if version.empty?
      version
    end

    private

    # :nocov:
    def production?
      Rails.env.production?
    end
    # :nocov:

    def test?
      Rails.env.test?
    end

    # :nocov:
    def read_version_file
      File.read(Rails.root.join('VERSION')).strip
    rescue Errno::ENOENT
      ''
    end
    # :nocov:

    # :nocov:
    def read_version_from_git
      `git describe --long --tags`.strip
    end
    # :nocov:

    def exception_message
      msg = 'Could not determine the backend version. '
      msg +=<commit_msg>Set version in test environment (#107)<commit_after># frozen_string_literal: true

# The Version is a non-persistent object that represents thet version of the
# backend
class Version < ActiveModelSerializers::Model
  # The exception thrown, when the version of the backend could not be
  # determined; be it by using the VERSION file or by git command
  class CouldNotDetermineVersion < StandardError
    def initialize(msg = 'Could not determine backend version.')
      super(msg)
    end
  end

  attr_accessor :commit, :tag, :full, :commits_since_tag

  def initialize(version_string)
    match = /(.+)-(\d+)-g([a-z0-9]{7,40})/.match(version_string)
    super(full: version_string,
          tag: match[1],
          commits_since_tag: match[2].to_i,
          commit: match[3])
  end

  class << self
    def load_version
      version =
        if production?
          read_version_file
        else
          read_version_from_git
        end
      raise CouldNotDetermineVersion, exception_message if version.empty?
      version
    end

    private

    # :nocov:
    def production?
      Rails.env.production?
    end
    # :nocov:

    def test?
      Rails.env.test? && !ENV['ONTOHUB_SYSTEM_TEST']
    end

    # :nocov:
    def read_version_file
      File.read(Rails.root.join('VERSION')).strip
    rescue Errno::ENOENT
      ''
    end
    # :nocov:

    # :nocov:
    def read_version_from_git
      `git describe --long --tags`.strip
    end
    # :nocov:

    def exception_message
      msg = 'Could not determine the backend version. '
      msg +=
{'fraction_non_alphanumeric': 0.0900259067357513, 'fraction_numerical': 0.006152849740932643, 'mean_word_length': 2.526255707762557, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7351784', 'n_tokens_mistral': 960, 'n_tokens_neox': 949, 'n_words': 266}