Class: Sinclair::EqualsChecker::Reader Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sinclair/equals_checker/reader.rb

Overview

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

Class capable of reading an attribute from models

Author:

  • darthjee

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ Reader

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 new instance of Reader.

Parameters:

  • attribute (Symbol)

    name of the attribute (method or variable) to be accessed in the models



29
30
31
# File 'lib/sinclair/equals_checker/reader.rb', line 29

def initialize(attribute)
  @attribute = attribute
end

Class Method Details

.attributes_match?(attribute, model, other) ⇒ TrueClass, FalseClass

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.

Checks if two attributes from 2 object match

Parameters:

  • attribute (Symbol)

    attribute name

  • model (Object)

    object to be compared with other

  • other (Object)

    object to be compared with model

Returns:

  • (TrueClass, FalseClass)

See Also:



21
22
23
24
25
# File 'lib/sinclair/equals_checker/reader.rb', line 21

def self.attributes_match?(attribute, model, other)
  reader = new(attribute)

  reader.read_from(model) == reader.read_from(other)
end

Instance Method Details

#read_from(model) ⇒ Object

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.

Reads the attribute from the model

When attribute is a method name, calls that method on the model

When attribute is an instance variable name, that is read directly from the model

Parameters:

  • model (Object)

    the model to be read

Returns:



42
43
44
45
46
# File 'lib/sinclair/equals_checker/reader.rb', line 42

def read_from(model)
  return model.send(attribute) unless attribute.to_s.match?(/^@.*/)

  model.instance_variable_get(attribute)
end