Class: Chef::Sugar::Constraints::Constraint

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

Overview

This class is a wrapper around a version requirement that adds a nice DSL for comparing constraints:

Examples:

Comparing a single constraint

Constraint.new('~> 1.2.3').satisfied_by?('1.2.7')

Comparing multiple constraints

Constraint.new('> 1.2.3', '< 2.0.0').satisfied_by?('1.2.7')

Instance Method Summary collapse

Constructor Details

#initialize(*constraints) ⇒ Constraint

Create a new constraint object.

Parameters:


37
38
39
# File 'lib/chef/sugar/constraints.rb', line 37

def initialize(*constraints)
  @requirement = Gem::Requirement.new(*constraints)
end

Instance Method Details

#satisfied_by?(version) ⇒ Boolean

Determine if the given version string is satisfied by this constraint or group of constraints.

Examples:

Given a satisified constraint

Constraint.new('~> 1.2.0').satisfied_by?('1.2.5') #=> true

Given an unsatisfied constraint

Constraint.new('~> 1.2.0').satisfied_by?('2.0.0') #=> false

Parameters:

  • version (String)

    the version to compare

Returns:

  • (Boolean)

    true if the constraint is satisfied, false otherwise


58
59
60
# File 'lib/chef/sugar/constraints.rb', line 58

def satisfied_by?(version)
  @requirement.satisfied_by?(Gem::Version.new(version))
end