Module: MmUsesUuid::ClassMethods

Defined in:
lib/mm_uses_uuid.rb

Instance Method Summary collapse

Instance Method Details

#convert_ids_to_BSON(ids) ⇒ Object



89
90
91
# File 'lib/mm_uses_uuid.rb', line 89

def convert_ids_to_BSON(ids)
  ids.map {|id| BsonUuid.to_mongo(id)}
end

#deserialize_id(id) ⇒ Object



53
54
55
# File 'lib/mm_uses_uuid.rb', line 53

def deserialize_id(id)
  BSON::Binary.new(id, BSON::Binary::SUBTYPE_UUID)
end

#find(*ids) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/mm_uses_uuid.rb', line 73

def find(*ids)
  batch_mode = ids.first.is_a?(Array) || ids.length > 1
  ids.flatten!
  ids = convert_ids_to_BSON(ids)
  results = super(ids)
  batch_mode ? results : results.first
end

#find!(*ids) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/mm_uses_uuid.rb', line 81

def find!(*ids)
  batch_mode = ids.first.is_a?(Array) || ids.length > 1
  ids.flatten!
  ids = convert_ids_to_BSON(ids)
  results = super(ids)
  batch_mode ? results : results.first
end

#make_uuidObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mm_uses_uuid.rb', line 57

def make_uuid
  uuid = SecureRandom.uuid.gsub!('-', '')
  if single_collection_inherited? and not embeddable?
    lookup_class_name = collection_name.singularize.camelize
  else
    lookup_class_name = name
  end
  replacement_lsn = UuidModel.class_lsn_lookup[lookup_class_name] || 0x00
  uuid[-2..-1] = replacement_lsn.to_s(16).rjust(2,'0')
  BSON::Binary.new(uuid, BSON::Binary::SUBTYPE_UUID)
  
rescue => e
  binding.pry
  raise e
end

#serialize_id(object) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/mm_uses_uuid.rb', line 44

def serialize_id(object)
  case object
  when MongoMapper::Document, MongoMapper::EmbeddedDocument
    object.id.to_s
  else
    object.to_s
  end       
end

#uuid_lsn(lsn_integer) ⇒ Object

end



108
109
110
111
# File 'lib/mm_uses_uuid.rb', line 108

def uuid_lsn(lsn_integer)
  raise "lsn_integer must be from 0-255" unless (0..255).cover?(lsn_integer)
  UuidModel.add_lsn_mapping(lsn_integer, self.name)
end