Class: Drupid::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/drupid/project_version.rb

Overview

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

Constant Summary collapse

UNKNOWN =
-1
DEVELOPMENT =
1
UNSTABLE =
2
ALPHA =
4
BETA =
8
RC =
16
EMPTY =
32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core_num, v) ⇒ Version

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



103
104
105
106
107
108
109
110
111
# File 'lib/drupid/project_version.rb', line 103

def initialize(core_num, v)
  raise 'Drupal version is not a string.' unless v.is_a?(String)
  @core = Drupid::VersionCore.new(core_num)
  @major = v.match(/^(\d+)/)[1].to_i
  @patchlevel = $~.post_match.match(/\.(\d+|x)/)[1]
  @patchlevel = @patchlevel.to_i if 'x' != @patchlevel
  @extra_string = ''
  encode_extra($~.post_match) # Initialize @extra_type and @extra_num
end

Instance Attribute Details

#coreObject (readonly)

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



82
83
84
# File 'lib/drupid/project_version.rb', line 82

def core
  @core
end

#extra_numObject (readonly)

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



92
93
94
# File 'lib/drupid/project_version.rb', line 92

def extra_num
  @extra_num
end

#extra_typeObject (readonly)

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.



90
91
92
# File 'lib/drupid/project_version.rb', line 90

def extra_type
  @extra_type
end

#majorObject (readonly)

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



84
85
86
# File 'lib/drupid/project_version.rb', line 84

def major
  @major
end

#patchlevelObject (readonly)

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



86
87
88
# File 'lib/drupid/project_version.rb', line 86

def patchlevel
  @patchlevel
end

Class Method Details

.from_s(v) ⇒ Object

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



114
115
116
117
118
119
120
# File 'lib/drupid/project_version.rb', line 114

def self.from_s v
  if v.match(/^(\d+)\.x-(\d+.+)$/)
    Version.new($1.to_i, $2)
  else
    raise NotDrupalVersionError, "Cannot build a version from this string: #{v}"
  end
end

Instance Method Details

#<=>(w) ⇒ Object

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’.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/drupid/project_version.rb', line 183

def <=>(w)
  c = @core <=> w.core
  if 0 == c
    c = @major <=> w.major
    if 0 == c
      c = @patchlevel <=> w.patchlevel
      case c
      when nil # e.g., 1 vs 'x'
        c = ('x' == @patchlevel) ? -1 : 1
      when 0
        c = @extra_type <=> w.extra_type
        if 0 == c
          c = @extra_num <=> w.extra_num
        end
      end
    end
  end
  c
end

#==(other) ⇒ Object

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.



171
172
173
174
175
176
177
# File 'lib/drupid/project_version.rb', line 171

def ==(other)
  @core == other.core and
  @major == other.major and
  @patchlevel == other.patchlevel and
  @extra_type == other.extra_type and
  @extra_num == other.extra_num
end

#alpha?Boolean

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

Returns:

  • (Boolean)


139
140
141
# File 'lib/drupid/project_version.rb', line 139

def alpha?
  ALPHA == @extra_type
end

#beta?Boolean

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

Returns:

  • (Boolean)


144
145
146
# File 'lib/drupid/project_version.rb', line 144

def beta?
  BETA == @extra_type
end

#better(w) ⇒ Object

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’).



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/drupid/project_version.rb', line 214

def better(w)
  if ('x' == @patchlevel and EMPTY == @extra_type) or (EMPTY == w.extra_type and 'x' == w.patchlevel)
    c = (self <=> w)
  else
    c = @extra_type <=> w.extra_type
    if 0 == c
      c = (self <=> w)
    end
  end
  c
end

#development_snapshot?Boolean

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

Returns:

  • (Boolean)


124
125
126
# File 'lib/drupid/project_version.rb', line 124

def development_snapshot?
  'x' == @patchlevel or DEVELOPMENT == @extra_type
end

#extraObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/drupid/project_version.rb', line 226

def extra
  case @extra_type
    when EMPTY then t = ''
    when UNSTABLE then t = 'unstable'
    when ALPHA then t = 'alpha'
    when BETA then t = 'beta'
    when RC then t = 'rc'
    when DEVELOPMENT then t = 'dev'
  else # unknown
    t = @extra_string
  end
  if UNKNOWN == @extra_num
    t
  else
    t + @extra_num.to_s
  end
end

#longObject

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



161
162
163
164
165
# File 'lib/drupid/project_version.rb', line 161

def long
  xtr = extra()
  xtr = '-' + xtr if '' != xtr
  @core.to_s + '-' + @major.to_s + '.' + @patchlevel.to_s + xtr
end

#release_candidate?Boolean

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

Returns:

  • (Boolean)


134
135
136
# File 'lib/drupid/project_version.rb', line 134

def release_candidate?
  RC == @extra_type
end

#shortObject

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



154
155
156
157
158
# File 'lib/drupid/project_version.rb', line 154

def short
  xtr = extra()
  xtr = '-' + xtr if '' != xtr
  @major.to_s + '.' + @patchlevel.to_s + xtr
end

#stable?Boolean

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

Returns:

  • (Boolean)


129
130
131
# File 'lib/drupid/project_version.rb', line 129

def stable?
  EMPTY == @extra_type and 'x' != @patchlevel
end

#to_sObject

A synonym for self.short.



149
150
151
# File 'lib/drupid/project_version.rb', line 149

def to_s
  short
end