Class: GemHadar::VersionSpec
- Inherits:
-
Object
- Object
- GemHadar::VersionSpec
- Includes:
- Comparable
- Defined in:
- lib/gem_hadar/version_spec.rb
Overview
A class that represents a version specification for a gem.
This class provides functionality to parse and manipulate version strings, including handling of semantic versioning formats and optional ‘v’ prefixes. It supports creating version specifications from various input formats and provides methods to access the underlying version information and string representation.
Instance Attribute Summary collapse
-
#version ⇒ Tins::StringVersion::Version?
readonly
Retrieves the parsed version object.
Class Method Summary collapse
-
.[](spec, with_prefix: nil, without_prefix: nil) ⇒ GemHadar::VersionSpec
The [] method creates a new VersionSpec instance from a specification.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
The <=> method compares this version specification with another object.
-
#eql?(other) ⇒ TrueClass, FalseClass
The eql? method checks for equality between this version specification and another object.
-
#hash ⇒ Integer
The hash method computes a hash value for the version specification.
-
#head? ⇒ Boolean
The head? method checks if the version string represents a HEAD reference.
-
#initialize(string, version) ⇒ VersionSpec
constructor
Initializes a new VersionSpec instance.
-
#tag ⇒ String
The tag method returns the version tag string with appropriate prefixing.
-
#to_s ⇒ String
Returns the original string representation.
-
#untag ⇒ String
The untag method returns the version string without a ‘v’ prefix.
-
#version? ⇒ Boolean
Checks if a version object was successfully parsed.
-
#with_prefix ⇒ String
Adds a ‘v’ prefix to the version string if not already present.
-
#without_prefix ⇒ String
Removes the ‘v’ prefix from the version string if present.
Constructor Details
#initialize(string, version) ⇒ VersionSpec
Initializes a new VersionSpec instance.
74 75 76 |
# File 'lib/gem_hadar/version_spec.rb', line 74 def initialize(string, version) @string, @version = string, version end |
Instance Attribute Details
#version ⇒ Tins::StringVersion::Version? (readonly)
Retrieves the parsed version object.
81 82 83 |
# File 'lib/gem_hadar/version_spec.rb', line 81 def version @version end |
Class Method Details
.[](spec, with_prefix: nil, without_prefix: nil) ⇒ GemHadar::VersionSpec
The [] method creates a new VersionSpec instance from a specification.
This factory method attempts to extract version information from the provided specification, handling both gem specifications and version strings. It supports optional prefix handling for the resulting version string.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gem_hadar/version_spec.rb', line 42 def [](spec, with_prefix: nil, without_prefix: nil) !(with_prefix && without_prefix) or raise ArgumentError, 'with_prefix and without_prefix is invalid' spec.is_a?(self) and return spec obj, version = nil, (spec.version rescue nil) if version ; elsif /\Av?(\d+\.\d+\.\d+)\z/ =~ spec begin version = Tins::StringVersion::Version.new($1) rescue ArgumentError end end spec = spec.to_s if version if with_prefix spec = spec.sub(/\A(?!v)/, 'v') elsif without_prefix spec = spec.sub(/\Av?/, '') end end obj = new(spec, version) obj.freeze end |
Instance Method Details
#<=>(other) ⇒ Integer
The <=> method compares this version specification with another object.
This method implements the comparison operator for VersionSpec objects, allowing them to be sorted and compared using standard Ruby comparison operators.
158 159 160 161 162 |
# File 'lib/gem_hadar/version_spec.rb', line 158 def <=>(other) other.is_a?(self.class) or raise TypeError, "other needs to be a #{self.class}" other.version or raise TypeError, "cannot compare to #{other.inspect}" version <=> other.version end |
#eql?(other) ⇒ TrueClass, FalseClass
The eql? method checks for equality between this version specification and another object.
This method determines if the current VersionSpec instance is equal to another object by first checking if the other object is of the same class. If so, it compares either both objects represent HEAD references or their underlying version objects for equality.
176 177 178 |
# File 'lib/gem_hadar/version_spec.rb', line 176 def eql?(other) other.is_a?(self.class) && (head? && other.head? || version == other.version) end |
#hash ⇒ Integer
The hash method computes a hash value for the version specification.
This method returns a hash code that represents the version specification. If the version specification has a valid version object, it returns the hash of that version object. Otherwise, it returns the hash of the string representation of the version specification.
188 189 190 191 192 193 194 |
# File 'lib/gem_hadar/version_spec.rb', line 188 def hash if version? version.hash else to_s.hash end end |
#head? ⇒ Boolean
The head? method checks if the version string represents a HEAD reference.
This method returns true if the internal string representation of the version spec is exactly ‘HEAD’, indicating that it refers to the latest commit rather than a specific tagged version.
97 98 99 |
# File 'lib/gem_hadar/version_spec.rb', line 97 def head? @string == 'HEAD' end |
#tag ⇒ String
The tag method returns the version tag string with appropriate prefixing.
This method checks if the version represents a HEAD reference and returns the raw string representation if so. Otherwise, it returns the version string with a ‘v’ prefix added to it.
108 109 110 |
# File 'lib/gem_hadar/version_spec.rb', line 108 def tag head? ? to_s : with_prefix end |
#to_s ⇒ String
Returns the original string representation.
127 128 129 |
# File 'lib/gem_hadar/version_spec.rb', line 127 def to_s @string.to_s end |
#untag ⇒ String
The untag method returns the version string without a ‘v’ prefix.
This method checks if the version represents a HEAD reference and returns the raw string representation if so. Otherwise, it returns the version string with the ‘v’ prefix removed.
120 121 122 |
# File 'lib/gem_hadar/version_spec.rb', line 120 def untag head? ? to_s : without_prefix end |
#version? ⇒ Boolean
Checks if a version object was successfully parsed.
86 87 88 |
# File 'lib/gem_hadar/version_spec.rb', line 86 def version? !!@version end |
#with_prefix ⇒ String
Adds a ‘v’ prefix to the version string if not already present.
141 142 143 |
# File 'lib/gem_hadar/version_spec.rb', line 141 def with_prefix to_s.sub(/\A(?!v)/, 'v') end |
#without_prefix ⇒ String
Removes the ‘v’ prefix from the version string if present.
134 135 136 |
# File 'lib/gem_hadar/version_spec.rb', line 134 def without_prefix to_s.sub(/\Av/, '') end |