Class: ImageOptim::BinResolver::SimpleVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/image_optim/bin_resolver/simple_version.rb

Overview

Allows comparision of simple versions, only numbers separated by dots are taken into account

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ SimpleVersion

Initialize with a string or an object convertible to string

SimpleVersion.new('2.0.1') <=> SimpleVersion.new(2)


14
15
16
17
# File 'lib/image_optim/bin_resolver/simple_version.rb', line 14

def initialize(str)
  @str = String(str)
  @parts = @str.split('.').map(&:to_i).reverse.drop_while(&:zero?).reverse
end

Instance Attribute Details

#partsObject (readonly)

Numbers extracted from version string



9
10
11
# File 'lib/image_optim/bin_resolver/simple_version.rb', line 9

def parts
  @parts
end

Instance Method Details

#<=>(other) ⇒ Object

Compare version parts of self with other



25
26
27
28
# File 'lib/image_optim/bin_resolver/simple_version.rb', line 25

def <=>(other)
  other = self.class.new(other) unless other.is_a?(self.class)
  parts <=> other.parts
end

#to_sObject

Returns original version string



20
21
22
# File 'lib/image_optim/bin_resolver/simple_version.rb', line 20

def to_s
  @str
end