Class: Version

Inherits:
Object
  • Object
show all
Extended by:
Comparable, Enumerable
Includes:
Comparable
Defined in:
lib/Version.rb

Defined Under Namespace

Modules: VERSION

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_string = nil) ⇒ Version

Returns a new instance of Version.



272
273
274
# File 'lib/Version.rb', line 272

def initialize(version_string = nil)
  @version_string = version_string.to_s
end

Class Attribute Details

.version_stringsObject

Returns the value of attribute version_strings.



31
32
33
# File 'lib/Version.rb', line 31

def version_strings
  @version_strings
end

Instance Attribute Details

#version_stringObject

Returns the value of attribute version_string.



270
271
272
# File 'lib/Version.rb', line 270

def version_string
  @version_string
end

Class Method Details

.<<(version) ⇒ Object



33
34
35
36
# File 'lib/Version.rb', line 33

def <<(version)
  self.version_strings ||= []
  self.version_strings << to_version_string(version)
end

.<=>(version_1, version_2) ⇒ Object



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
160
161
162
163
164
165
166
167
168
169
# File 'lib/Version.rb', line 115

def <=>(version_1, version_2)
  version_1 = Version.new(Version.to_version_string(version_1))
  version_2 = Version.new(Version.to_version_string(version_2))
  if version_1.has_hyphenated_part? || version_2.has_hyphenated_part?
    if (to_a(version_1, false) <=> to_a(version_2, false)) == 0
      if version_1.hyphenated_part == version_2.hyphenated_part
        0
      elsif version_1.hyphenated_part =~ /^dev/
        case version_2.hyphenated_part
        when /^dev/; version_1.hyphenated_part <=> version_2.hyphenated_part
        when /^preview/; -1
        when /^rc/; -1
        when /^p\d+/; -1
        else; -1
        end
      elsif version_1.hyphenated_part =~ /^preview/
        case version_2.hyphenated_part
        when /^preview/; version_1.hyphenated_part <=> version_2.hyphenated_part
        when /^dev/; 1
        when /^rc/; -1
        when /^p\d+/; -1
        else; -1
        end
      elsif version_1.hyphenated_part =~ /^rc/
        case version_2.hyphenated_part
        when /^rc/; version_1.hyphenated_part <=> version_2.hyphenated_part
        when /^dev/; 1
        when /^preview/; 1
        when /^p\d+/; -1
        else; -1
        end
      elsif version_1.hyphenated_part =~ /^p\d+/
        case version_2.hyphenated_part
        when /^p\d+/; version_1.hyphenated_part <=> version_2.hyphenated_part
        when /^dev/; 1
        when /^preview/; 1
        when /^rc/; 1
        else; -1 # This assumes that 2.1.0 != 2.1.0-p0 (ie. 2.1.0-p0 < 2.1.0) and that for all other patch levels that anything with no patch level is superior.
        end
      else
        case version_2.hyphenated_part
        when /^dev/; 1
        when /^preview/; 1
        when /^rc/; 1
        when /^p\d+/; 1
        else; 1
        end
      end
    else
      to_a(version_1, false) <=> to_a(version_2, false)
    end
  else
    to_a(version_1) <=> to_a(version_2)
  end
end

.===(version_1, version_2) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/Version.rb', line 171

def ===(version_1, version_2)
  version_1_size = to_a(version_1).size
  version_2_size = to_a(version_2).size
  maximum_shared_places = [version_1_size, version_2_size].min
  if version_1_size < version_2_size
    new_version_2 = to_a(version_2).take(maximum_shared_places).join('.')
    self.<=>(version_1, new_version_2).zero? ? true : false
  elsif version_1_size > version_2_size
    new_version_1 = to_a(version_1).take(maximum_shared_places).join('.')
    self.<=>(new_version_1, version_2).zero? ? true : false
  else
    self.<=>(version_1, version_2).zero? ? true : false
  end
end

.build(version) ⇒ Object



72
73
74
75
76
# File 'lib/Version.rb', line 72

def build(version)
  build = to_version_string(version).split('.').fourth
  build = build.split('-').first if build && hyphenated_part?(version)
  build
end

.build?(version) ⇒ Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/Version.rb', line 234

def build?(version)
  build(version) ? true : false
end

.dev(version) ⇒ Object



