Class: Sinclair::EqualsChecker
- Inherits:
-
Object
- Object
- Sinclair::EqualsChecker
- Defined in:
- lib/sinclair/equals_checker.rb
Overview
Class responsible for checking if two instances of a class are the equals
Instance Method Summary collapse
-
#add(*attributes) ⇒ Set<Symbol,String>
Adds new fields to equals checker.
-
#initialize(*attributes) ⇒ EqualsChecker
constructor
A new instance of EqualsChecker.
-
#match?(model, other) ⇒ TrueClass, FalseClass
Returns if 2 objects are equals.
Constructor Details
#initialize(*attributes) ⇒ EqualsChecker
Returns a new instance of EqualsChecker.
52 53 54 |
# File 'lib/sinclair/equals_checker.rb', line 52 def initialize(*attributes) @attributes = Set.new(attributes.flatten) end |
Instance Method Details
#add(*attributes) ⇒ Set<Symbol,String>
Adds new fields to equals checker
checker.match?(model1, model2) # returns true
checker.add(:age)
checker.match?(model1, model2) # returns false
73 74 75 |
# File 'lib/sinclair/equals_checker.rb', line 73 def add(*attributes) @attributes += Set.new(attributes.flatten) end |
#match?(model, other) ⇒ TrueClass, FalseClass
Returns if 2 objects are equals
The check takes into consideration:
- They should be instances of the same class
- Their attributes (method calls) should return the same value,
for the configured attributes
- Public and private attributes are checked
88 89 90 91 92 93 94 |
# File 'lib/sinclair/equals_checker.rb', line 88 def match?(model, other) return false unless model.class == other.class attributes.all? do |attr| model.send(attr) == other.send(attr) end end |