class Chef::VersionConstraint

Constants

DEFAULT_CONSTRAINT
OPS
PATTERN
STANDARD_OPS
VERSION_CLASS

Attributes

op[R]
raw_version[R]
version[R]

Public Class Methods

new(constraint_spec=DEFAULT_CONSTRAINT) click to toggle source
# File lib/chef/version_constraint.rb, line 29
def initialize(constraint_spec=DEFAULT_CONSTRAINT)
  case constraint_spec
  when nil
    parse(DEFAULT_CONSTRAINT)
  when Array
    parse_from_array(constraint_spec)
  when String
    parse(constraint_spec)
  else
    msg = "VersionConstraint should be created from a String. You gave: #{constraint_spec.inspect}"
    raise Chef::Exceptions::InvalidVersionConstraint, msg
  end
end

Public Instance Methods

==(o)
Alias for: eql?
eql?(o) click to toggle source
# File lib/chef/version_constraint.rb, line 60
def eql?(o)
  o.class == self.class && @op == o.op && @version == o.version
end
Also aliased as: ==
include?(v) click to toggle source
# File lib/chef/version_constraint.rb, line 43
def include?(v)
  version = if v.respond_to? :version # a CookbookVersion-like object
              self.class::VERSION_CLASS.new(v.version.to_s)
            else
              self.class::VERSION_CLASS.new(v.to_s)
            end
 do_op(version)
end
inspect() click to toggle source
# File lib/chef/version_constraint.rb, line 52
def inspect
  "(#{@op} #{@version})"
end
to_s() click to toggle source
# File lib/chef/version_constraint.rb, line 56
def to_s
  "#{@op} #{@version}"
end

Private Instance Methods

do_op(other_version) click to toggle source
# File lib/chef/version_constraint.rb, line 67
def do_op(other_version)
  if STANDARD_OPS.include? @op
    other_version.send(@op.to_sym, @version)
  elsif @op == '='
    other_version == @version
  elsif @op == '~>'
    if @missing_patch_level
      (other_version.major == @version.major &&
       other_version.minor >= @version.minor)
    else
      (other_version.major == @version.major &&
       other_version.minor == @version.minor &&
       other_version.patch >= @version.patch)
    end
  else                      # should never happen
    raise "bad op #{@op}"
  end
end
parse(str) click to toggle source
# File lib/chef/version_constraint.rb, line 98
def parse(str)
  @missing_patch_level = false
  if str.index(" ").nil? && str =~ /^[0-9]/
    # try for lone version, implied '='
    @raw_version = str
    @version = self.class::VERSION_CLASS.new(@raw_version)
    @op = "="
  elsif PATTERN.match str
    @op = $1
    @raw_version = $2
    @version = self.class::VERSION_CLASS.new(@raw_version)
    if raw_version.split('.').size <= 2
      @missing_patch_level = true
    end
  else
    raise Chef::Exceptions::InvalidVersionConstraint, "'#{str}'"
  end
end
parse_from_array(constraint_spec) click to toggle source
# File lib/chef/version_constraint.rb, line 86
def parse_from_array(constraint_spec)
  if constraint_spec.empty?
    parse(DEFAULT_CONSTRAINT)
  elsif constraint_spec.size == 1
    parse(constraint_spec.first)
  else
    msg = "only one version constraint operation is supported, but you gave #{constraint_spec.size} "
    msg << "['#{constraint_spec.join(', ')}']"
    raise Chef::Exceptions::InvalidVersionConstraint, msg
  end
end