class UuidModel

Public Class Methods

add_lsn_mapping(ind, class_name) click to toggle source
# 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() click to toggle source
# File lib/mm_uses_uuid.rb, line 182
def class_lsn_lookup
  @@class_lsn_lookup
end
class_name_from_id(id, options = {}) click to toggle source
# File lib/mm_uses_uuid.rb, line 186
def class_name_from_id(id, options = {})
  lsn = id.to_s[-2..-1].hex
  class_name = @@lsn_class_lookup[lsn]
  if class_name.nil? and options[: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) click to toggle source
# File lib/mm_uses_uuid.rb, line 199
def find(*args)
  
  # raise "foo"
  
  options = args.last.is_a?(Hash) ? args.pop : {}
  fields  = *options[: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, options)
    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
Also aliased as: find_with_fields
find!(*args) click to toggle source
# File lib/mm_uses_uuid.rb, line 272
def find!(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options.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, options).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
Also aliased as: find_with_fields!
find_by_id(id) click to toggle source
# File lib/mm_uses_uuid.rb, line 195
def find_by_id(id)
  find id
end
find_with_fields(*args)
Alias for: find
find_with_fields!(*args)
Alias for: find!
lsn_class_lookup() click to toggle source
# File lib/mm_uses_uuid.rb, line 178
def lsn_class_lookup
  @@lsn_class_lookup
end