Class: Prick::Version

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

Constant Summary collapse

PRE_LABEL =
"pre"
PRE_RE =
/^#{PRE_LABEL}\.(\d+)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, custom: nil, feature: nil) ⇒ Version

Returns a new instance of Version.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/prick/version.rb', line 63

def initialize(version, custom: nil, feature: nil)
  case version
    when String
      version =~ VERSION_RE
      self.class.version?(version) or raise "Expected a version, got #{version.inspect}"
      @custom = custom || $1
      @semver = Semantic::Version.new($2)
      @feature = feature || $3
    when Semantic::Version
      @custom = custom
      @semver = version.dup
      @feature = feature
    when Version
      @custom = custom || version.custom
      @semver = version.semver.dup
      @feature = feature || version.feature
  else
    raise "Expected a String, Version, or Semantic::Version, got #{version.class}"
  end
end

Instance Attribute Details

#customObject

Returns the value of attribute custom.



25
26
27
# File 'lib/prick/version.rb', line 25

def custom
  @custom
end

#featureObject

Returns the value of attribute feature.



27
28
29
# File 'lib/prick/version.rb', line 27

def feature
  @feature
end

#semverObject

Returns the value of attribute semver.



26
27
28
# File 'lib/prick/version.rb', line 26

def semver
  @semver
end

Class Method Details

.version?(string) ⇒ Boolean

Return true if ‘string` is a version. If true, it sets the Regex capture groups 1-3. See also Constants::VERSION_RE

Returns:

  • (Boolean)


23
# File 'lib/prick/version.rb', line 23

def self.version?(string) (string =~ VERSION_RE).nil? ? false : true end

.zeroObject



18
# File 'lib/prick/version.rb', line 18

def self.zero() Version.new("0.0.0") end

Instance Method Details

#<=>(other) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/prick/version.rb', line 112

def <=>(other)
  r = (custom || "") <=> (other.custom || "")
  return r if r != 0
  r = semver <=> other.semver
  return r if r != 0
  r = (feature || "") <=> (other.feature || "")
  return r
end

#cloneObject



58
# File 'lib/prick/version.rb', line 58

def clone() Version.new(self) end

#custom?Boolean

Return true if this is a custom release

Returns:

  • (Boolean)


39
# File 'lib/prick/version.rb', line 39

def custom?() !@custom.nil? end

#dupObject



57
# File 'lib/prick/version.rb', line 57

def dup() Version.new(self) end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


60
# File 'lib/prick/version.rb', line 60

def eql?(other) self == other end

#feature?Boolean

Return true if this is a feature release

Returns:

  • (Boolean)


42
# File 'lib/prick/version.rb', line 42

def feature?() !@feature.nil? end

#hashObject



61
# File 'lib/prick/version.rb', line 61

def hash() @semver.hash end

#increment(part, pre_initial_value = 1) ⇒ Object

‘part` can be one of :major, :minor, :patch, or :pre. If pre is undefined, it is set to `pre_initial_value`



86
87
88
89
90
91
92
93
94
# File 'lib/prick/version.rb', line 86

def increment(part, pre_initial_value = 1) 
  if part == :pre
    v = self.dup
    v.pre = (v.pre ? v.pre+1 : pre_initial_value)
    v
  else
    Version.new(semver.increment!(part), custom: custom, feature: feature)
  end
end

#inspectObject

Render as string



127
# File 'lib/prick/version.rb', line 127

def inspect() to_s end

#majorObject



29
# File 'lib/prick/version.rb', line 29

def major() @semver.major end

#major=(major) ⇒ Object



30
# File 'lib/prick/version.rb', line 30

def major=(major) @semver.major = major end

#minorObject



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

def minor() @semver.minor end

#minor=(minor) ⇒ Object



33
# File 'lib/prick/version.rb', line 33

def minor=(minor) @semver.minor = minor end

#patchObject



35
# File 'lib/prick/version.rb', line 35

def patch() @semver.patch end

#patch=(patch) ⇒ Object



36
# File 'lib/prick/version.rb', line 36

def patch=(patch) @semver.patch = patch end

#preObject

The releases is stored as a String (eg. ‘pre.1’) in the semantic version but #pre returns only the Integer number



52
# File 'lib/prick/version.rb', line 52

def pre() @semver.pre =~ PRE_RE ? $1.to_i : nil end

#pre=(pre) ⇒ Object

#pre= expects an integer or nil argument



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

def pre=(pre) @semver.pre = (pre ? "#{PRE_LABEL}.#{pre}" : nil) end

#pre?Boolean

Return true if this is a pre-release

Returns:

  • (Boolean)


48
# File 'lib/prick/version.rb', line 48

def pre?() !@semver.pre.nil? end

#release?Boolean

Return true if this is a release branch

Returns:

  • (Boolean)


45
# File 'lib/prick/version.rb', line 45

def release?() !feature? end

#to_sObject

Render as String



122
123
124
# File 'lib/prick/version.rb', line 122

def to_s
  (custom ? "#{custom}-" : "") + semver.to_s + (feature ? "_#{feature}" : "")
end

#truncate(part) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/prick/version.rb', line 96

def truncate(part)
  case part
    when :pre
      v = self.dup
      v.feature = nil
      v.pre = nil
      v
    when :feature
      v = self.dup
      v.feature = nil
      v
  else
    raise NotYet
  end
end

#zero?Boolean

Returns:

  • (Boolean)


19
# File 'lib/prick/version.rb', line 19

def zero?() self == Version.zero end