Module: Versionomy::Format::Rubygems

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

Overview

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

Defined Under Namespace

Modules: ExtraMethods

Class Method Summary collapse

Class Method Details

.createObject

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.



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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# 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