Class: MilkTea::PackageVersion

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

Constant Summary collapse

VERSION_PATTERN =
/\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch) ⇒ PackageVersion

Returns a new instance of PackageVersion.



28
29
30
31
32
# File 'lib/milk_tea/packages/version.rb', line 28

def initialize(major, minor, patch)
  @major = Integer(major)
  @minor = Integer(minor)
  @patch = Integer(patch)
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



11
12
13
# File 'lib/milk_tea/packages/version.rb', line 11

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



11
12
13
# File 'lib/milk_tea/packages/version.rb', line 11

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



11
12
13
# File 'lib/milk_tea/packages/version.rb', line 11

def patch
  @patch
end

Class Method Details

.parse(value, label: "package version") ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/milk_tea/packages/version.rb', line 13

def self.parse(value, label: "package version")
  return value if value.is_a?(self)

  text = value.to_s.strip
  raise PackageVersionError, "#{label} cannot be empty" if text.empty?

  match = VERSION_PATTERN.match(text)
  unless match
    raise PackageVersionError,
          "#{label} #{text.inspect} must use semantic version format MAJOR.MINOR.PATCH"
  end

  new(match[1].to_i, match[2].to_i, match[3].to_i)
end

Instance Method Details

#<=>(other) ⇒ Object



34
35
36
37
38
# File 'lib/milk_tea/packages/version.rb', line 34

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

  [major, minor, patch] <=> [other.major, other.minor, other.patch]
end

#to_sObject



40
41
42
# File 'lib/milk_tea/packages/version.rb', line 40

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