Sha256: 3573229bc8c098b861e0cab5d7c00688a6ad3843037330d7bb8cb2a32a7a8a5e

Contents?: true

Size: 1.13 KB

Versions: 18

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'sinclair/comparable/class_methods'

class Sinclair
  # @api public
  # @author darthjee
  #
  # Concern to be added on classes for easy +==+ comparison
  #
  # @example
  #   class SampleModel
  #     include Sinclair::Comparable
  #
  #     comparable_by :name
  #     attr_reader :name, :age
  #
  #     def initialize(name: nil, age: nil)
  #       @name = name
  #       @age  = age
  #     end
  #   end
  #
  #   model1 = model_class.new(name: 'jack', age: 21)
  #   model2 = model_class.new(name: 'jack', age: 23)
  #
  #   model1 == model2 # returns true
  module Comparable
    extend ActiveSupport::Concern

    # Checks if an instance of a comparable is equals another
    #
    # @param other [Object] an object that should be equal to comparable
    #
    # @return [Boolean]
    # @example (see Sinclair::Comparable)
    def ==(other)
      klass = self.class
      superklass = klass.superclass

      return false unless klass.equals_checker.match?(self, other)
      return true unless superklass.include?(Sinclair::Comparable)

      superklass.equals_checker.match?(self, other)
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
sinclair-2.1.1 lib/sinclair/comparable.rb
sinclair-2.1.0 lib/sinclair/comparable.rb
sinclair-2.0.1 lib/sinclair/comparable.rb
sinclair-2.0.0 lib/sinclair/comparable.rb
sinclair-1.16.3 lib/sinclair/comparable.rb
sinclair-1.16.2 lib/sinclair/comparable.rb
sinclair-1.16.1 lib/sinclair/comparable.rb
sinclair-1.16.0 lib/sinclair/comparable.rb
sinclair-1.15.0 lib/sinclair/comparable.rb
sinclair-1.14.2 lib/sinclair/comparable.rb
sinclair-1.14.1 lib/sinclair/comparable.rb
sinclair-1.14.0 lib/sinclair/comparable.rb
sinclair-1.13.0 lib/sinclair/comparable.rb
sinclair-1.12.1 lib/sinclair/comparable.rb
sinclair-1.12.0 lib/sinclair/comparable.rb
sinclair-1.11.0 lib/sinclair/comparable.rb
sinclair-1.10.0 lib/sinclair/comparable.rb
sinclair-1.9.0 lib/sinclair/comparable.rb