Sha256: 9640856560d7a878ad8ec63ff63c591e5ea3b9228b604e4277fba893ddade6d1

Contents?: true

Size: 648 Bytes

Versions: 192

Compression:

Stored size: 648 Bytes

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/common_concern/module_setup'

module EacRubyUtils
  class CompareBy
    attr_reader :fields

    def initialize(fields)
      @fields = fields
    end

    def apply(klass)
      pself = self
      klass.include(::Comparable)
      %i[<=> eql?].each do |cmp_method|
        klass.define_method(cmp_method) do |other|
          pself.object_values(self).send(cmp_method, pself.object_values(other))
        end
      end
      klass.define_method(:hash) { pself.object_values(self).hash }
    end

    def object_values(object)
      fields.map { |field| object.send(field) }
    end
  end
end

Version data entries

192 entries across 192 versions & 3 rubygems

Version Path
eac_tools-0.10.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_tools-0.9.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_ruby_utils-0.95.3 lib/eac_ruby_utils/compare_by.rb
eac_tools-0.8.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_tools-0.7.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_tools-0.6.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_tools-0.5.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_ruby_utils-0.95.2 lib/eac_ruby_utils/compare_by.rb
eac_tools-0.4.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.120.2 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.120.1 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.120.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.119.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.118.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.117.3 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
avm-tools-0.117.2 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_ruby_utils-0.95.1 lib/eac_ruby_utils/compare_by.rb
avm-tools-0.117.1 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb
eac_ruby_utils-0.95.0 lib/eac_ruby_utils/compare_by.rb
avm-tools-0.117.0 sub/eac_ruby_utils/lib/eac_ruby_utils/compare_by.rb