Class: Versionaire::Version

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

Overview

An immutable, semantic version value object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Version.



22
23
24
25
26
27
# File 'lib/versionaire/version.rb', line 22

def initialize major: 0, minor: 0, maintenance: 0
  @major = major
  @minor = minor
  @maintenance = maintenance
  validate
end

Instance Attribute Details

#maintenanceObject (readonly)

Returns the value of attribute maintenance.



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

def maintenance
  @maintenance
end

#majorObject (readonly)

Returns the value of attribute major.



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

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



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

def minor
  @minor
end

Class Method Details

.arguments(major, minor, maintenance) ⇒ Object



18
19
20
# File 'lib/versionaire/version.rb', line 18

def self.arguments major, minor, maintenance
  Hash[keys.zip [major, minor, maintenance]]
end

.formatObject



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

def self.format
  /\Av?\d{1,}\.\d{1,}\.\d{1,}\z/
end

.keysObject



10
11
12
# File 'lib/versionaire/version.rb', line 10

def self.keys
  i(major minor maintenance)
end

Instance Method Details

#+(other) ⇒ Object



29
30
31
# File 'lib/versionaire/version.rb', line 29

def + other
  self.class.new self.class.arguments(*reduce(other, :+))
end

#-(other) ⇒ Object



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

def - other
  self.class.new self.class.arguments(*reduce(other, :-))
end

#<=>(other) ⇒ Object



42
43
44
# File 'lib/versionaire/version.rb', line 42

def <=> other
  to_s <=> other.to_s
end

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



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

def == other
  other.is_a?(Version) && to_s == other.to_s
end

#hashObject



46
47
48
# File 'lib/versionaire/version.rb', line 46

def hash
  [major, minor, maintenance, self.class].hash
end

#labelObject



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

def label
  "v#{self}"
end

#to_aObject



59
60
61
# File 'lib/versionaire/version.rb', line 59

def to_a
  [major, minor, maintenance]
end

#to_hObject



63
64
65
# File 'lib/versionaire/version.rb', line 63

def to_h
  {major: major, minor: minor, maintenance: maintenance}
end

#to_sObject Also known as: to_str



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

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