class Blacksmith::VersionHelper::Version

See: semver.org

Constants

SemVerRegexp

Attributes

build[RW]
major[RW]
minor[RW]
patch[RW]
pre[RW]

Public Class Methods

new(version_str) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 13
def initialize version_str
  raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") unless version_str =~ SemVerRegexp

  version, parts = version_str.split '-'
  if not parts.nil? and parts.include? '+'
    @pre, @build = parts.split '+'
  elsif version.include? '+'
    version, @build = version.split '+'
  else
    @pre = parts
  end


  @major, @minor, @patch = version.split('.').map(&:to_i)
end

Public Instance Methods

<(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 69
def < other_version
  (self <=> other_version) == -1
end
<=(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 77
def <= other_version
  (self <=> other_version) <= 0
end
<=>(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 50
def <=> other_version
  other_version = Version.new(other_version) if other_version.is_a? String

  v1 = self.dup
  v2 = other_version.dup

  # The build must be excluded from the comparison, so that e.g. 1.2.3+foo and 1.2.3+bar are semantically equal.
  # "Build metadata SHOULD be ignored when determining version precedence".
  # (SemVer 2.0.0-rc.2, paragraph 10 - http://www.semver.org)
  v1.build = nil
  v2.build = nil

  compare_recursively(v1.to_a, v2.to_a)
end
==(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 81
def == other_version
  (self <=> other_version) == 0
end
>(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 65
def > other_version
  (self <=> other_version) == 1
end
>=(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 73
def >= other_version
  (self <=> other_version) >= 0
end
full!() click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 107
def full!
  env_var = "BLACKSMITH_FULL_VERSION"
  begin
    ENV.fetch env_var
  rescue KeyError
    raise Exception, "Setting the full version requires setting the #{env_var} environment variable to the new version"
  end
end
increment!(term) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 116
def increment!(term)
  new_version = clone

  if term != :patch || @pre.nil?
    new_version.send("#{term}=", send(term) + 1)
  end

  new_version.minor = 0 if term == :major
  new_version.patch = 0 if term == :major || term == :minor
  new_version.build = new_version.pre = nil

  new_version
end
satisfies(other_version) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 85
def satisfies other_version
  return true if other_version.strip == '*'
  parts = other_version.split(/(\d(.+)?)/, 2)
  comparator, other_version_string = parts[0].strip, parts[1].strip

  begin
    Version.new other_version_string
    comparator.empty? && comparator = '=='
    satisfies_comparator? comparator, other_version_string
  rescue ArgumentError
    if ['<', '>', '<=', '>='].include?(comparator)
      satisfies_comparator? comparator, pad_version_string(other_version_string)
    else
      tilde_matches? other_version_string
    end
  end
end
to_a() click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 29
def to_a
  [@major, @minor, @patch, @pre, @build]
end
Also aliased as: to_array
to_array()
Alias for: to_a
to_h() click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 41
def to_h
  keys = [:major, :minor, :patch, :pre, :build]
  Hash[keys.zip(self.to_a)]
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_s() click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 33
def to_s
  str = [@major, @minor, @patch].join '.'
  str << '-' << @pre unless @pre.nil?
  str << '+' << @build unless @build.nil?

  str
end
Also aliased as: to_string
to_string()
Alias for: to_s

Private Instance Methods

compare_recursively(ary1, ary2) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 154
def compare_recursively ary1, ary2
  # Short-circuit the recursion entirely if they're just equal
  return 0 if ary1 == ary2

  a = ary1.shift; b = ary2.shift

  # Reached the end of the arrays, equal all the way down
  return 0 if a.nil? and b.nil?

  # Mismatched types (ie. one has a pre and the other doesn't)
  if a.nil? and not b.nil?
    return 1
  elsif not a.nil? and b.nil?
    return -1
  end

  if a < b
    return -1
  elsif a > b
    return 1
  end

  # Versions are equal thus far, so recurse down to the next part.
  compare_recursively ary1, ary2
end
pad_version_string(version_string) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 132
def pad_version_string version_string
  parts = version_string.split('.').reject {|x| x == '*'}
  while parts.length < 3
    parts << '0'
  end
  parts.join '.'
end
satisfies_comparator?(comparator, other_version_string) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 146
def satisfies_comparator? comparator, other_version_string
  if comparator == '~'
    tilde_matches? other_version_string
  else
    self.send comparator, other_version_string
  end
end
tilde_matches?(other_version_string) click to toggle source
# File lib/puppet_blacksmith/version_helper.rb, line 140
def tilde_matches? other_version_string
  this_parts = to_a.collect(&:to_s)
  other_parts = other_version_string.split('.').reject {|x| x == '*'}
  other_parts == this_parts[0..other_parts.length-1]
end