Module: Dieses::Support::Hash

Included in:
Dieses::Support
Defined in:
lib/dieses/support/hash.rb

Instance Method Summary collapse

Instance Method Details

#hashify_by(array_of_objects, attribute) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/dieses/support/hash.rb', line 6

def hashify_by(array_of_objects, attribute)
  {}.tap do |hash|
    array_of_objects.each do |object|
      hash[object.send(attribute)] = object
    rescue NoMethodError
      raise ArgumentError, "Object not respond to #{attribute}: #{object}"
    end
  end
end