Class: Indexer::Version::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/indexer/version/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.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/indexer/version/constraint.rb', line 27

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

  case constraint
  when Array
    @stamp = "%s %s" % [@operator, @number]
  when String
    constraint = "0+" if constraint.strip == ""
    @stamp = constraint
  end
end

Instance Attribute Details

#numberObject (readonly)

Verison number.



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

def number
  @number
end

#operatorObject (readonly)

Constraint operator.



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

def operator
  @operator
end

Class Method Details

.[](operator, number) ⇒ Object



22
23
24
# File 'lib/indexer/version/constraint.rb', line 22

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

.constraint_lambda(constraint) ⇒ Object

Parses a string constraint returning the operation as a lambda.



110
111
112
# File 'lib/indexer/version/constraint.rb', line 110

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

.parse(constraint) ⇒ Object



17
18
19
# File 'lib/indexer/version/constraint.rb', line 17

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

.parse_constraint(constraint) ⇒ Object

Parses a string constraint returning the operator and value.



115
116
117
118
# File 'lib/indexer/version/constraint.rb', line 115

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

Instance Method Details

#parse(constraint) ⇒ Object (private)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/indexer/version/constraint.rb', line 72

def parse(constraint)
  case constraint
  when Array
    op, num = constraint
  when ""
    op, val = ">=", "0"
  when /^(.*?)\~$/
    op, val = "=~", $1
  when /^(.*?)\+$/
    op, val = ">=", $1
  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. ++



55
56
57
58
# File 'lib/indexer/version/constraint.rb', line 55

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.



62
63
64
65
66
67
# File 'lib/indexer/version/constraint.rb', line 62

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

#to_sObject



46
47
48
# File 'lib/indexer/version/constraint.rb', line 46

def to_s
  @stamp
end