Class: UuidModel

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/mm_uses_uuid.rb

Class Method Summary collapse

Class Method Details

.add_lsn_mapping(ind, klass) ⇒ Object



41
42
43
44
# File 'lib/mm_uses_uuid.rb', line 41

def self.add_lsn_mapping(ind, klass)
  @@lsn_class_lookup[ind] = klass
  @@class_lsn_lookup = @@lsn_class_lookup.invert
end

.class_lsn_lookupObject



50
51
52
# File 'lib/mm_uses_uuid.rb', line 50

def self.class_lsn_lookup
  @@class_lsn_lookup
end

.find(*ids) ⇒ Object



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

def self.find(*ids)
  ids.flatten!.uniq!
  ids_by_class = ids.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |id, hsh|
    lsn = id.to_s[-1].hex
    klass = @@lsn_class_lookup[lsn]
    if klass.nil?
      raise "expected to find a class in @@lsn_class_lookup[#{lsn}] of the MongoMapper module but there was no entry. You need to set uuid_lsn in your class."
    end
    hsh[klass] << id
  end
  result = ids_by_class.map {|klass, ids| klass.find(ids)} .flatten
  ids.length == 1 ? result.first : result
end

.find!(*ids) ⇒ Object

Raises:

  • (MongoMapper::DocumentNotFound)


68
69
70
71
72
73
74
75
76
# File 'lib/mm_uses_uuid.rb', line 68

def self.find!(*ids)
  ids.flatten!.uniq!
  raise MongoMapper::DocumentNotFound, "Couldn't find without an ID" if ids.size == 0
  find(*ids).tap do |result|
    if result.nil? || ids.size != Array(result).size
      raise MongoMapper::DocumentNotFound, "Couldn't find all of the ids (#{ids.join(',')}). Found #{Array(result).size}, but was expecting #{ids.size}"
    end
  end
end

.lsn_class_lookupObject



46
47
48
# File 'lib/mm_uses_uuid.rb', line 46

def self.lsn_class_lookup
  @@lsn_class_lookup
end