Class: Factorix::MODVersion

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/factorix/mod_version.rb,
lib/factorix/mod_version.rb

Overview

Represent a 3-component MOD version number (major.minor.patch)

This class represents Factorio’s MOD version format, which uses 24 bits (3 x 8-bit unsigned integers) to store version information.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#majorInteger (readonly)

Returns major version number (0-255).

Returns:

  • (Integer)

    major version number (0-255)



22
23
24
# File 'lib/factorix/mod_version.rb', line 22

def major
  @major
end

#minorInteger (readonly)

Returns minor version number (0-255).

Returns:

  • (Integer)

    minor version number (0-255)



22
23
24
# File 'lib/factorix/mod_version.rb', line 22

def minor
  @minor
end

#patchInteger (readonly)

Returns patch version number (0-255).

Returns:

  • (Integer)

    patch version number (0-255)



22
23
24
# File 'lib/factorix/mod_version.rb', line 22

def patch
  @patch
end

Class Method Details

.from_numbers(major, minor, patch) ⇒ MODVersion

Create MODVersion from three integers

Parameters:

  • major (Integer)

    major version number (0-255)

  • minor (Integer)

    minor version number (0-255)

  • patch (Integer)

    patch version number (0-255)

Returns:

Raises:



69
70
71
72
73
74
75
# File 'lib/factorix/mod_version.rb', line 69

def self.from_numbers(major, minor, patch)
  validate_component(major, :major)
  validate_component(minor, :minor)
  validate_component(patch, :patch)

  new(major:, minor:, patch:)
end

.from_string(str) ⇒ MODVersion

Create MODVersion from version string “X.Y.Z” or “X.Y”

Accepts both 3-part (X.Y.Z) and 2-part (X.Y) version strings. For 2-part versions, patch defaults to 0.

Parameters:

  • str (String)

    version string in “X.Y.Z” or “X.Y” format

Returns:

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/factorix/mod_version.rb', line 42

def self.from_string(str)
  if /\A(\d+)\.(\d+)\.(\d+)\z/ =~ str
    major = Integer($1, 10)
    minor = Integer($2, 10)
    patch = Integer($3, 10)
  elsif /\A(\d+)\.(\d+)\z/ =~ str
    major = Integer($1, 10)
    minor = Integer($2, 10)
    patch = 0
  else
    raise VersionParseError, "invalid version string: #{str.inspect}"
  end

  validate_component(major, :major)
  validate_component(minor, :minor)
  validate_component(patch, :patch)

  new(major:, minor:, patch:)
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compare with another MODVersion

Parameters:

Returns:

  • (Integer, nil)

    -1, 0, 1 for less than, equal to, greater than; nil if not comparable



93
94
95
96
97
# File 'lib/factorix/mod_version.rb', line 93

def <=>(other)
  return nil unless other.is_a?(MODVersion)

  to_a <=> other.to_a
end

#to_aArray<Integer>

Convert to array of integers

Returns:

  • (Array<Integer>)

    Array containing [major, minor, patch]



87
# File 'lib/factorix/mod_version.rb', line 87

def to_a = [major, minor, patch].freeze

#to_sString

Convert to string representation

Returns:

  • (String)

    Version string in format “X.Y.Z”



82
# File 'lib/factorix/mod_version.rb', line 82

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