module Versionomy::Format::Semver

This is a namespace for the implementation of the semver schema and format.

Public Class Methods

create() click to toggle source

Create the 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 schema and format definition DSLs.

# File lib/versionomy/format_definitions/semver.rb, line 105
def self.create

  # The following is the definition of the semver schema
  schema_ = Schema.create do

    # The first field has the default value of 1. All other fields
    # have a default value of 0. Thus, the default version number
    # overall is "1.0".
    field(:major, :type => :integer, :default_value => 1) do
      field(:minor, :type => :integer) do
        field(:patch, :type => :integer) do
          field(:prerelease_suffix, :type => :string) do
            to_compare do |a_, b_|
              a_.length == 0 ? (b_.length == 0 ? 0 : 1) : (b_.length == 0 ? -1 : a_ <=> b_)
            end
          end
        end
      end
    end

    # An alias
    alias_field(:special_suffix, :prerelease_suffix)

    # Add the methods in this module to each value
    add_module(Format::Semver::ExtraMethods)
  end

  # The following is the definition of the standard format. It
  # understands the standard schema defined above.
  Format::Delimiter.new(schema_) do

    # All version number strings must start with the major version.
    # Unlike other fields, it is not preceded by the usual "dot"
    # delimiter, but it can be preceded by a "v" indicator.
    field(:major) do
      recognize_number(:delimiter_regexp => 'v?', :default_delimiter => '')
    end

    # The remainder of the core version number are represented as
    # integers delimited by periods. These fields are required.
    field(:minor) do
      recognize_number
    end
    field(:patch) do
      recognize_number
    end

    # The optional prerelease field is represented as a string
    # beginning with an alphabetic character.
    field(:prerelease_suffix) do
      recognize_regexp('[a-zA-Z][0-9a-zA-Z-]*', :default_value_optional => true,
                       :delimiter_regexp => '', :default_delimiter => '')
    end
  end
end