module Versionomy::Format::Rubygems

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

Public Class Methods

create() click to toggle source

Create the rubygems format. This method is called internally when Versionomy loads the rubygems 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/rubygems.rb, line 122
def self.create

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

    # Global comparison function
    to_compare_type(:string) do |a_, b_|
      if a_.kind_of?(::Integer)
        if b_.kind_of?(::Integer)
          a_ <=> b_
        else
          1
        end
      else
        if b_.kind_of?(::Integer)
          -1
        else
          a_ <=> b_
        end
      end
    end

    # Global canonicalization function
    to_canonicalize_type(:string) do |val_|
      if val_.kind_of?(::Integer)
        val_
      else
        val_ = val_.to_s
        if val_ =~ /\A\d*\z/
          val_.to_i
        else
          val_
        end
      end
    end

    # 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(:field0, :type => :integer, :default_value => 1) do
      field(:field1, :type => :string) do
        field(:field2, :type => :string) do
          field(:field3, :type => :string) do
            field(:field4, :type => :string) do
              field(:field5, :type => :string) do
                field(:field6, :type => :string) do
                  field(:field7, :type => :string)
                end
              end
            end
          end
        end
      end
    end

    # Some field aliases providing alternate names for major fields
    alias_field(:major, :field0)
    alias_field(:minor, :field1)

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

  # The following is the definition of the rubygems format. It
  # understands the rubygems 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 any delimiter.
    field(:field0) do
      recognize_number(:delimiter_regexp => '', :default_delimiter => '')
    end

    # The remainder of the version number are represented as strings
    # or integers delimited by periods by default. Each is also
    # dependent on the presence of the previous field, so
    # :requires_previous_field retains its default value of true.
    # Finally, they can be optional in an unparsed string if they are
    # set to the default value of 0.
    field(:field1) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end
    field(:field2) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end
    field(:field3) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end
    field(:field4) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end
    field(:field5) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end
    field(:field6) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end
    field(:field7) do
      recognize_regexp('[0-9a-zA-Z]+', :default_value_optional => true)
    end

    # By default, we require that at least the first two fields
    # appear in an unparsed version string.
    default_unparse_params(:required_fields => [:field1])
  end
end