Class: Version
- Inherits:
-
Object
- Object
- Version
- Includes:
- Comparable
- Defined in:
- lib/asrake/version/version.rb
Overview
Encodes version-numbering logic into a convenient class.
Defined Under Namespace
Classes: Component
Class Method Summary collapse
-
.current(path = nil) ⇒ Object
name as
pathto autodetect within a given directory. -
.to_version(obj) ⇒ Object
Converts a String, Hash, or Array into a Version instance.
-
.version_file(filename) ⇒ Object
Attempts to detect the version file for the passed
filename.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares a Version against any
otherobject that responds toto_version. -
#[]=(index, value) ⇒ Object
Set the component of the Version at
indextovalue. - #bump(component = -1,, pre = false, trim = false) ⇒ Object
-
#bump!(component = -1,, pre = false, trim = false) ⇒ Object
Bumps the version number.
-
#initialize(major, minor = 0, revision = nil, *rest) ⇒ Version
constructor
Creates a new version number, with a
majorversion number,minorrevision number,revisionnumber, and optionally more (unnamed) version components. -
#inspect ⇒ Object
Returns a human-friendly version format.
-
#length ⇒ Object
Returns the current length of the version number.
- #prerelease? ⇒ Boolean
-
#resize!(length) ⇒ Object
Resizes the Version to
length, removing any trailing components. -
#to_a ⇒ Object
Converts the version number into an array of its components.
-
#to_hash ⇒ Object
Converts the version number into a hash of its components.
-
#to_s ⇒ Object
The canonical representation of a version number.
-
#to_version ⇒ Object
Returns
self. -
#to_yaml ⇒ Object
Returns a YAML representation of the version number.
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.
83 84 85 |
# File 'lib/asrake/version/version.rb', line 83 def initialize(major, minor = 0, revision = nil, *rest) self.components = [ major, minor, revision, *rest ] end |
Class Method Details
.current(path = nil) ⇒ Object
name as path to autodetect within a given directory
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/asrake/version/version.rb', line 39 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 Version.to_version(path.read.strip) when '.yml' then Version.to_version(YAML::load(path.read)) end end |
.to_version(obj) ⇒ Object
Converts a String, Hash, or Array into a Version instance
68 69 70 71 72 73 74 75 76 |
# File 'lib/asrake/version/version.rb', line 68 def self.to_version(obj) if obj.kind_of? String Version.new *obj.split(%r{\.}) elsif obj.kind_of? Hash Version.new *obj.values_at(:major, :minor, :revision, :rest) elsif obj.kind_of? Array Version.new *obj 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.
58 59 60 61 62 63 |
# File 'lib/asrake/version/version.rb', line 58 def self.version_file(filename) Pathname(filename).dirname..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.
184 185 186 |
# File 'lib/asrake/version/version.rb', line 184 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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/asrake/version/version.rb', line 106 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
169 170 171 |
# File 'lib/asrake/version/version.rb', line 169 def bump(component = -1, pre = false, trim = false) return (Version.to_version(self.to_hash)).bump!(component, pre, trim) end |
#bump!(component = -1,, pre = false, trim = false) ⇒ Object
Bumps the version number. 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’
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/asrake/version/version.rb', line 148 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 |
#inspect ⇒ Object
Returns a human-friendly version format.
230 231 232 |
# File 'lib/asrake/version/version.rb', line 230 def inspect self.to_s.inspect end |
#length ⇒ Object
Returns the current length of the version number.
176 177 178 |
# File 'lib/asrake/version/version.rb', line 176 def length self.components.length end |
#prerelease? ⇒ Boolean
123 124 125 |
# File 'lib/asrake/version/version.rb', line 123 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.
131 132 133 134 |
# File 'lib/asrake/version/version.rb', line 131 def resize!(length) self.components = self.components.take(length) self end |
#to_a ⇒ Object
Converts the version number into an array of its components.
191 192 193 |
# File 'lib/asrake/version/version.rb', line 191 def to_a self.components.map {|c| c.to_s } end |
#to_hash ⇒ Object
Converts the version number into a hash of its components.
198 199 200 201 202 203 204 |
# File 'lib/asrake/version/version.rb', line 198 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_s ⇒ Object
The canonical representation of a version number.
209 210 211 |
# File 'lib/asrake/version/version.rb', line 209 def to_s self.to_a.join('.') end |
#to_version ⇒ Object
Returns self.
216 217 218 |
# File 'lib/asrake/version/version.rb', line 216 def to_version self end |
#to_yaml ⇒ Object
Returns a YAML representation of the version number.
223 224 225 |
# File 'lib/asrake/version/version.rb', line 223 def to_yaml YAML::dump(self.to_hash) end |