Module: Equatable

Defined in:
lib/equatable.rb,
lib/equatable/version.rb

Overview

Make it easy to define equality and hash methods.

Defined Under Namespace

Modules: Methods

Constant Summary collapse

VERSION =
'0.6.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#comparison_attrsArray<Symbol> (readonly)

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.

Holds all attributes used for comparison.

Returns:

API:

  • private



29
30
31
# File 'lib/equatable.rb', line 29

def comparison_attrs
  @comparison_attrs
end

Class Method Details

.included(base) ⇒ self

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.

Hook into module inclusion.

Parameters:

  • the module or class including Equatable

Returns:

API:

  • private



15
16
17
18
19
20
21
22
# File 'lib/equatable.rb', line 15

def self.included(base)
  super
  base.extend(self)
  base.class_eval do
    include Methods
    define_methods
  end
end

Instance Method Details

#attr_reader(*args) ⇒ undefined

Objects that include this module are assumed to be value objects. It is also assumed that the only values that affect the results of equality comparison are the values of the object’s attributes.

Parameters:

Returns:

API:

  • public



40
41
42
43
# File 'lib/equatable.rb', line 40

def attr_reader(*args)
  super
  comparison_attrs.concat(args)
end

#inherited(subclass) ⇒ 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.

Copy the comparison_attrs into the subclass.

Parameters:

API:

  • private



50
51
52
53
# File 'lib/equatable.rb', line 50

def inherited(subclass)
  super
  subclass.instance_variable_set(:@comparison_attrs, comparison_attrs.dup)
end