86
87
88
89
90
# File 'lib/Version.rb', line 86

def dev(version)
  if hyphenated_part = self.hyphenated_part(version)
    hyphenated_part(version).capture(/^(dev\d*)/)
  end
end

.dev?(version) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/Version.rb', line 244

def dev?(version)
  dev(version) ? true : false
end

.eachObject



186
187
188
# File 'lib/Version.rb', line 186

def each
  version_strings.each{|version_string| yield Version.new(version_string)}
end

.from_version_code(version_code) ⇒ Object



196
197
198
199
# File 'lib/Version.rb', line 196

def from_version_code(version_code)
  version_string = version_code.scan(/../).collect{|n| n.to_i}.join('.')
  Version.new(version_string)
end

.hyphenated_part(version) ⇒ Object



109
110
111
112
113
# File 'lib/Version.rb', line 109

def hyphenated_part(version)
  if to_version_string(version).match(/-/)
    to_version_string(version).split('-').last
  end
end

.hyphenated_part?(version) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/Version.rb', line 261

def hyphenated_part?(version)
  hyphenated_part(version) ? true : false
end

.latest(*versions) ⇒ Object



46
47
48
# File 'lib/Version.rb', line 46

def latest(*versions)
  sorted(*versions).last
end

.major(version) ⇒ Object



51
52
53
54
55
# File 'lib/Version.rb', line 51

def major(version)
  major = to_version_string(version).split('.').first
  major = major.split('-').first if major && hyphenated_part?(version)
  major
end

.major?(version) ⇒ Boolean

boolean methods

Returns:

  • (Boolean)


219
220
221
# File 'lib/Version.rb', line 219

def major?(version)
  major(version) ? true : false
end

.minor(version) ⇒ Object



58
59
60
61
62
# File 'lib/Version.rb', line 58

def minor(version)
  minor = to_version_string(version).split('.').second
  minor = minor.split('-').first if minor && hyphenated_part?(version)
  minor
end

.minor?(version) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/Version.rb', line 224

def minor?(version)
  minor(version) ? true : false
end

.patch(version) ⇒ Object



79
80
81
82
83
# File 'lib/Version.rb', line 79

def patch(version)
  if hyphenated_part = self.hyphenated_part(version)
    hyphenated_part(version).capture(/^(p\d+)/) # It's unlikely that there would be a p without a number.
  end
end

.patch?(version) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/Version.rb', line 239

def patch?(version)
  patch(version) ? true : false
end

.preview(version) ⇒ Object



94
95
96
97
98
# File 'lib/Version.rb', line 94

def preview(version)
  if hyphenated_part = self.hyphenated_part(version)
    hyphenated_part.capture(/^(preview\d*)/) # It's possible that there will be a preview without a number.
  end
end

.preview?(version) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/Version.rb', line 250

def preview?(version)
  preview(version) ? true : false
end

.release_candidate(version) ⇒ Object



101
102
103
104
105
# File 'lib/Version.rb', line 101

def release_candidate(version)
  if hyphenated_part = self.hyphenated_part(version)
    hyphenated_part(version).capture(/^(rc\d*)/)
  end
end

.release_candidate?(version) ⇒ Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/Version.rb', line 255

def release_candidate?(version)
  release_candidate(version) ? true : false
end

.sorted(*versions) ⇒ Object



39
40
41
42
43
44
# File 'lib/Version.rb', line 39

def sorted(*versions)
  versions = versions.flatten
  return_value = versions.collect{|version| Version.new(to_version_string(version))}.sort
  return_value = return_value.collect{|e| e.to_s} if versions.all?{|e| e.is_a?(String)}
  return_value
end

.tiny(version) ⇒ Object



65
66
67
68
69
# File 'lib/Version.rb', line 65

def tiny(version)
  tiny = to_version_string(version).split('.').third
  tiny = tiny.split('-').first if tiny && hyphenated_part?(version)
  tiny
end

.tiny?(version) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/Version.rb', line 229

def tiny?(version)
  tiny(version) ? true : false
end

.to_a(version, include_hyphenated = true) ⇒ Object



190
191
192
193
194
# File 'lib/Version.rb', line 190

def to_a(version, include_hyphenated = true)
  to_a = [major(version), minor(version), tiny(version), build(version)]
  to_a = to_a + [hyphenated_part(version)] if include_hyphenated
  to_a.compact
