class Drupid::Version

Represents a project’s version. A version has the form:

<core>.x-<major>.<patch level>[-<extra>]

Examples of versions include: ‘7.x-1.0’, ‘7.x-1.2-beta2’, ‘8.x-1.x-dev’.

See also: drupal.org/node/467026

Constants

ALPHA
BETA
DEVELOPMENT
EMPTY
RC
UNKNOWN
UNSTABLE

Attributes

core[R]

The core number, e.g., in 7.x-3.2.beta1, it is 7.

extra_num[R]

The numeric part of the extra description, e.g., 7.x-3.2-beta1, it is 1 (Fixnum).

extra_type[R]

The project’s type, which is one of the constants UNSTABLE, ALPHA, BETA, RC, DEVELOPMENT, EMPTY or UNKNOWN. For example, for 7.x-3.2-beta1, it is BETA.

major[R]

The major version number, e.g., in 7.x-3.2-beta1, it is 3 (Fixnum).

patchlevel[R]

The patch level, e.g., in 7.x-3.2-beta1, it is 2 (Fixnum).

Public Class Methods

from_s(v) click to toggle source

Builds a Drupid::Version object from a string, e.g., ‘8.x-2.0rc1’.

    # File lib/drupid/project_version.rb
113 def self.from_s v
114   if v.match(/^(\d+)\.x-(\d+.+)$/)
115     Version.new($1.to_i, $2)
116   else
117     raise NotDrupalVersionError, "Cannot build a version from this string: #{v}"
118   end
119 end
new(core_num, v) click to toggle source

Initializes a Version object from a core number and a short version.

    # File lib/drupid/project_version.rb
102 def initialize(core_num, v)
103   raise 'Drupal version is not a string.' unless v.is_a?(String)
104   @core = Drupid::VersionCore.new(core_num)
105   @major = v.match(/^(\d+)/)[1].to_i
106   @patchlevel = $~.post_match.match(/\.(\d+|x)/)[1]
107   @patchlevel = @patchlevel.to_i if 'x' != @patchlevel
108   @extra_string = ''
109   encode_extra($~.post_match) # Initialize @extra_type and @extra_num
110 end

Public Instance Methods

<=>(w) click to toggle source

Compares two versions according the their “natural” ordering.

According to this ordering, ‘1.x-dev’ < ‘1.0-unstable6’ < ‘1.0-alpha4’ < ‘1.0-beta0’ < ‘1.0-rc1’ < ‘1.0’ <‘2.x-dev’.

    # File lib/drupid/project_version.rb
182 def <=>(w)
183   c = @core <=> w.core
184   if 0 == c
185     c = @major <=> w.major
186     if 0 == c
187       c = @patchlevel <=> w.patchlevel
188       case c
189       when nil # e.g., 1 vs 'x'
190         c = ('x' == @patchlevel) ? -1 : 1
191       when 0
192         c = @extra_type <=> w.extra_type
193         if 0 == c
194           c = @extra_num <=> w.extra_num
195         end
196       end
197     end
198   end
199   c
200 end
==(other) click to toggle source

In Ruby 1.8.7, some equality tests fail with the following message:

No visible difference.
You should look at your implementation of ==.

if only <=> is defined. This is why we define == explicitly.

    # File lib/drupid/project_version.rb
170 def ==(other)
171   @core == other.core and
172   @major == other.major and
173   @patchlevel == other.patchlevel and
174   @extra_type == other.extra_type and
175   @extra_num == other.extra_num
176 end
alpha?() click to toggle source

Returns true if this is an alpha release (e.g., ‘8.x-1.0-alpha1’).

    # File lib/drupid/project_version.rb
138 def alpha?
139   ALPHA == @extra_type
140 end
beta?() click to toggle source

Returns true if this is a beta release (e.g., ‘8.x-1.0-beta1’).

    # File lib/drupid/project_version.rb
143 def beta?
144   BETA == @extra_type
145 end
better(w) click to toggle source

Compares two versions to determine which is “better”. Returns -1 if self is better than w, 0 when they are the same equivalent, 1 when w is better than w.

