Class: Babushka::VersionStr
- Includes:
- Comparable
- Defined in:
- lib/babushka/version_str.rb
Constant Summary collapse
- GemVersionOperators =
%w[= == != > < >= <= ~>].freeze
Instance Attribute Summary collapse
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#pieces ⇒ Object
readonly
Returns the value of attribute pieces.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(str) ⇒ VersionStr
constructor
A new instance of VersionStr.
- #to_s ⇒ Object
Constructor Details
#initialize(str) ⇒ VersionStr
Returns a new instance of VersionStr.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/babushka/version_str.rb', line 15 def initialize str @operator, @version = str.strip.scan(/^([^\s\w\-\.]+)?\s*v?([\w\-\.]+)$/i).first if !(@operator.nil? || GemVersionOperators.include?(@operator)) raise InvalidVersionOperator, "VersionStr.new('#{str}'): invalid operator '#{@operator}'." elsif !self.class.parseable_version?(@version) raise InvalidVersionStr, "VersionStr.new('#{str}'): couldn't parse a version number." else @pieces = @version.strip.scan(/\d+|[a-zA-Z]+|\w+/).map {|piece| piece[/^\d+$/] ? piece.to_i : piece } @operator = '==' if @operator.nil? || @operator == '=' end end |
Instance Attribute Details
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
8 9 10 |
# File 'lib/babushka/version_str.rb', line 8 def operator @operator end |
#pieces ⇒ Object (readonly)
Returns the value of attribute pieces.
8 9 10 |
# File 'lib/babushka/version_str.rb', line 8 def pieces @pieces end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
8 9 10 |
# File 'lib/babushka/version_str.rb', line 8 def version @version end |
Class Method Details
.parseable_version?(str) ⇒ Boolean
11 12 13 |
# File 'lib/babushka/version_str.rb', line 11 def self.parseable_version? str !str.nil? && !str[/\d|HEAD/].nil? end |
Instance Method Details
#<=>(other) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/babushka/version_str.rb', line 34 def <=> other other = other.to_version unless other.is_a? VersionStr max_length = [pieces.length, other.pieces.length].max (0...max_length).to_a.pick {|index| result = compare_pieces pieces[index], other.pieces[index] result unless result == 0 } || 0 end |
#to_s ⇒ Object
30 31 32 |
# File 'lib/babushka/version_str.rb', line 30 def to_s @operator == '==' ? @version : "#{@operator} #{@version}" end |