Module: GoldRecord::UUID
- Defined in:
- lib/gold_record/uuid.rb
Class Method Summary collapse
- .coerce(value) ⇒ Object
- .decode_hex(str) ⇒ Object
- .encode_hex(bin) ⇒ Object
- .random_generate ⇒ Object
- .urlsafe_decode64(str) ⇒ Object
-
.urlsafe_encode64(bin) ⇒ Object
Slightly modified backport from Ruby 1.9 www.ruby-doc.org/ruby-1.9/classes/Base64.html Strips padding char (=) and newlines from end of encoded string.
Class Method Details
.coerce(value) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gold_record/uuid.rb', line 14 def self.coerce(value) return nil unless value.kind_of?(String) if value.size == 22 value = urlsafe_decode64(value) elsif value.size == 32 || value.size == 36 value = decode_hex(value) end value = nil unless value.size == 16 value end |
.decode_hex(str) ⇒ Object
29 30 31 |
# File 'lib/gold_record/uuid.rb', line 29 def self.decode_hex(str) [str.delete("-")].pack("H*") end |
.encode_hex(bin) ⇒ Object
25 26 27 |
# File 'lib/gold_record/uuid.rb', line 25 def self.encode_hex(bin) bin.unpack("H*").first end |
.random_generate ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/gold_record/uuid.rb', line 3 def self.random_generate bin = SecureRandom.random_bytes(16) # Set version 4 bin[6] &= 0b01001111 bin[6] |= 0b01000000 # Set reserved bits bin[8] &= 0b10111111 bin[8] |= 0b10000000 bin end |
.urlsafe_decode64(str) ⇒ Object
41 42 43 |
# File 'lib/gold_record/uuid.rb', line 41 def self.urlsafe_decode64(str) "#{str.tr("-_", "+/")}===".unpack("m").first end |
.urlsafe_encode64(bin) ⇒ Object
Slightly modified backport from Ruby 1.9 www.ruby-doc.org/ruby-1.9/classes/Base64.html Strips padding char (=) and newlines from end of encoded string.
37 38 39 |
# File 'lib/gold_record/uuid.rb', line 37 def self.urlsafe_encode64(bin) [bin].pack("m").tr("+/=", "-_ ").strip.delete("\n") end |