Class: ComparableVersion
- Inherits:
-
Object
- Object
- ComparableVersion
- Includes:
- Comparable
- Defined in:
- lib/version_compare/comparable_version.rb
Constant Summary collapse
- DEFAULT_SEPARATOR =
".".freeze
- NAMES =
ComparableVersion component names
[:major, :minor, :tiny, :patch].freeze
Instance Attribute Summary collapse
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
ComparableVersion components comparison method.
-
#initialize(value, options = {}) ⇒ ComparableVersion
constructor
A new instance of ComparableVersion.
- #inspect ⇒ Object
- #to_a ⇒ Object (also: #to_ary)
-
#to_comparable_version ⇒ Object
Implicit conversion method.
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(value, options = {}) ⇒ ComparableVersion
Returns a new instance of ComparableVersion.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/version_compare/comparable_version.rb', line 12 def initialize(value, = {}) @separator = .fetch(:separator) { DEFAULT_SEPARATOR } @major, @minor, @tiny, @patch = if value.respond_to?(:to_comparable_version) value.to_comparable_version.to_a elsif value.respond_to?(:to_ary) value.to_ary.map(&:to_i) else value.to_s.split(separator).map(&:to_i) end end |
Instance Attribute Details
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
10 11 12 |
# File 'lib/version_compare/comparable_version.rb', line 10 def separator @separator end |
Instance Method Details
#<=>(other) ⇒ Object
ComparableVersion components comparison method. Uses Comparable to assess whether This ComparableVersion’s component value or the other ComparableVersion’s component value is greater or lesser. The first value to be found as greater or lesser determines which ComparableVersion object is greater or lesser.
Missing ComparableVersion components are treated as 0 values, which effectively gives them no weight in the comparison.
53 54 55 56 57 58 59 60 |
# File 'lib/version_compare/comparable_version.rb', line 53 def <=>(other) NAMES.each do |name| result = send(name).to_i <=> other.send(name).to_i return result unless result.zero? end 0 end |
#inspect ⇒ Object
25 26 27 |
# File 'lib/version_compare/comparable_version.rb', line 25 def inspect "<#{identification}>" end |
#to_a ⇒ Object Also known as: to_ary
39 40 41 |
# File 'lib/version_compare/comparable_version.rb', line 39 def to_a NAMES.map { |name| public_send(name) }.compact end |
#to_comparable_version ⇒ Object
Implicit conversion method
30 31 32 |
# File 'lib/version_compare/comparable_version.rb', line 30 def to_comparable_version self end |
#to_s ⇒ Object Also known as: to_str
34 35 36 |
# File 'lib/version_compare/comparable_version.rb', line 34 def to_s NAMES.map { |name| public_send(name) }.compact.join(separator) end |