Module: Versionomy::Format::Semver

Defined in:
lib/versionomy/format_definitions/semver.rb

Overview

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

Defined Under Namespace

Modules: ExtraMethods

Class Method Summary collapse

Class Method Details

.createObject

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.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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