Class: Library::Version::Constraint

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



288
289
290
291
292
293
294
295
296
297
# File 'lib/library/version.rb', line 288

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.



303
304
305
# File 'lib/library/version.rb', line 303

def number
  @number
end

#operatorObject (readonly)

Constraint operator.



300
301
302
# File 'lib/library/version.rb', line 300

def operator
  @operator
end

Class Method Details

.[](operator, number) ⇒ Object



283
284
285
# File 'lib/library/version.rb', line 283

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

.constraint_lambda(constraint) ⇒ Object

Parses a string constraint returning the operation as a lambda.



373
374
375
# File 'lib/library/version.rb', line 373

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

.parse(constraint) ⇒ Object



277
278
279
280
# File 'lib/library/version.rb', line 277

def self.parse(constraint)
  return constraint if self === constraint
  new(constraint)
end

.parse_constraint(constraint) ⇒ Object

Parses a string constraint returning the operator and value.



378
379
380
381
# File 'lib/library/version.rb', line 378

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

Instance Method Details

#compare(version) ⇒ Object



330
331
332
# File 'lib/library/version.rb', line 330

def compare(version)
  version.send(operator, number)
end

#parse(constraint) ⇒ Object (private)



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/library/version.rb', line 337

def parse(constraint)
  case constraint
  when Array
    op, num = constraint
  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.new(*val) #Version::Number.new(*val)
end

#to_gem_versionObject

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



315
316
317
318
# File 'lib/library/version.rb', line 315

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.



322
323
324
325
326
327
# File 'lib/library/version.rb', line 322

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

#to_sObject



306
307
308
# File 'lib/library/version.rb', line 306

def to_s
  @stamp
end