end

.to_version_code(version) ⇒ Object



201
202
203
204
# File 'lib/Version.rb', line 201

def to_version_code(version)
  version_string = to_version_string(version)
  to_a(version_string, false).collect{|part| part.rjust(2, '0')}.join
end

.to_version_string(version) ⇒ Object

Should I handle version codes here? If I don’t handle version codes for any argument which makes reference to a version, then I could simply do a version.to_s in place of using to_version_string().



208
209
210
211
212
213
214
215
# File 'lib/Version.rb', line 208

def to_version_string(version)
  version = version.to_s
  if version.match(/\./)
    version
  else
    from_version_code(version).to_s
  end
end

Instance Method Details

#<=>(other_version) ⇒ Object



322
323
324
# File 'lib/Version.rb', line 322

def <=>(other_version)
  self.class.<=>(version_string, other_version)
end

#===(other_version) ⇒ Object



326
327
328
# File 'lib/Version.rb', line 326

def ===(other_version)
  self.class.===(version_string, other_version)
end

#buildObject



291
292
293
# File 'lib/Version.rb', line 291

def build
  self.class.build(version_string)
end

#build?Boolean

Returns:

  • (Boolean)


373
374
375
# File 'lib/Version.rb', line 373

def build?
  self.class.build?(version_string)
end

#devObject



301
302
303
# File 'lib/Version.rb', line 301

def dev
  self.class.dev(version_string)
end

#dev?Boolean

Returns:

  • (Boolean)


383
384
385
# File 'lib/Version.rb', line 383

def dev?
  self.class.dev?(version_string)
end

#hyphenated_partObject



318
319
320
# File 'lib/Version.rb', line 318

def hyphenated_part
  patch || dev || preview || release_candidate
end

#hyphenated_part?Boolean

Returns:

  • (Boolean)


400
401
402
# File 'lib/Version.rb', line 400

def hyphenated_part?
  patch? || dev? || preview? || release_candidate?
end

#least_significant_version_part_nameObject



342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/Version.rb', line 342

def least_significant_version_part_name
  if patch?
    :patch
  elsif build?
    :build
  elsif tiny?
    :tiny
  elsif minor?
    :minor
  else
    :major
  end
end

#majorObject



276
277
278
# File 'lib/Version.rb', line 276

def major
  self.class.major(version_string)
end

#major?Boolean

boolean methods

Returns:

  • (Boolean)


358
359
360
# File 'lib/Version.rb', line 358

def major?
  self.class.major?(version_string)
end

#minorObject



281
282
283
# File 'lib/Version.rb', line 281

def minor
  self.class.minor(version_string)
end

#minor?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/Version.rb', line 363

def minor?
  self.class.minor?(version_string)
end

#patchObject



296
297
298
# File 'lib/Version.rb', line 296

def patch
  self.class.patch(version_string)
end

#patch?Boolean

Returns:

  • (Boolean)


378
379
380
# File 'lib/Version.rb', line 378

def patch?
  self.class.patch?(version_string)
end

#previewObject



307
308
309
# File 'lib/Version.rb', line 307

def preview
  self.class.preview(version_string)
end

#preview?Boolean

Returns:

  • (Boolean)


389
390
391
# File 'lib/Version.rb', line 389

def preview?
  self.class.preview?(version_string) ? true : false
end

#release_candidateObject



312
313
314
# File 'lib/Version.rb', line 312

def release_candidate
  self.class.release_candidate(version_string)
end

#release_candidate?Boolean

Returns:

  • (Boolean)


394
395
396
# File 'lib/Version.rb', line 394

def release_candidate?
  self.class.release_candidate?(version_string) ? true : false
end

#tinyObject



286
287
288
# File 'lib/Version.rb', line 286

def tiny
  self.class.tiny(version_string)
end

#tiny?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'lib/Version.rb', line 368

def tiny?
  self.class.tiny?(version_string)
end

#to_aObject



330
331
332
# File 'lib/Version.rb', line 330

def to_a
  self.class.to_a(version_string)
end

#to_sObject



334
335
336
# File 'lib/Version.rb', line 334

def to_s
  version_string
end

#version_codeObject



338
339
340
# File 'lib/Version.rb', line 338

def version_code
  self.class.version_code(version_string)
end