Class: Version::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/versus/constraint.rb

Overview

The Constraint class models a single version equality or inequality. It consists of the operator and the version number. – TODO: Please improve me!

TODO: This should ultimately replace the class methods of Version::Number.

TODO: Do we need to support version “from-to” spans ? ++

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint) ⇒ Constraint

Returns a new instance of Constraint.



25
26
27
28
29
30
31
32
33
34
# File 'lib/versus/constraint.rb', line 25

def initialize(constraint)
  @operator, @number = parse(constraint || '0+')

  case constraint
  when Array
    @stamp = "%s %s" % [@operator, @number]
  when String
    @stamp = constraint || '0+'
  end
end

Instance Attribute Details

#numberObject (readonly)

Verison number.



40
41
42
# File 'lib/versus/constraint.rb', line 40

def number
  @number
end

#operatorObject (readonly)

Constraint operator.



37
38
39
# File 'lib/versus/constraint.rb', line 37

def operator
  @operator
end

Class Method Details

.[](operator, number) ⇒ Object



20
21
22
# File 'lib/versus/constraint.rb', line 20

def self.[](operator, number)
  new([operator, number])
end

.constraint_lambda(constraint) ⇒ Object

Parses a string constraint returning the operation as a lambda.



118
119
120
# File 'lib/versus/constraint.rb', line 118

def self.constraint_lambda(constraint)
  new(constraint).to_proc
end

.parse(constraint) ⇒ Object



15
16
17
# File 'lib/versus/constraint.rb', line 15

def self.parse(constraint)
  new(constraint)
end

.parse_constraint(constraint) ⇒ Object

Parses a string constraint returning the operator and value.



123
124
125
126
# File 'lib/versus/constraint.rb', line 123

def self.parse_constraint(constraint)
  c = new(constraint)
  return c.operator, c.number
end

Instance Method Details

#call(v) ⇒ Object Also known as: match?



69
70
71
72
# File 'lib/versus/constraint.rb', line 69

def call(v)
  n = Version::Number.parse(v)
  n.send(operator, number)
end

#parse(constraint) ⇒ Object (private)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/versus/constraint.rb', line 80

def parse(constraint)
  case constraint
  when Constraint
    op, val = constraint.operator, constraint.number
  when Array
    op, num = constraint
  when /^(.*?)\~$/
    op, val = "=~", $1.strip
  when /^(.*?)\+$/
    op, val = ">=", $1.strip
  when /^(.*?)\-$/
    op, val = "<", $1
  when /^(=~|~>|<=|>=|==|=|<|>)?\s*(\d+(:?[-.]\w+)*)$/
    if op = $1
      op = '=~' if op == '~>'
      op = '==' if op == '='
      val = $2.split(/\W+/)
    else
      op = '=='
      val = constraint.split(/\W+/)
    end
  else
    raise ArgumentError #constraint.split(/\s+/)
  end
  return op, Version::Number.new(*val)
end

#to_gem_versionObject

Converts the version into a constraint string recognizable by RubyGems. – TODO: Better name Constraint#to_s2. ++



52
53
54
55
# File 'lib/versus/constraint.rb', line 52

def to_gem_version
  op = (operator == '=~' ? '~>' : operator)
  "%s %s" % [op, number]
end

#to_procObject

Convert constraint to Proc object which can be used to test a version number.



61
62
63
64
65
66
# File 'lib/versus/constraint.rb', line 61

def to_proc
  lambda do |v|
    n = Version::Number.parse(v)
    n.send(operator, number)
  end
end

#to_sObject



43
44
45
# File 'lib/versus/constraint.rb', line 43

def to_s
  @stamp
end