Class: Versionaire::Version

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

Overview

An immutable, semantic version value object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major: 0, minor: 0, patch: 0) ⇒ Version

Returns a new instance of Version.



14
15
16
17
18
# File 'lib/versionaire/version.rb', line 14

def initialize major: 0, minor: 0, patch: 0
  super
  validate
  freeze
end

Instance Attribute Details

#majorObject

Returns the value of attribute major

Returns:

  • (Object)

    the current value of major



8
9
10
# File 'lib/versionaire/version.rb', line 8

def major
  @major
end

#minorObject

Returns the value of attribute minor

Returns:

  • (Object)

    the current value of minor



8
9
10
# File 'lib/versionaire/version.rb', line 8

def minor
  @minor
end

#patchObject

Returns the value of attribute patch

Returns:

  • (Object)

    the current value of patch



8
9
10
# File 'lib/versionaire/version.rb', line 8

def patch
  @patch
end

Instance Method Details

#+(other) ⇒ Object



24
# File 'lib/versionaire/version.rb', line 24

def +(other) = (revalue(other.to_h) { |previous, current| previous + current }).freeze

#-(other) ⇒ Object



26
# File 'lib/versionaire/version.rb', line 26

def -(other) = (revalue(other.to_h) { |previous, current| previous - current }).freeze

#<=>(other) ⇒ Object



32
# File 'lib/versionaire/version.rb', line 32

def <=>(other) = to_s <=> other.to_s

#==(other) ⇒ Object Also known as: eql?



28
# File 'lib/versionaire/version.rb', line 28

def ==(other) = hash == other.hash

#[]=(key, value) ⇒ Object



20
21
22
# File 'lib/versionaire/version.rb', line 20

def []= key, value
  super.tap { validate }
end

#bump(key) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/versionaire/version.rb', line 42

def bump key
  case key
    when :major then bump_major
    when :minor then bump_minor
    when :patch then bump_patch
    else fail Error, %(Invalid key: #{key.inspect}. Use: #{members.to_sentence "or"}.)
  end
end

#down(key, value = 1) ⇒ Object



34
35
36
# File 'lib/versionaire/version.rb', line 34

def down key, value = 1
  (revalue(key => value) { |previous, current| previous - current }).freeze
end

#inspectObject



51
# File 'lib/versionaire/version.rb', line 51

def inspect = to_s.inspect

#to_procObject



53
# File 'lib/versionaire/version.rb', line 53

def to_proc = method(:[]).to_proc

#to_sObject Also known as: to_str



55
# File 'lib/versionaire/version.rb', line 55

def to_s = to_a.join DELIMITER

#up(key, value = 1) ⇒ Object



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

def up key, value = 1
  (revalue(key => value) { |previous, current| previous + current }).freeze
end