Module: BSON::ObjectId::UUIDConvertable::ClassMethods

Defined in:
lib/bson/object_id/uuid_convertable.rb

Instance Method Summary collapse

Instance Method Details

#from_string(string) ⇒ Object



34
35
36
# File 'lib/bson/object_id/uuid_convertable.rb', line 34

def from_string(string)
  UUID_REGEX.match(string.to_s) ? from_uuid(string) : super
end

#from_uuid(string) ⇒ Object

rubocop:disable Metrics/AbcSize



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bson/object_id/uuid_convertable.rb', line 38

def from_uuid(string) # rubocop:disable Metrics/AbcSize
  parts = string.split('-')

  timestamp = parts[0].to_i(16) + (parts[1].to_i(16) << 32) + ((parts[2].to_i(16) & 0x0FFF) << 48)
  machine_pid = parts[4][2..-1]

  high_counter = parts[4][0..1]
  mid_counter = parts[3][2..3]
  low_counter = (timestamp % 10_000_000) & 0xFF

  seconds = (timestamp - GREGORIAN_EPOCH_OFFSET) / 10_000_000

  from_string(format('%06x', seconds) + machine_pid + high_counter + mid_counter + format('%02x', low_counter))
end

#legal?(string) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/bson/object_id/uuid_convertable.rb', line 30

def legal?(string)
  UUID_REGEX.match(string.to_s) ? true : super
end