Module: Airbrake::HashKeyable

Included in:
PerformanceBreakdown, Query, Queue, Request
Defined in:
lib/airbrake-ruby/hash_keyable.rb

Overview

HashKeyable allows instances of the class to be used as a Hash key in a consistent manner.

The class that includes it must implement to_h, which defines properties that all of the instances must share in order to produce the same #hash.

Examples:

class C
  include Airbrake::HashKeyable

  def initialize(key)
    @key = key
  end

  def to_h
    { 'key' => @key }
  end
end

h = {}
h[C.new('key1')] = 1
h[C.new('key1')] #=> 1
h[C.new('key2')] #=> nil

Instance Method Summary collapse

Instance Method Details

#eql?(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


28
29
30
# File 'lib/airbrake-ruby/hash_keyable.rb', line 28

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

#hashInteger

Returns:

  • (Integer)


33
34
35
# File 'lib/airbrake-ruby/hash_keyable.rb', line 33

def hash
  to_h.hash
end