Class: Prick::Version
- Inherits:
-
Object
- Object
- Prick::Version
- Includes:
- Comparable
- Defined in:
- lib/prick/version.rb
Constant Summary collapse
- PRE_LABEL =
"pre"
- PRE_RE =
/^#{PRE_LABEL}\.(\d+)$/
Instance Attribute Summary collapse
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#feature ⇒ Object
Returns the value of attribute feature.
-
#semver ⇒ Object
Returns the value of attribute semver.
Class Method Summary collapse
-
.version?(string) ⇒ Boolean
Return true if ‘string` is a version.
- .zero ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #clone ⇒ Object
-
#custom? ⇒ Boolean
Return true if this is a custom release.
- #dup ⇒ Object
- #eql?(other) ⇒ Boolean
-
#feature? ⇒ Boolean
Return true if this is a feature release.
- #hash ⇒ Object
-
#increment(part, pre_initial_value = 1) ⇒ Object
‘part` can be one of :major, :minor, :patch, or :pre.
-
#initialize(version, custom: nil, feature: nil) ⇒ Version
constructor
A new instance of Version.
-
#inspect ⇒ Object
Render as string.
- #major ⇒ Object
- #major=(major) ⇒ Object
- #minor ⇒ Object
- #minor=(minor) ⇒ Object
- #patch ⇒ Object
- #patch=(patch) ⇒ Object
-
#pre ⇒ Object
The releases is stored as a String (eg. ‘pre.1’) in the semantic version but #pre returns only the Integer number.
-
#pre=(pre) ⇒ Object
#pre= expects an integer or nil argument.
-
#pre? ⇒ Boolean
Return true if this is a pre-release.
-
#release? ⇒ Boolean
Return true if this is a release branch.
-
#to_s ⇒ Object
Render as String.
- #truncate(part) ⇒ Object
- #zero? ⇒ Boolean
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
#custom ⇒ Object
Returns the value of attribute custom.
25 26 27 |
# File 'lib/prick/version.rb', line 25 def custom @custom end |
#feature ⇒ Object
Returns the value of attribute feature.
27 28 29 |
# File 'lib/prick/version.rb', line 27 def feature @feature end |
#semver ⇒ Object
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
23 |
# File 'lib/prick/version.rb', line 23 def self.version?(string) (string =~ VERSION_RE).nil? ? false : true 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 |
#custom? ⇒ Boolean
Return true if this is a custom release
39 |
# File 'lib/prick/version.rb', line 39 def custom?() !@custom.nil? end |
#eql?(other) ⇒ 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
42 |
# File 'lib/prick/version.rb', line 42 def feature?() !@feature.nil? end |
#hash ⇒ Object
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 |
#inspect ⇒ Object
Render as string
127 |
# File 'lib/prick/version.rb', line 127 def inspect() to_s end |
#major ⇒ Object
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 |
#minor ⇒ Object
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 |
#patch ⇒ Object
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 |
#pre ⇒ Object
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
48 |
# File 'lib/prick/version.rb', line 48 def pre?() !@semver.pre.nil? end |
#release? ⇒ Boolean
Return true if this is a release branch
45 |
# File 'lib/prick/version.rb', line 45 def release?() !feature? end |
#to_s ⇒ Object
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 |