class Version

Attributes

version_strings[RW]
version_string[RW]

Public Class Methods

<<(version) click to toggle source
# File lib/Version.rb, line 33
def <<(version)
  self.version_strings ||= []
  self.version_strings << to_version_string(version)
end
<=>(version_1, version_2) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 234
def build?(version)
  build(version) ? true : false
end
dev(version) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 244
def dev?(version)
  dev(version) ? true : false
end
each() { |version| ... } click to toggle source
# File lib/Version.rb, line 186
def each
  version_strings.each{|version_string| yield Version.new(version_string)}
end
from_version_code(version_code) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 261
def hyphenated_part?(version)
  hyphenated_part(version) ? true : false
end
latest(*versions) click to toggle source
# File lib/Version.rb, line 46
def latest(*versions)
  sorted(*versions).last
end
major(version) click to toggle source
# 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) click to toggle source

boolean methods

# File lib/Version.rb, line 219
def major?(version)
  major(version) ? true : false
end
minor(version) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 224
def minor?(version)
  minor(version) ? true : false
end
new(version_string = nil) click to toggle source
# File lib/Version.rb, line 272
def initialize(version_string = nil)
  @version_string = version_string.to_s
end
patch(version) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 239
def patch?(version)
  patch(version) ? true : false
end
preview(version) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 250
def preview?(version)
  preview(version) ? true : false
end
release_candidate(version) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 255
def release_candidate?(version)
  release_candidate(version) ? true : false
end
sorted(*versions) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/Version.rb, line 229
def tiny?(version)
  tiny(version) ? true : false
end
to_a(version, include_hyphenated = true) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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

Public Instance Methods

<=>(other_version) click to toggle source
# File lib/Version.rb, line 322
def <=>(other_version)
  self.class.<=>(version_string, other_version)
end
===(other_version) click to toggle source
# File lib/Version.rb, line 326
def ===(other_version)
  self.class.===(version_string, other_version)
end
build() click to toggle source
# File lib/Version.rb, line 291
def build
  self.class.build(version_string)
end
build?() click to toggle source
# File lib/Version.rb, line 373
def build?
  self.class.build?(version_string)
end
dev() click to toggle source
# File lib/Version.rb, line 301
def dev
  self.class.dev(version_string)
end
dev?() click to toggle source
# File lib/Version.rb, line 383
def dev?
  self.class.dev?(version_string)
end
hyphenated_part() click to toggle source
# File lib/Version.rb, line 318
def hyphenated_part
  patch || dev || preview || release_candidate
end
hyphenated_part?() click to toggle source
# File lib/Version.rb, line 400
def hyphenated_part?
  patch? || dev? || preview? || release_candidate?
end
least_significant_version_part_name() click to toggle source
# 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
major() click to toggle source
# File lib/Version.rb, line 276
def major
  self.class.major(version_string)
end
major?() click to toggle source

boolean methods

# File lib/Version.rb, line 358
def major?
  self.class.major?(version_string)
end
minor() click to toggle source
# File lib/Version.rb, line 281
def minor
  self.class.minor(version_string)
end
minor?() click to toggle source
# File lib/Version.rb, line 363
def minor?
  self.class.minor?(version_string)
end
patch() click to toggle source
# File lib/Version.rb, line 296
def patch
  self.class.patch(version_string)
end
patch?() click to toggle source
# File lib/Version.rb, line 378
def patch?
  self.class.patch?(version_string)
end
preview() click to toggle source
# File lib/Version.rb, line 307
def preview
  self.class.preview(version_string)
end
preview?() click to toggle source
# File lib/Version.rb, line 389
def preview?
  self.class.preview?(version_string) ? true : false
end
release_candidate() click to toggle source
# File lib/Version.rb, line 312
def release_candidate
  self.class.release_candidate(version_string)
end
release_candidate?() click to toggle source
# File lib/Version.rb, line 394
def release_candidate?
  self.class.release_candidate?(version_string) ? true : false
end
tiny() click to toggle source
# File lib/Version.rb, line 286
def tiny
  self.class.tiny(version_string)
end
tiny?() click to toggle source
# File lib/Version.rb, line 368
def tiny?
  self.class.tiny?(version_string)
end
to_a() click to toggle source
# File lib/Version.rb, line 330
def to_a
  self.class.to_a(version_string)
end
to_s() click to toggle source
# File lib/Version.rb, line 334
def to_s
  version_string
end
version_code() click to toggle source
# File lib/Version.rb, line 338
def version_code
  self.class.version_code(version_string)
end