Class: FFI::UUID

Inherits:
Object
  • Object
show all
Extended by:
Library
Defined in:
lib/ffi/ffi_uuid_impl.rb

Overview

uuid_generate_xxx takex uuid_t which is a 16 byte unsigned char that is directly updated - treat like a bang! method

Class Method Summary collapse

Class Method Details

.generate(algorithm = :random) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ffi/ffi_uuid_impl.rb', line 54

def self.generate(algorithm=:random)
  binary_uuid = " " * 16
  formatted_result = " " * 36  # 32 hex chars + 4 dashes
  case algorithm
  when :time
    FFI::UUID.uuid_generate_time(binary_uuid)
  else
    FFI::UUID.uuid_generate_random(binary_uuid)
  end
  ##puts "BINARY_UUID #{binary_uuid.bytes.to_a.inspect}"

  FFI::UUID.uuid_unparse(binary_uuid, formatted_result)
  formatted_result    # manual/slower/ruby way .unpack("H*")[0]
end

.generate_randomObject



69
70
71
# File 'lib/ffi/ffi_uuid_impl.rb', line 69

def self.generate_random
  generate(:random)
end

.generate_timeObject



73
74
75
# File 'lib/ffi/ffi_uuid_impl.rb', line 73

def self.generate_time
  generate(:time)
end

.get_uuids(num = 1, algorithm = :random) ⇒ Object

get a set of uuids - useful for server/backend apps that need a lot defaults to the :random algorightm algorithm=:time will switch to the less desirable/secure time based method



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ffi/ffi_uuid_impl.rb', line 87

def self.get_uuids(num=1, algorithm=:random)
  uuids = Array.new(num) # preallocate to avoid expansion
  num.times { |i|
    result = case algorithm
    when :time
      generate_time
    else
      generate_random
    end
    uuids[i-1] = result
  }
  uuids
end

.unparse(binary_uuid) ⇒ Object



77
78
79
80
81
82
# File 'lib/ffi/ffi_uuid_impl.rb', line 77

def self.unparse(binary_uuid)
  raise "UUID unparsed required non-nil input" if binary_uuid.nil? || binary_uuid.empty?
  formatted_uuid = " " * 36
  FFI::UUID.uuid_unparse(binary_uuid, formatted_uuid)
  formatted_uuid
end