Class: Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/version.rb,
lib/version.rb

Overview

Encodes version-numbering logic into a convenient class.

Defined Under Namespace

Classes: Component

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor = 0, revision = nil, *rest) ⇒ Version

Creates a new version number, with a major version number, minor revision number, revision number, and optionally more (unnamed) version components.



54
55
56
# File 'lib/version.rb', line 54

def initialize(major, minor = 0, revision = nil, *rest)
  self.components = [ major, minor, revision, *rest ]
end

Class Method Details

.current(path = nil) ⇒ Object

Searches through the parent directories of the calling method and looks for a VERSION or VERSION.yml file to parse out the current version. Pass

Pass a filename to path to override autodetection, or pass a directory name as path to autodetect within a given directory



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/version.rb', line 23

def self.current(path = nil)
  # if path is nil, detect automatically; if path is a directory, detect
  # automatically in the directory; if path is a filename, use it directly
  path = path ? Pathname.new(path) : self.version_file(caller.first)
  path = self.version_file(path) unless path.nil? or path.file?

  return nil unless path

  case path.extname
    when ''      then path.read.strip.to_version
    when '.yml'  then YAML::load(path.read).to_version
  end
end

.version_file(filename) ⇒ Object

Attempts to detect the version file for the passed filename. Looks up the directory hierarchy for a file named VERSION or VERSION.yml. Returns a Pathname for the file if found, otherwise nil.



42
43
44
45
46
47
# File 'lib/version.rb', line 42

def self.version_file(filename)
  Pathname(filename).dirname.expand_path.ascend do |d|
    break d.join('VERSION')     if d.join('VERSION').file?
    break d.join('VERSION.yml') if d.join('VERSION.yml').file?
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compares a Version against any other object that responds to to_version.



159
160
161
# File 'lib/version.rb', line 159

def <=>(other)
  self.components <=> other.to_version.components
end

#[]=(index, value) ⇒ Object

Set the component of the Version at index to value. Zeroes out any trailing components.

If index is greater than the length of the version number, pads the version number with zeroes until index.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/version.rb', line 77

def []=(index, value)
  return self.resize!(index)               if value.nil? || value.to_s.empty?
  return self[self.length + index] = value if index < 0

  length = self.length - index
  zeroes = Array.new length.abs, Version::Component.new('0')
  value  = Version::Component.new(value.to_s)

  if length >= 0
    self.components[index, length] = zeroes
    self.components[index]         = value
  else
    self.components += zeroes
    self.components << value
  end
end

#bump(component = -1,, pre = false, trim = false) ⇒ Object

Bumps the version number.



144
145
146
# File 'lib/version.rb', line 144

def bump(component = -1, pre = false, trim = false)
  self.dup.bump!(component, pre, trim)
end

#bump!(component = -1,, pre = false, trim = false) ⇒ Object

Bumps the version number and replaces the current object. Pass component to bump a component other than the least-significant part. Set pre to true if you want to bump the component to a prerelease version. Set trim to true if you want the version to be resized to only large enough to contain the component set.

"1.0.4a".bump!                       # => '1.0.4'
"1.0.4a".bump!(:pre)                 # => '1.0.4b'
"1.0.4a".bump!(:minor, false, true)  # => '1.1'
"1.0.4a".bump!(:minor, true, true)   # => '1.1a
"1.0.4a".bump!(:minor, true, false)  # => '1.1.0a'


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/version.rb', line 120

def bump!(component = -1, pre = false, trim = false)
  case component
    when :major    then self.bump!(0,  pre,  trim)
    when :minor    then self.bump!(1,  pre,  trim)
    when :revision then self.bump!(2,  pre,  trim)
    when :pre      then self.bump!(-1, true, trim)
    else
      # resize to match the new length, if applicable
      self.resize!(component + 1) if (trim or component >= self.length)

      # mark all but the changed bit as non-prerelease
      self[0...component].each(&:unprerelease!)

      # I don't even understand this part any more; god help you
      self[component] = self[component].next if     pre and self.prerelease? and component == self.length - 1
      self[component] = self[component].next unless pre and self.prerelease? and component == -1
      self[-1]        = self[-1].next(true)  if pre
      self
  end
end

#inspectObject

Returns a human-friendly version format.



205
206
207
# File 'lib/version.rb', line 205

def inspect
  self.to_s.inspect
end

#lengthObject

Returns the current length of the version number.



151
152
153
# File 'lib/version.rb', line 151

def length
  self.components.length
end

#prerelease?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/version.rb', line 94

def prerelease?
  self.components.any? {|c| c.prerelease? }
end

#resize!(length) ⇒ Object

Resizes the Version to length, removing any trailing components. Is a no-op if length is greater than its current length.



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

def resize!(length)
  self.components = self.components.take(length)
  self
end

#to_aObject

Converts the version number into an array of its components.



166
167
168
# File 'lib/version.rb', line 166

def to_a
  self.components.map {|c| c.to_s }
end

#to_hashObject

Converts the version number into a hash of its components.



173
174
175
176
177
178
179
# File 'lib/version.rb', line 173

def to_hash
  { :major    => self.major,
    :minor    => self.minor,
    :revision => self.revision,
    :rest     => self.length > 3 ? self.to_a.drop(3) : nil }.
    delete_if {|k,v| v.nil? }
end

#to_sObject

The canonical representation of a version number.



184
185
186
# File 'lib/version.rb', line 184

def to_s
  self.to_a.join('.')
end

#to_versionObject

Returns self.



191
192
193
# File 'lib/version.rb', line 191

def to_version
  self
end

#to_yamlObject

Returns a YAML representation of the version number.



198
199
200
# File 'lib/version.rb', line 198

def to_yaml
  YAML::dump(self.to_hash)
end