Class: Yast::Ops::GenericComparable Deprecated

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
src/ruby/yast/ops.rb

Overview

Deprecated.

use native ruby comparing, comparing various class usually is not usable.

Generic comparator that can compare various classes together like yast, order is based on yast class order.

Constant Summary

CLASS_ORDER =

ordered classes from low priority to high

[::NilClass, ::FalseClass, ::TrueClass, ::Integer, ::Float,
::String, Yast::Path, ::Symbol, ::Array, Yast::Term, ::Hash].freeze

Instance Method Summary collapse

Constructor Details

#initialize(value, localized = false) ⇒ GenericComparable



488
489
490
491
# File 'src/ruby/yast/ops.rb', line 488

def initialize(value, localized = false)
  @value = value
  @localized = localized
end

Instance Method Details

#<=>(other) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'src/ruby/yast/ops.rb', line 495

def <=>(other)
  if @value.class == other.class
    case @value
    when ::Array
      ListComparator.new(@value, @localized) <=> other
    when ::NilClass
      0 # comparison of two nils is equality
    when ::Hash
      HashComparator.new(@value, @localized) <=> other
    when ::String
      @localized ? Yast.strcoll(@value, other) : (@value <=> other)
    else
      @value <=> other
    end
  else
    return @value <=> other if @value.is_a?(::Numeric) && other.is_a?(::Numeric)

    # workaround for older ruby versions which have value.is_a?(Integer) but value.class => Fixnum
    # No longer problem with ruby 2.4
    order = CLASS_ORDER.index(@value.class) || CLASS_ORDER.index(::Integer)
    other_order = CLASS_ORDER.index(other.class) || CLASS_ORDER.index(::Integer)
    order <=> other_order
  end
end