Class: Gitlab::Database::ShaAttribute

Inherits:
BINARY_TYPE
  • Object
show all
Defined in:
lib/gitlab/database/sha_attribute.rb

Overview

Class for casting binary data to hexadecimal SHA1 hashes (and vice-versa).

Using ShaAttribute allows you to store SHA1 values as binary while still using them as if they were stored as string values. This gives you the ease of use of string values, but without the storage overhead.

Constant Summary collapse

PACK_FORMAT =
'H*'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.serialize(value) ⇒ Object

Casts a SHA1 in hexadecimal to the proper binary format.



31
32
33
34
35
# File 'lib/gitlab/database/sha_attribute.rb', line 31

def self.serialize(value)
  arg = value ? [value].pack(PACK_FORMAT) : nil

  BINARY_TYPE.new.serialize(arg)
end

Instance Method Details

#deserialize(value) ⇒ Object

Casts binary data to a SHA1 in hexadecimal.



18
19
20
21
# File 'lib/gitlab/database/sha_attribute.rb', line 18

def deserialize(value)
  value = super(value)
  value ? value.unpack1(PACK_FORMAT) : nil
end

#serialize(value) ⇒ Object

Casts a SHA1 in hexadecimal to the proper binary format.



24
25
26
27
28
# File 'lib/gitlab/database/sha_attribute.rb', line 24

def serialize(value)
  arg = value ? [value].pack(PACK_FORMAT) : nil

  BINARY_TYPE.new.serialize(arg)
end