Sha256: c813e317bf973b2192299ac5f52c8e4171b1245e61cd7a31422aafcd6200acda

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module Misc
  MAX_ARRAY_DIGEST_LENGTH = 100_000
  def self.digest_str(obj)
    if obj.respond_to?(:digest_str)
      obj.digest_str
    else
      case obj
      when String
        #'\'' << obj << '\''
        if Path === obj || ! Open.exists?(obj)
          '\'' << obj << '\''
        else
          "File MD5: #{Misc.file_md5(obj)}"
        end
      when Integer, Symbol
        obj.to_s
      when Array
        if obj.length > MAX_ARRAY_DIGEST_LENGTH
          length = obj.length
          mid = length/2
          sample_pos = [1, 2, mid, length-2, length-1]
          "[#{length}:" << obj.values_at(*sample_pos).inject(""){|acc,o| acc.empty? ? Misc.digest_str(o) : acc << ', ' << Misc.digest_str(o) } << ']'
        else
          '[' << obj.inject(""){|acc,o| acc.empty? ? Misc.digest_str(o) : acc << ', ' << Misc.digest_str(o) } << ']'
        end
      when Hash
        '{' << obj.inject(""){|acc,p| s = Misc.digest_str(p.first) << "=" << Misc.digest_str(p.last); acc.empty? ? s : acc << ', ' << s } << '}'
      when Integer
        obj.to_s
      when Float
        if obj % 1 == 0
          obj.to_i.to_s
        elsif obj.abs > 10
          "%.1f" % obj
        elsif obj.abs > 1
          "%.3f" % obj
        else
          "%.6f" % obj
        end
      when TrueClass
        "true"
      when FalseClass
        "false"
      else
        obj.inspect
      end
    end
  end

  def self.digest(obj)
    str = String === obj ? obj : Misc.digest_str(obj)
    Digest::MD5.hexdigest(str)
  end

  def self.file_md5(file)
    file = file.find if Path === file
    file = File.expand_path(file)
    #md5file = file + '.md5'
    Persist.persist("MD5:#{file}", :string) do
      Digest::MD5.file(file).hexdigest
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
scout-essentials-1.1.0 lib/scout/misc/digest.rb
scout-essentials-1.0.0 lib/scout/misc/digest.rb
scout-gear-9.1.0 lib/scout/misc/digest.rb