Module: HashFingerprint

Defined in:
lib/hash_fingerprint.rb,
lib/hash_fingerprint/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.compare(left_object, right_object) ⇒ Object

Public: Returns true/false if objects match

left_object - A hash, or array right_object - A hash, or array

Examples

ordered_hash = {a: {b: 'b', c: 'c'} d: 'd'}
unordered_hash = {:d => 'd', :a => {:c => 'c', :b => 'b'}}
HashFingerprint.compare(left_object, right_object)
=> true

Returns bool



45
46
47
# File 'lib/hash_fingerprint.rb', line 45

def self.compare(left_object, right_object)
  fingerprint(left_object) == fingerprint(right_object)
end

.fingerprint(object) ⇒ Object

Public: Returns SHA256 fingerprint of object

object - A hash, or array

Examples

hash = {a: {b: 'b', c: 'c'} d: 'd'}
HashFingerprint.fingerprint(object)
=> "95045071ee6ce0333d7cd6408962b64a7520d45e0be6161ecd666a30bf7f3706"

Returns string



29
30
31
# File 'lib/hash_fingerprint.rb', line 29

def self.fingerprint(object)
  Digest::SHA256.hexdigest get_object_string object
end

.get_object_string(object) ⇒ Object

Public: Returns string representation of object

object - A hash, or array

Examples

hash = {a: {b: 'b', c: 'c'} d: 'd'}
HashFingerprint.get_object_string(object)
=> "[\"[\\\"b=>b\\\", \\\"c=>c\\\"]Array=>[\\\"b=>b\\\", \\\"c=>c\\\"]Array\", \"d=>d\"]Array"

Returns string



15
16
17
# File 'lib/hash_fingerprint.rb', line 15

def self.get_object_string(object)
    parse(object).tap { |obj| return obj.class == String ? obj : to_string(obj) }
end