Module: TTY::Equatable

Included in:
Table, Table::ColumnSet, Vector
Defined in:
lib/tty/support/equatable.rb

Overview

Make it easy to define equality and hash methods.

Defined Under Namespace

Modules: Methods

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>)


32
33
34
# File 'lib/tty/support/equatable.rb', line 32

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)


16
17
18
19
20
21
22
23
24
25
# File 'lib/tty/support/equatable.rb', line 16

def self.included(base)
  super
  base.extend(self)
  base.class_eval do
    define_comparison_attrs
    include Methods
    define_methods
  end
  self
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)


43
44
45
46
# File 'lib/tty/support/equatable.rb', line 43

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)


53
54
55
56
# File 'lib/tty/support/equatable.rb', line 53

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