Sha256: 938ec6f4a74889e37b4151120c924898159e719e663d23b46eed541170a0c89c

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

class Version
  include Comparable

  # Version component names
  NAMES = [:major, :minor, :tiny, :patch].freeze

  attr_accessor *NAMES

  def initialize(value)
    @major, @minor, @tiny, @patch = begin
      if value.respond_to?(:to_ary)
        value.to_ary.map(&:to_i)
      elsif value.respond_to?(:to_version)
        value.to_version.to_a
      else
        value.to_s.split('.').map(&:to_i)
      end
    end
  end

  # Implicit conversion method
  def to_version
    self
  end

  def to_s
    [major, minor, tiny, patch].compact.join('.')
  end
  alias :to_str :to_s

  def to_f
    to_s.to_f
  end

  def to_a
    NAMES.map { |name| send(name) }.compact
  end
  alias :to_ary :to_a

  # Version components comparison method. Uses Comparable to assess whether
  # This Version's component value or the other Version's component value is
  # greater or lesser. The first value to be found as greater or lesser
  # determines which Version object is greater or lesser.
  #
  # Missing Version components are treated as 0 values, which effectively gives
  # them no weight in the comparison.
  #
  # @params [Version] other the other Version object we are comparing with
  def <=>(other)
    NAMES.each do |name|
      result = send(name).to_i <=> other.send(name).to_i
      return result unless result.zero?
    end
    0
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
version_compare-0.0.1 lib/version_compare/version.rb