Class: Epuber::Version
- Inherits:
-
Object
- Object
- Epuber::Version
- Includes:
- Comparable
- Defined in:
- lib/epuber/vendor/version.rb
Instance Attribute Summary collapse
- #segments ⇒ Array<Numeric> readonly
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
-
.correct?(version) ⇒ Boolean
True if the
versionstring matches RubyGems’ requirements.
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric
Compares this version with
otherreturning -1, 0, or 1 if the other version is larger, the same, or smaller than this one. -
#initialize(version) ⇒ Version
constructor
A new instance of Version.
- #to_s ⇒ String
Constructor Details
#initialize(version) ⇒ Version
Returns a new instance of Version.
26 27 28 29 30 31 32 |
# File 'lib/epuber/vendor/version.rb', line 26 def initialize(version) unless self.class.correct?(version) raise StandardError, "Malformed version number string #{version}" end @version = version.to_s.strip end |
Instance Attribute Details
#segments ⇒ Array<Numeric> (readonly)
36 37 38 |
# File 'lib/epuber/vendor/version.rb', line 36 def segments @segments end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
21 22 23 |
# File 'lib/epuber/vendor/version.rb', line 21 def version @version end |
Class Method Details
.correct?(version) ⇒ Boolean
True if the version string matches RubyGems’ requirements.
17 18 19 |
# File 'lib/epuber/vendor/version.rb', line 17 def self.correct?(version) version.to_s =~ VERSION_RE end |
Instance Method Details
#<=>(other) ⇒ Numeric
Compares this version with other returning -1, 0, or 1 if the other version is larger, the same, or smaller than this one.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/epuber/vendor/version.rb', line 54 def <=>(other) return unless other.is_a?(Version) || other.is_a?(String) || other.is_a?(Float) || other.is_a?(Fixnum) other = other.is_a?(Version) ? other : Version.new(other) return 0 if @version == other.version lhsegments = segments rhsegments = other.segments lhsize = lhsegments.size rhsize = rhsegments.size limit = (lhsize > rhsize ? lhsize : rhsize) - 1 i = 0 while i <= limit lhs, rhs = lhsegments[i] || 0, rhsegments[i] || 0 i += 1 next if lhs == rhs return -1 if lhs.is_a?(String) && is_number(rhs) return 1 if is_number(lhs) && rhs.is_a?(String) return lhs <=> rhs end 0 end |
#to_s ⇒ String
44 45 46 |
# File 'lib/epuber/vendor/version.rb', line 44 def to_s "#{segments.join('.')}" end |