Module: Sinclair::Comparable::ClassMethods

Defined in:
lib/sinclair/comparable/class_methods.rb

Overview

Class methods of Sinclair::Comparable

Examples:

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

Author:

  • darthjee

Instance Method Summary collapse

Instance Method Details

#comparable_by(*attributes) ⇒ Set<Symbol,String>

Adds fields to the comparison algorythim

Examples:

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

Parameters:

  • attributes (Array<Symbol>)

    attributes to be added to comparison

Returns:

  • (Set<Symbol,String>)

See Also:



19
20
21
# File 'lib/sinclair/comparable/class_methods.rb', line 19

def comparable_by(*attributes)
  equals_checker.add(*attributes)
end

#equals_checkerSinclair::EqualsChecker

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a comparable configured for the class



28
29
30
# File 'lib/sinclair/comparable/class_methods.rb', line 28

def equals_checker
  @equals_checker ||= Sinclair::EqualsChecker.new
end