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.7.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:

  • (Array<Symbol>)


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:

  • base (Module)

    the module or class including Equatable

Returns:

  • (self)


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:

  • *args (Array<Symbol>)

Returns:

  • (undefined)


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:

  • subclass (Class)


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

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