module MemModel::Guid::ClassMethods

Public Instance Methods

find_all_matching(substring) click to toggle source
# 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_id() click to toggle source
# File lib/mem_model/guid.rb, line 6
def generate_id
  [guid_prefix, '-', new_uuid].join.upcase
end
guid_prefix() click to toggle source
# File lib/mem_model/guid.rb, line 10
def guid_prefix
  name[0...3]
end
new_uuid() click to toggle source
# 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) click to toggle source
# 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