Class: UuidModel
- Inherits:
-
Object
- Object
- UuidModel
- Includes:
- MongoMapper::Document
- Defined in:
- lib/mm_uses_uuid.rb
Class Method Summary collapse
- .add_lsn_mapping(ind, class_name) ⇒ Object
- .class_lsn_lookup ⇒ Object
- .class_name_from_id(id, options = {}) ⇒ Object
- .find(*args) ⇒ Object (also: find_with_fields)
- .find!(*args) ⇒ Object (also: find_with_fields!)
- .find_by_id(id) ⇒ Object
- .lsn_class_lookup ⇒ Object
Class Method Details
.add_lsn_mapping(ind, class_name) ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/mm_uses_uuid.rb', line 169 def add_lsn_mapping(ind, class_name) class_name = class_name.to_s if current_class_name = @@lsn_class_lookup[ind] raise "cannont assign #{class_name} to #{ind} as #{current_class_name} is already assigned to that LSN" end @@lsn_class_lookup[ind] = class_name @@class_lsn_lookup = @@lsn_class_lookup.invert end |
.class_lsn_lookup ⇒ Object
182 183 184 |
# File 'lib/mm_uses_uuid.rb', line 182 def class_lsn_lookup @@class_lsn_lookup end |
.class_name_from_id(id, options = {}) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/mm_uses_uuid.rb', line 186 def class_name_from_id(id, = {}) lsn = id.to_s[-2..-1].hex class_name = @@lsn_class_lookup[lsn] if class_name.nil? and [:error_if_no_lsn_match] raise "expected to find a class name in @@lsn_class_lookup[#{lsn}] of the MongoMapper module but there was no entry. You need to set uuid_lsn in your class." end class_name end |
.find(*args) ⇒ Object Also known as: find_with_fields
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/mm_uses_uuid.rb', line 199 def find(*args) # raise "foo" = args.last.is_a?(Hash) ? args.pop : {} fields = *[:fields] fields = nil if fields.empty? batch_mode = args.first.is_a?(Array) || args.length > 1 ids = args.flatten.uniq ids.map! {|id| BsonUuid.to_mongo(id)} ids_by_model = ids.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |id, hsh| model_name = class_name_from_id(id, ) hsh[model_name.constantize] << id if model_name end if defined? Celluloid #NOTE: because IdentityMap is in the current thread only... #we have to manage it ourselves if using Celluloid im_results = [] unless fields ids_by_model.clone.each do |model, model_ids| model_ids.each do |model_id| doc = model.get_from_identity_map(model_id) if doc im_results << doc ids_by_model[model].delete model_id end end ids_by_model.delete(model) if ids_by_model[model].empty? end end future_db_results = ids_by_model.map do |model, model_ids| query = model.where(:id => model_ids) query = query.fields(fields) if fields Celluloid::Future.new { query.all } end db_results = future_db_results.map(&:value).flatten if fields db_results.each(&:remove_from_identity_map) else db_results.each(&:add_to_identity_map) end results = im_results + db_results else #NOTE: as this is in the current thread, IdentityMap management is normal results = ids_by_model.map do |model, model_ids| if fields model.where(:id => model_ids).fields(fields).all #models will be removed from the map else model.find model_ids #we use this so that we read and write to the identity map end end.flatten end batch_mode ? results : results.first # rescue => e # binding.pry end |
.find!(*args) ⇒ Object Also known as: find_with_fields!
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/mm_uses_uuid.rb', line 272 def find!(*args) = args.last.is_a?(Hash) ? args.pop : {} .merge(:error_if_no_lsn_match => true) ids = args.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 |
.find_by_id(id) ⇒ Object
195 196 197 |
# File 'lib/mm_uses_uuid.rb', line 195 def find_by_id(id) find id end |
.lsn_class_lookup ⇒ Object
178 179 180 |
# File 'lib/mm_uses_uuid.rb', line 178 def lsn_class_lookup @@lsn_class_lookup end |