Class: Prick::Version

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

Defined Under Namespace

Classes: FormatError

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.



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 64

def initialize(version, custom: nil, feature: nil)
  case version
    when String
      version =~ VERSION_RE or raise Version::FormatError, "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 Internal, "Expected a String, Version, or Semantic::Version, got #{version.class}"
  end
end

Instance Attribute Details

#customObject

Returns the value of attribute custom.



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

def custom
  @custom
end

#featureObject

Returns the value of attribute feature.



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

def feature
  @feature
end

#semverObject

Returns the value of attribute semver.



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

def semver
  @semver
end

Class Method Details

.try(version) ⇒ Object

Try converting the string version into a Version object. Return nil if unsuccessful



85
86
87
# File 'lib/prick/version.rb', line 85

def self.try(version)
  version?(version) ? new(version) : nil
end

.version?(string) ⇒ Boolean

Return true if string is a version

Returns:

  • (Boolean)


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

def self.version?(string) !(string =~ VERSION_RE).nil? end

.zeroObject



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

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

Instance Method Details

#<=>(other) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/prick/version.rb', line 132

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



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

def clone() Version.new(self) end

#custom?Boolean

Return true if this is a custom release

Returns:

  • (Boolean)


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

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

#dupObject



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

def dup() Version.new(self) end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other) self == other end

#feature?Boolean

Return true if this is a feature release

Returns:

  • (Boolean)


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

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

#hashObject



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

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



91
92
93
# File 'lib/prick/version.rb', line 91

def increment(part, pre_initial_value = 1) 
  self.dup.increment!(part, pre_initial_value)
end

#increment!(part, pre_initial_value = 1) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/prick/version.rb', line 95

def increment!(part, pre_initial_value = 1)
  if part == :pre
    self.pre = (self.pre ? self.pre + 1 : pre_initial_value)
  else
    @semver = semver.increment!(part)
  end
  self
end

#inspectObject

Render as string



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

def inspect() to_s end


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

def link
  !feature? or raise Internal, "Version #{to_s} is a feature, not a release"
  File.join(RELEASE_DIR, to_s)
end

#majorObject



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

def major() @semver.major end

#major=(major) ⇒ Object



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

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

#minorObject



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

def minor() @semver.minor end

#minor=(minor) ⇒ Object



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

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

#patchObject



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

def patch() @semver.patch end

#patch=(patch) ⇒ Object



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

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

#pathObject



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

def path
  parts = [FEATURE_DIR, truncate(:pre), feature].compact
  File.join(*parts)
end

#preObject

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



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

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

#pre=(pre) ⇒ Object

#pre= expects an integer or nil argument



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

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

#pre?Boolean

Return true if this is a pre-release

Returns:

  • (Boolean)


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

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

#release?Boolean

Return true if this is a release branch (and not a prerelease)

Returns:

  • (Boolean)


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

def release?() !feature? && !pre? end

#tagObject



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

def tag() "v#{to_s}" end

#to_sObject

Render as String



142
143
144
# File 'lib/prick/version.rb', line 142

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

#truncate(part) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/prick/version.rb', line 104

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)


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

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