Module: MemModel::Guid::ClassMethods

Defined in:
lib/mem_model/guid.rb

Instance Method Summary collapse

Instance Method Details

#find_all_matching(substring) ⇒ Object



14
15
16
# File 'lib/mem_model/guid.rb', line 14

def find_all_matching(substring)
  store.select{ |r| r.id.include?(substring.to_s.upcase) }
end

#generate_idObject



6
7
8
# File 'lib/mem_model/guid.rb', line 6

def generate_id
  [guid_prefix, '-', new_uuid].join.upcase
end

#guid_prefixObject



10
11
12
# File 'lib/mem_model/guid.rb', line 10

def guid_prefix
  name[0...3]
end

#new_uuidObject



39
40
41
42
43
44
# File 'lib/mem_model/guid.rb', line 39

def new_uuid
  ary = self.random_bytes(16).unpack("NnnnnN")
  ary[2] = (ary[2] & 0x0fff) | 0x4000
  ary[3] = (ary[3] & 0x3fff) | 0x8000
  "%08x-%04x-%04x-%04x-%04x%08x" % ary
end

#random_bytes(n = 16) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mem_model/guid.rb', line 18

def random_bytes(n=16)
  flags = File::RDONLY
  flags |= File::NONBLOCK if defined? File::NONBLOCK
  flags |= File::NOCTTY if defined? File::NOCTTY
  flags |= File::NOFOLLOW if defined? File::NOFOLLOW
  begin
    File.open("/dev/urandom", flags) {|f|
      unless f.stat.chardev?
        raise Errno::ENOENT
      end
      ret = f.readpartial(n)
      if ret.length != n
        raise NotImplementedError, "Unexpected partial read from random device"
      end
      return ret
    }
  rescue Errno::ENOENT
    raise NotImplementedError, "No random device"
  end
end