Class: Semver::String

Inherits:
Object
  • Object
show all
Defined in:
lib/semver-string/string.rb

Overview

A helper class for working with semantic versioning strings.

version = Semver::String.new('1.2.3-alpha+build-123')

version.major #=> 1
version.major #=> 2
version.patch #=> 3
version.pre_release #=> alpha
version. #=> build-123
version.to_s #=> "1.2.3-alpha+build-123"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major:, minor:, patch:, pre_release: nil, build_metadata: nil) ⇒ String

Returns a new instance of String.

Parameters:

  • major (Integer)
  • minor (Integer)
  • patch (Integer)
  • pre_release (String, nil) (defaults to: nil)
  • build_metadata (String, nil) (defaults to: nil)

36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/semver-string/string.rb', line 36

def initialize(
  major:,
  minor:,
  patch:,
  pre_release: nil,
  build_metadata: nil
)
  @major = major
  @minor = minor
  @patch = patch
  @pre_release = pre_release
  @build_metadata = 
  @string = format_string
end

Instance Attribute Details

#build_metadataString? (readonly)

Returns:


64
65
66
# File 'lib/semver-string/string.rb', line 64

def 
  @build_metadata
end

#majorInteger (readonly)

Returns:

  • (Integer)

52
53
54
# File 'lib/semver-string/string.rb', line 52

def major
  @major
end

#minorInteger (readonly)

Returns:

  • (Integer)

55
56
57
# File 'lib/semver-string/string.rb', line 55

def minor
  @minor
end

#patchInteger (readonly)

Returns:

  • (Integer)

58
59
60
# File 'lib/semver-string/string.rb', line 58

def patch
  @patch
end

#pre_releaseString? (readonly)

Returns:


61
62
63
# File 'lib/semver-string/string.rb', line 61

def pre_release
  @pre_release
end

Class Method Details

.parse(string) ⇒ Version

Parameters:

Returns:

  • (Version)

25
26
27
28
29
# File 'lib/semver-string/string.rb', line 25

def self.parse(string)
  semver = allocate
  semver.send(:initialize, Parser.new.parse(string))
  semver
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns:

  • (Integer)

    Returns -1, 0, or 1.


90
91
92
# File 'lib/semver-string/string.rb', line 90

def <=>(other)
  Comparator.new.compare(left: self, right: other)
end

#==(other) ⇒ Boolean

Returns true if the string value of the two objects are equal.

Returns:

  • (Boolean)

    Returns true if the string value of the two objects are equal.


84
85
86
# File 'lib/semver-string/string.rb', line 84

def ==(other)
  to_s == other.to_s
end

#to_strString Also known as: to_s

Returns:


67
68
69
# File 'lib/semver-string/string.rb', line 67

def to_str
  @string
end