Class: Solve::Version

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

Overview

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_array) ⇒ Version #initialize(version_string) ⇒ Version #initialize(version) ⇒ Version

Returns a new instance of Version.

Overloads:

  • #initialize(version_array) ⇒ Version

    Examples:

    Version.new([1, 2, 3]) => #<Version: @major=1, @minor=2, @patch=3>

    Parameters:

    • version_array (Array)
  • #initialize(version_string) ⇒ Version

    Examples:

    Version.new("1.2.3") => #<Version: @major=1, @minor=2, @patch=3>

    Parameters:

    • version_string (#to_s)
  • #initialize(version) ⇒ Version

    Examples:

    Version.new(Version.new("1.2.3")) => #<Version: @major=1, @minor=2, @patch=3>

    Parameters:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solve/version.rb', line 46

def initialize(*args)
  if args.first.is_a?(Array)
    @major, @minor, @patch = args.first
  else
    @major, @minor, @patch = self.class.split(args.first.to_s)
  end

  @major ||= 0
  @minor ||= 0
  @patch ||= 0
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



24
25
26
# File 'lib/solve/version.rb', line 24

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



25
26
27
# File 'lib/solve/version.rb', line 25

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



26
27
28
# File 'lib/solve/version.rb', line 26

def patch
  @patch
end

Class Method Details

.split(version_string) ⇒ Array

Parameters:

  • version_string (#to_s)

Returns:

  • (Array)

Raises:

  • (InvalidVersionFormat)


10
11
12
13
14
15
16
17
18
19
# File 'lib/solve/version.rb', line 10

def split(version_string)
  major, minor, patch = case version_string.to_s
  when /^(\d+)\.(\d+)\.(\d+)$/
    [ $1.to_i, $2.to_i, $3.to_i ]
  when /^(\d+)\.(\d+)$/
    [ $1.to_i, $2.to_i, nil ]
  else
    raise Errors::InvalidVersionFormat.new(version_string)
  end
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)


61
62
63
64
65
66
67
# File 'lib/solve/version.rb', line 61

def <=>(other)
  [:major, :minor, :patch].each do |method|
    ans = (self.send(method) <=> other.send(method))
    return ans if ans != 0
  end
  0
end

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


72
73
74
# File 'lib/solve/version.rb', line 72

def eql?(other)
  other.is_a?(Version) && self == other
end

#inspectObject



76
77
78
# File 'lib/solve/version.rb', line 76

def inspect
  to_s
end

#to_sObject



80
81
82
# File 'lib/solve/version.rb', line 80

def to_s
  "#{major}.#{minor}.#{patch}"
end