Class: Library::Version

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • constraint (String)

    valid version constraint, e.g. “>1.0” and “1.0+”

Returns:

  • (Proc)

    procedure for making contraint comparisons



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.

Parameters:

  • valid (String)

    version constraint , e.g. “= 2.1.3”

Returns:

  • (Array<String>)

    operator and version number pair



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.

Parameters:

  • version (Hash)

    hash

Returns:



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.

Parameters:

  • other (Library::Version, Array)

    a Library::Version or equvalent Array to compare

Returns:

  • (Integer)

    ‘-1`, `0`, or `1` for less, equal or greater, respectively



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).

Parameters:

Returns:

  • (Boolean)

    match pessimistic constraint?



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.

Parameters:

  • index (Integer)

    a segment index of the version

Returns:

  • (Integer, String)

    version segment



127
128
129
# File 'lib/library/version.rb', line 127

def [](index)
  @tuple.fetch(index,0)
end

#buildString

Build returns the remaining portions of the version tuple after patch joined by ‘.’.

Returns:

  • (String)

    version segments after the 3rd in point-format



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

#majorObject

Major is the first number in the version series.



168
# File 'lib/library/version.rb', line 168

def major ; @tuple[0] ; end

#minorObject

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

#patchObject

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?

Returns:

  • (Boolean)


214
215
216
217
# File 'lib/library/version.rb', line 214

def satisfy?(constraint)
  c = Constraint.parse(constraint)
  send(c.operator, c.number)
end

#sizeInteger

Size of version tuple.

Returns:

  • (Integer)

    number of segments



202
203
204
# File 'lib/library/version.rb', line 202

def size
  @tuple.size
end

#to_sString

Returns string representation of version, e.g. “1.0.0”.

Returns:

  • (String)

    version number in dot format



102
103
104
# File 'lib/library/version.rb', line 102

def to_s
  @tuple.compact.join('.')
end

#to_strString

Library::Version is not technically a String-type. This is here only becuase ‘File.join` calls it instead of #to_s.

Returns:

  • (String)

    version number in dot format



112
113
114
# File 'lib/library/version.rb', line 112

def to_str
  @tuple.compact.join('.')
end

#tupleArray (protected)

The internal tuple modeling the version number.

Returns:

  • (Array)

    internal tuple representing the version



226
227
228
# File 'lib/library/version.rb', line 226

def tuple
  @tuple
end