module Versionomy::Conversion::Semver

This is a namespace for the implementation of the conversion between the semver and standard formats.

Public Class Methods

create_semver_to_standard() click to toggle source

Create the conversion from semver to standard format. This method is called internally when Versionomy loads the semver format, and you should not need to call it again. It is documented so that you can inspect its source code from RDoc, since the source contains useful examples of how to use the conversion DSLs.

# File lib/versionomy/format_definitions/semver.rb, line 243
def self.create_semver_to_standard

  # We'll use a parsing conversion.
  Conversion::Parsing.new do

    # Handle the case where the semver version ends with a string
    # field, e.g. "1.0b". We want to treat this like "1.0b0" rather
    # than "1.0-2" since the semver semantic states that this is a
    # prerelease version. So we add 0 to the end of the parsed string
    # if it ends in a letter.
    to_modify_string do |str_, convert_params_|
      str_.gsub(/([[:alpha:]])\z/, '\10')
    end

  end

end
create_standard_to_semver() click to toggle source

Create the conversion from standard to semver format. This method is called internally when Versionomy loads the semver format, and you should not need to call it again. It is documented so that you can inspect its source code from RDoc, since the source contains useful examples of how to use the conversion DSLs.

# File lib/versionomy/format_definitions/semver.rb, line 186
def self.create_standard_to_semver

  # We'll use a parsing conversion.
  Conversion::Parsing.new do

    # Sanity check the original value and make sure it doesn't
    # include fields that we don't support.
    to_modify_original_value do |value_, convert_params_|
      if value_.has_field?(:patchlevel) && value_.patchlevel != 0
        raise Errors::ConversionError, 'Cannot convert a version with a patchlevel to semver'
      end
      if value_.tiny2 != 0
        raise Errors::ConversionError, 'Cannot convert a version more than three fields to semver'
      end
      value_
    end

    # We're going to modify how the standard format version is
    # unparsed, so the semver format will have a better chance
    # of parsing it.
    to_modify_unparse_params do |params_, convert_params_|

      # All three fields are required
      params_[:minor_required] = true
      params_[:tiny_required] = true

      # If the standard format version has a prerelease notation,
      # make sure it isn't set off using a delimiter.
      params_[:release_type_delim] = ''
      params_[:development_version_delim] = ''
      params_[:development_minor_delim] = '-'
      params_[:alpha_version_delim] = ''
      params_[:alpha_minor_delim] = '-'
      params_[:beta_version_delim] = ''
      params_[:beta_minor_delim] = '-'
      params_[:release_candidate_version_delim] = ''
      params_[:release_candidate_minor_delim] = '-'
      params_[:preview_version_delim] = ''
      params_[:preview_minor_delim] = '-'

      # If the standard format version includes a "v" prefix, strip it
      params_[:major_delim] = nil

      params_
    end

  end

end