Class: Chef::Sugar::Constraints::Version

Inherits:
String show all
Defined in:
lib/chef/sugar/constraints.rb

Overview

This class exposes a single version constraint object that wraps the string representation of a version string and proved helpful comparator methods.

Examples:

Create a new version

Chef::Sugar::Version('1.2.3')

Compare a version with constraints

Chef::Sugar::Version('1.2.3').satisfies?('~> 1.3.4', '< 2.0.5')

Instance Method Summary collapse

Methods inherited from String

#flush, #satisfied_by?

Constructor Details

#initialize(version) ⇒ Version

Create a new version object.

Parameters:

  • version (String)

    the version to create



81
82
83
84
# File 'lib/chef/sugar/constraints.rb', line 81

def initialize(version)
  super
  @version = Gem::Version.new(version)
end

Instance Method Details

#satisfies?(*constraints) ⇒ Boolean

Determine if the given constraint is satisfied by this version.

Examples:

Given a satisified version

Version.new('1.2.5').satisfies?('~> 1.2.0') #=> true

Given an unsatisfied version

Version.new('2.0.0').satisfies?('~> 1.2.0') #=> false

Parameters:

Returns:

  • (Boolean)

    true if the version satisfies the constraints, false otherwise



102
103
104
# File 'lib/chef/sugar/constraints.rb', line 102

def satisfies?(*constraints)
  Gem::Requirement.new(*constraints).satisfied_by?(@version)
end