By our own definitions, stable versions (e.g., ‘1.0’) are better than release candidates (e.g., (‘1.1-rc1’), which are better than beta releases (e.g., ‘1.2-beta2’), which are better than alpha releases (e.g., ‘1.3-alpha1’), which are better than unstable releases (e.g., ‘1.4-unstable10’), which are better than development snapshots (e.g., ‘2.x-dev’), which are better than anything else (e.g., ‘3.x’).

    # File lib/drupid/project_version.rb
213 def better(w)
214   if ('x' == @patchlevel and EMPTY == @extra_type) or (EMPTY == w.extra_type and 'x' == w.patchlevel)
215     c = (self <=> w)
216   else
217     c = @extra_type <=> w.extra_type
218     if 0 == c
219       c = (self <=> w)
220     end
221   end
222   c
223 end
development_snapshot?() click to toggle source

Returns true if this version represents a development snapshot; returns false otherwise.

    # File lib/drupid/project_version.rb
123 def development_snapshot?
124   'x' == @patchlevel or DEVELOPMENT == @extra_type
125 end
extra() click to toggle source
    # File lib/drupid/project_version.rb
225 def extra
226   case @extra_type
227     when EMPTY then t = ''
228     when UNSTABLE then t = 'unstable'
229     when ALPHA then t = 'alpha'
230     when BETA then t = 'beta'
231     when RC then t = 'rc'
232     when DEVELOPMENT then t = 'dev'
233   else # unknown
234     t = @extra_string
235   end
236   if UNKNOWN == @extra_num
237     t
238   else
239     t + @extra_num.to_s
240   end
241 end
long() click to toggle source

Returns the full textual representation of this version, e.g., ‘7.x-3.2’.

    # File lib/drupid/project_version.rb
160 def long
161   xtr = extra()
162   xtr = '-' + xtr if '' != xtr
163   @core.to_s + '-' + @major.to_s + '.' + @patchlevel.to_s + xtr
164 end
release_candidate?() click to toggle source

Returns true if this is a release candidate (e.g., ‘8.x-1.0-rc1’).

    # File lib/drupid/project_version.rb
133 def release_candidate?
134   RC == @extra_type
135 end
short() click to toggle source

Returns a short textual representation of this version, e.g., ‘3.2’.

    # File lib/drupid/project_version.rb
153 def short
154   xtr = extra()
155   xtr = '-' + xtr if '' != xtr
156   @major.to_s + '.' + @patchlevel.to_s + xtr
157 end
stable?() click to toggle source

Returns true if this a stable version (e.g., ‘8.x-1.0’).

    # File lib/drupid/project_version.rb
128 def stable?
129   EMPTY == @extra_type and 'x' != @patchlevel
130 end
to_s() click to toggle source

A synonym for self.short.

    # File lib/drupid/project_version.rb
148 def to_s
149   short
150 end

Private Instance Methods

encode_extra(e) click to toggle source
    # File lib/drupid/project_version.rb
245 def encode_extra(e)
246   @extra_string = e.start_with?('-') ? e.sub(/-/, '') : e
247   if e.match(/dev(\d*)$/)
248     @extra_type = DEVELOPMENT
249     @extra_num = ($~[1] == '') ? UNKNOWN : $~[1].to_i
250     return
251   end
252   e.match(/^-{0,1}([A-z]*)(\d*)$/)
253   if nil != $~
254     t = $~[1]
255     n = $~[2]
256     case t
257     when /^$/       then @extra_type = EMPTY
258     when /dev/      then @extra_type = DEVELOPMENT
259     when /unstable/ then @extra_type = UNSTABLE
260     when /alpha/    then @extra_type = ALPHA
261     when /beta/     then @extra_type = BETA
262     when /rc/       then @extra_type = RC
263     else
264       @extra_type = UNKNOWN
265     end
266     @extra_num = ('' != n) ? n.to_i : UNKNOWN
267   else
268     @extra_type = UNKNOWN
269     @extra_num = UNKNOWN
270   end
271   return
272 end