Class: Object

Inherits:
BasicObject
Defined in:
lib/monkey_patches.rb,
lib/monkey_patches.rb

Overview

check if is a checksum

Constant Summary collapse

REGEX =
/\A[0-9a-f]{32,128}\z/i
CHARS =
{
  md2: 32,
  md4: 32,
  md5: 32,
  sha1: 40,
  sha224: 56,
  sha256: 64,
  sha384: 96,
  sha512: 128
}

Instance Method Summary collapse

Instance Method Details

#be_a_checksumObject

return if this a checksum

Examples:

checksum.be_a_checksum


80
81
82
# File 'lib/monkey_patches.rb', line 80

def be_a_checksum
  !!(self =~ REGEX)
end

#deep_string_keysObject

transform hash keys to strings



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/monkey_patches.rb', line 43

def deep_string_keys
  if( is_a?( Hash ) )
    return inject({}) do |memo, (k, v)|
      memo.tap { |m| m[k.to_s] = v.deep_string_keys }
    end
  elsif( is_a?( Array ) )
    return map(&:deep_string_keys)
  end

  self
end

#deep_symbolize_keysObject

transform hash keys to symbols

Examples:

multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } }.deep_string_keys

Returns:

  • { foo: ‘bar’, level1: { ‘level2’ => ‘baz’ } }



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/monkey_patches.rb', line 28

def deep_symbolize_keys
  if( is_a?( Hash ) )
    return inject({}) do |memo, (k, v)|
      memo.tap { |m| m[k.to_sym] = v.deep_string_keys }
    end
  elsif( is_a?( Array ) )
    return map(&:deep_string_keys)
  end

  self
end

#produced_by(name) ⇒ Object

return true if the checksum created by spezified type

Examples:

checksum.produced_by(:md5)

checksum.produced_by(:sha256)

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
# File 'lib/monkey_patches.rb', line 92

def produced_by( name )
  function = name.to_s.downcase.to_sym

  raise ArgumentError, "unknown algorithm given to be_a_checksum.produced_by: #{function}" unless CHARS.include?(function)

  return true if( size == CHARS[function] )
  false
end