Class: Semverse::Version

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

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, 'rc.1', 'build.1']) => #<Version: @major=1, @minor=2, @patch=3, @pre_release='rc.1', @build='build.1'>

    Parameters:

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

    Examples:

    Version.new("1.2.3-rc.1+build.1") => #<Version: @major=1, @minor=2, @patch=3, @pre_release='rc.1', @build='build.1'>

    Parameters:

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

    Examples:

    Version.new(Version.new("1.2.3-rc.1+build.1")) => #<Version: @major=1, @minor=2, @pre_release='rc.1', @build='build.1'>

    Parameters:


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/semverse/version.rb', line 60

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

  @major       ||= 0
  @minor       ||= 0
  @patch       ||= 0
  @pre_release ||= nil
  @build       ||= nil
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build


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

def build
  @build
end

#majorObject (readonly)

Returns the value of attribute major


36
37
38
# File 'lib/semverse/version.rb', line 36

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor


37
38
39
# File 'lib/semverse/version.rb', line 37

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch


38
39
40
# File 'lib/semverse/version.rb', line 38

def patch
  @patch
end

#pre_releaseObject (readonly)

Returns the value of attribute pre_release


39
40
41
# File 'lib/semverse/version.rb', line 39

def pre_release
  @pre_release
end

Class Method Details

.coerce(object) ⇒ Version

Coerce the object into a version.

Parameters:

Returns:


9
10
11
# File 'lib/semverse/version.rb', line 9

def coerce(object)
  object.is_a?(self) ? object : new(object)
end

.split(version_string) ⇒ Array

Parameters:

  • version_string (#to_s)

Returns:

  • (Array)

Raises:


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/semverse/version.rb', line 18

def split(version_string)
  case version_string.to_s
  when /^(\d+)\.(\d+)\.(\d+)(-([0-9a-z\-\.]+))?(\+([0-9a-z\-\.]+))?$/i
    [ $1.to_i, $2.to_i, $3.to_i, $5, $7 ]
  when /^(\d+)\.(\d+)\.(\d+)?$/
    [ $1.to_i, $2.to_i, $3.to_i ]
  when /^(\d+)\.(\d+)?$/
    [ $1.to_i, $2.to_i, 0 ]
  when /^(\d+)$/
    [ $1.to_i, 0, 0 ]
  else
    raise InvalidVersionFormat.new(version_string)
  end
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/semverse/version.rb', line 77

def <=>(other)
  [:major, :minor, :patch].each do |release|
    ans = self.send(release) <=> other.send(release)
    return ans if ans != 0
  end
  ans = pre_release_and_build_presence_score <=> other.pre_release_and_build_presence_score
  return ans if ans != 0
  ans = identifiers_comparaison(other, :pre_release)
  return ans if ans != 0
  if build && other.build
    return identifiers_comparaison(other, :build)
  else
    return build.to_s <=> other.build.to_s
  end
  0
end

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

134
135
136
# File 'lib/semverse/version.rb', line 134

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

#identifiers(release) ⇒ Array

Returns:

  • (Array)

95
96
97
98
99
# File 'lib/semverse/version.rb', line 95

def identifiers(release)
  send(release).to_s.split('.').map do |str|
    str.to_i.to_s == str ? str.to_i : str
  end
end

#identifiers_comparaison(other, release) ⇒ Integer

Parameters:

Returns:

  • (Integer)

117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/semverse/version.rb', line 117

def identifiers_comparaison(other, release)
  [identifiers(release).length, other.identifiers(release).length].max.times do |i|
    if identifiers(release)[i].class == other.identifiers(release)[i].class
      ans = identifiers(release)[i] <=> other.identifiers(release)[i]
      return ans if ans != 0
    elsif identifiers(release)[i] && other.identifiers(release)[i]
      return identifiers(release)[i].class.to_s <=> other.identifiers(release)[i].class.to_s
    elsif identifiers(release)[i] || other.identifiers(release)[i]
      return other.identifiers(release)[i].class.to_s <=> identifiers(release)[i].class.to_s
    end
  end
  0
end

#inspectObject


138
139
140
# File 'lib/semverse/version.rb', line 138

def inspect
  "#<#{self.class.to_s} #{to_s}>"
end

#pre_release?Boolean

Returns:

  • (Boolean)

101
102
103
# File 'lib/semverse/version.rb', line 101

def pre_release?
  !!pre_release
end

#pre_release_and_build_presence_scoreInteger

Returns:

  • (Integer)

110
111
112
# File 'lib/semverse/version.rb', line 110

def pre_release_and_build_presence_score
  pre_release ? 0 : (build.nil? ? 1 : 2)
end

#to_sObject


142
143
144
145
146
147
# File 'lib/semverse/version.rb', line 142

def to_s
  str = "#{major}.#{minor}.#{patch}"
  str += "-#{pre_release}" if pre_release
  str += "+#{build}" if build
  str
end

#zero?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'lib/semverse/version.rb', line 105

def zero?
  [major, minor, patch].all? { |n| n == 0 }
end