Class: Indexer::Version::Constraint
- Inherits:
-
Object
- Object
- Indexer::Version::Constraint
- 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
-
#number ⇒ Object
readonly
Verison number.
-
#operator ⇒ Object
readonly
Constraint operator.
Class Method Summary collapse
- .[](operator, number) ⇒ Object
-
.constraint_lambda(constraint) ⇒ Object
Parses a string constraint returning the operation as a lambda.
- .parse(constraint) ⇒ Object
-
.parse_constraint(constraint) ⇒ Object
Parses a string constraint returning the operator and value.
Instance Method Summary collapse
-
#initialize(constraint) ⇒ Constraint
constructor
A new instance of Constraint.
-
#to_gem_version ⇒ Object
Converts the version into a constraint string recognizable by RubyGems.
-
#to_proc ⇒ Object
Convert constraint to Proc object which can be used to test a version number.
- #to_s ⇒ Object
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
#number ⇒ Object (readonly)
Verison number.
43 44 45 |
# File 'lib/indexer/version/constraint.rb', line 43 def number @number end |
#operator ⇒ Object (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
#to_gem_version ⇒ Object
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_proc ⇒ Object
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_s ⇒ Object
46 47 48 |
# File 'lib/indexer/version/constraint.rb', line 46 def to_s @stamp end |