Sha256: 3f61445c649239b7305e17802f6238c985e54fb6df10b2c3661043ef86a37139

Contents?: true

Size: 891 Bytes

Versions: 161

Compression:

Stored size: 891 Bytes

Contents

require 'digest/md5'

module Linguist
  module MD5
    # Public: Create deep nested digest of value object.
    #
    # Useful for object comparison.
    #
    # 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

161 entries across 161 versions & 4 rubygems

Version Path
github-linguist-6.3.1 lib/linguist/md5.rb
github-linguist-6.3.0 lib/linguist/md5.rb
github-linguist-6.2.0 lib/linguist/md5.rb
github-linguist-6.1.0 lib/linguist/md5.rb
github-linguist-6.0.1 lib/linguist/md5.rb
github-linguist-6.0.0 lib/linguist/md5.rb
github-linguist-5.3.3 lib/linguist/md5.rb
github-linguist-5.3.2 lib/linguist/md5.rb
github-linguist-5.3.1 lib/linguist/md5.rb
github-linguist-5.3.0 lib/linguist/md5.rb
github-linguist-5.2.0 lib/linguist/md5.rb
github-linguist-5.1.0 lib/linguist/md5.rb
github-linguist-5.0.11 lib/linguist/md5.rb
github-linguist-5.0.10 lib/linguist/md5.rb
github-linguist-5.0.9 lib/linguist/md5.rb
github-linguist-5.0.8 lib/linguist/md5.rb
github-linguist-5.0.7 lib/linguist/md5.rb
github-linguist-5.0.6 lib/linguist/md5.rb
github-linguist-5.0.5 lib/linguist/md5.rb
github-linguist-5.0.4 lib/linguist/md5.rb