Class: Library::Version
- Inherits:
-
Object
- Object
- Library::Version
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/library/version.rb,
lib/library/version.rb
Overview
The Library::Version class is essentially a tuple (immutable array) with special comparision operators.
Defined Under Namespace
Classes: Constraint
Class Method Summary collapse
-
.[](*args) ⇒ Object
Convenience alias for ::new.
-
.constraint_lambda(constraint) ⇒ Proc
Parses a string constraint returning the operation as a lambda.
-
.parse_constraint(constraint) ⇒ Array<String>
Converts a constraint into an operator and value.
-
.parse_hash(data) ⇒ Library::Version
Parse common Hash-based version, i.e.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
“Spaceship” comparsion operator.
-
#=~(other) ⇒ Boolean
Pessimistic constraint (like ‘~>’ in gems).
-
#[](index) ⇒ Integer, String
Access indexed segment of version number.
-
#build ⇒ String
Build returns the remaining portions of the version tuple after
patch
joined by ‘.’. -
#each(&block) ⇒ Object
Iterate over each version segment.
-
#initialize(*args) ⇒ Version
constructor
Instantiate new instance of Version.
-
#major ⇒ Object
Major is the first number in the version series.
-
#minor ⇒ Object
Minor is the second number in the version series.
-
#parse_yaml(yaml) ⇒ Object
Parse YAML-based version.
-
#patch ⇒ Object
Patch is third number in the version series.
-
#satisfy?(constraint) ⇒ Boolean
Does this version satisfy a given constraint?.
-
#size ⇒ Integer
Size of version tuple.
-
#to_s ⇒ String
Returns string representation of version, e.g.
-
#to_str ⇒ String
Library::Version is not technically a String-type.
-
#tuple ⇒ Array
protected
The internal tuple modeling the version number.
Constructor Details
#initialize(*args) ⇒ Version
Instantiate new instance of Version.
91 92 93 94 95 |
# File 'lib/library/version.rb', line 91 def initialize(*args) args = args.flatten.compact args = args.join('.').split(/\W+/) @tuple = args.map{ |i| /^\d+$/ =~ i.to_s ? i.to_i : i } end |
Class Method Details
.[](*args) ⇒ Object
Convenience alias for ::new.
13 14 15 |
# File 'lib/library/version.rb', line 13 def self.[](*args) new(*args) end |
.constraint_lambda(constraint) ⇒ Proc
Parses a string constraint returning the operation as a lambda.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/library/version.rb', line 24 def self.constraint_lambda(constraint) op, val = *parse_constraint(constraint) lambda do |t| case t when Version t.send(op, val) else Version.new(t).send(op, val) end end end |
.parse_constraint(constraint) ⇒ Array<String>
Converts a constraint into an operator and value.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/library/version.rb', line 44 def self.parse_constraint(constraint) constraint = constraint.strip re = %r{^(=~|~>|<=|>=|==|=|<|>)?\s*(\d+(:?\.\S+)*)$} if md = re.match(constraint) if op = md[1] op = '=~' if op == '~>' op = '==' if op == '=' val = new(*md[2].split(/\W+/)) else op = '==' val = new(*constraint.split(/\W+/)) end else raise ArgumentError, "invalid constraint" end return op, val end |
.parse_hash(data) ⇒ Library::Version
Parse common Hash-based version, i.e. Jeweler format.
69 70 71 72 73 74 75 76 77 |
# File 'lib/library/version.rb', line 69 def self.parse_hash(data) data = data.inject({}){ |h,(k,v)| h[k.to_sym] = v; h } if data[:major] vers = data.values_at(:major, :minor, :patch, :build) else vers = data[:vers] || data[:version] end new vers end |
Instance Method Details
#<=>(other) ⇒ Integer
“Spaceship” comparsion operator.
140 141 142 143 144 145 146 |
# File 'lib/library/version.rb', line 140 def <=>(other) [size, other.size].max.times do |i| c = self[i] <=> (other[i] || 0) return c if c != 0 end 0 end |
#=~(other) ⇒ Boolean
Pessimistic constraint (like ‘~>’ in gems).
156 157 158 159 160 161 162 163 |
# File 'lib/library/version.rb', line 156 def =~(other) #other = other.to_t upver = other.tuple.dup i = upver.index(0) i = upver.size unless i upver[i-1] += 1 self >= other && self < upver end |
#[](index) ⇒ Integer, String
Access indexed segment of version number. Returns ‘0` if index is non-existant.
127 128 129 |
# File 'lib/library/version.rb', line 127 def [](index) @tuple.fetch(index,0) end |
#build ⇒ String
Build returns the remaining portions of the version tuple after patch
joined by ‘.’.
186 187 188 |
# File 'lib/library/version.rb', line 186 def build @tuple[3..-1].join('.') end |
#each(&block) ⇒ Object
Iterate over each version segment.
193 194 195 |
# File 'lib/library/version.rb', line 193 def each(&block) @tuple.each(&block) end |
#major ⇒ Object
Major is the first number in the version series.
168 |
# File 'lib/library/version.rb', line 168 def major ; @tuple[0] ; end |
#minor ⇒ Object
Minor is the second number in the version series.
173 |
# File 'lib/library/version.rb', line 173 def minor ; @tuple[1] || 0 ; end |
#parse_yaml(yaml) ⇒ Object
Parse YAML-based version. TODO: deprecate ?
83 84 85 86 |
# File 'lib/library/version.rb', line 83 def parse_yaml(yaml) require 'yaml' parse_hash( YAML.load(yaml) ) end |
#patch ⇒ Object
Patch is third number in the version series.
178 |
# File 'lib/library/version.rb', line 178 def patch ; @tuple[2] || 0 ; end |
#satisfy?(constraint) ⇒ Boolean
Does this version satisfy a given constraint?
214 215 216 217 |
# File 'lib/library/version.rb', line 214 def satisfy?(constraint) c = Constraint.parse(constraint) send(c.operator, c.number) end |
#size ⇒ Integer
Size of version tuple.
202 203 204 |
# File 'lib/library/version.rb', line 202 def size @tuple.size end |
#to_s ⇒ String
Returns string representation of version, e.g. “1.0.0”.
102 103 104 |
# File 'lib/library/version.rb', line 102 def to_s @tuple.compact.join('.') end |
#to_str ⇒ String
Library::Version is not technically a String-type. This is here only becuase ‘File.join` calls it instead of #to_s.
112 113 114 |
# File 'lib/library/version.rb', line 112 def to_str @tuple.compact.join('.') end |
#tuple ⇒ Array (protected)
The internal tuple modeling the version number.
226 227 228 |
# File 'lib/library/version.rb', line 226 def tuple @tuple end |