Class: Sinclair::EqualsChecker
- Defined in:
- lib/sinclair/equals_checker.rb,
lib/sinclair/equals_checker/reader.rb
Overview
Class responsible for checking if two instances of a class are the equals
Defined Under Namespace
Classes: Reader
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.
56 57 58 |
# File 'lib/sinclair/equals_checker.rb', line 56 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
77 78 79 |
# File 'lib/sinclair/equals_checker.rb', line 77 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
92 93 94 95 96 97 98 |
# File 'lib/sinclair/equals_checker.rb', line 92 def match?(model, other) return false unless model.class == other.class attributes.all? do |attr| Reader.attributes_match?(attr, model, other) end end |