Module: SublimeDSL::Tools::ValueEquality

Overview

A mix-in allowing to compare objects based on value rather than identity.

It redefines #eql?, #hash and #== based on a #value_id method, and provides a default implementation for #value_id.

Instance Method Summary collapse

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns true if the class of other is the same or a subclass of self.class, and #value_id are the same using “==”.

Returns:

  • (Boolean)


16
17
18
# File 'lib/sublime_dsl/tools/value_equality.rb', line 16

def eql?(other)
  other.is_a?(self.class) && other.value_id == self.value_id
end

#hashObject

Returns #hash for #value_id.



24
25
26
# File 'lib/sublime_dsl/tools/value_equality.rb', line 24

def hash
  value_id.hash
end

#value_idObject

Default value identity: array of instance variable values.



30
31
32
# File 'lib/sublime_dsl/tools/value_equality.rb', line 30

def value_id
  instance_variables.map { |v| instance_variable_get(v) }
end