Module: Datadog::AppSec::Utils::HashCoercion

Defined in:
lib/datadog/appsec/utils/hash_coercion.rb

Overview

A module for coercing arbitrary objects into hashes.

Class Method Summary collapse

Class Method Details

.coerce(object) ⇒ Hash?

A best effort to coerce an object to a hash with methods known to various frameworks with a fallback to standard library.

Parameters:

  • object (Object)

    The object to coerce.

Returns:

  • (Hash, nil)

    The coerced ‘Hash` or `nil` if the object is not coercible.



13
14
15
16
17
18
19
# File 'lib/datadog/appsec/utils/hash_coercion.rb', line 13

def self.coerce(object)
  return object.as_json if object.respond_to?(:as_json)
  return object.to_hash if object.respond_to?(:to_hash)
  return object.to_h if object.respond_to?(:to_h)

  nil
end