Sha256: 0b4725764f3216f04e31247649f78ffc527eb0f0a75117d71974aaec495322d9

Contents?: true

Size: 891 Bytes

Versions: 16

Compression:

Stored size: 891 Bytes

Contents

require 'digest/md5'

module Linguist
  module MD5
    # Public: Create deep nested digest of value object.
    #
    # Useful for object comparsion.
    #
    # obj - Object to digest.
    #
    # Returns String hex digest
    def self.hexdigest(obj)
      digest = Digest::MD5.new

      case obj
      when String, Symbol, Integer
        digest.update "#{obj.class}"
        digest.update "#{obj}"
      when TrueClass, FalseClass, NilClass
        digest.update "#{obj.class}"
      when Array
        digest.update "#{obj.class}"
        for e in obj
          digest.update(hexdigest(e))
        end
      when Hash
        digest.update "#{obj.class}"
        for e in obj.map { |(k, v)| hexdigest([k, v]) }.sort
          digest.update(e)
        end
      else
        raise TypeError, "can't convert #{obj.inspect} into String"
      end

      digest.hexdigest
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
ol-github-linguist-2.4.2.5 lib/linguist/md5.rb
geothird-linguist-2.6.1 lib/linguist/md5.rb
github-linguist-2.6.2 lib/linguist/md5.rb
github-linguist-2.6.1 lib/linguist/md5.rb
github-linguist-2.5.1 lib/linguist/md5.rb
github-linguist-2.5.0 lib/linguist/md5.rb
github-linguist-2.4.0 lib/linguist/md5.rb
github-linguist-2.3.4 lib/linguist/md5.rb
github-linguist-2.3.3 lib/linguist/md5.rb
github-linguist-2.3.1 lib/linguist/md5.rb
github-linguist-2.3.0 lib/linguist/md5.rb
github-linguist-2.2.1 lib/linguist/md5.rb
github-linguist-2.2.0 lib/linguist/md5.rb
github-linguist-2.1.2 lib/linguist/md5.rb
github-linguist-2.1.1 lib/linguist/md5.rb
github-linguist-2.1.0 lib/linguist/md5.rb