module MopedMapping

Constants

VERSION

Attributes

enabled[R]

Public Instance Methods

collection_map(db_name, mapping) { || ... } click to toggle source
# File lib/moped_mapping.rb, line 72
def collection_map(db_name, mapping)
  if block_given?
    bak, db_collection_map[db_name] = db_collection_map[db_name], mapping
    begin
      yield
    ensure
      db_collection_map[db_name] = bak
    end
  else
    db_collection_map[db_name] = mapping
  end
end
db_collection_map() click to toggle source
# File lib/moped_mapping.rb, line 28
def db_collection_map
  t = Thread.current
  result = t[:MopedMapping_db_collection_map]
  unless result
    result = nil
    result = Thread.main[:MopedMapping_db_collection_map] unless t == Thread.main
    result = result.dup if result
    result ||= {}
    t[:MopedMapping_db_collection_map] = result
  end
  result
end
disable(&block) click to toggle source
# File lib/moped_mapping.rb, line 11
def disable(&block); set_enabled(false, &block); end
enable(&block) click to toggle source
# File lib/moped_mapping.rb, line 10
def enable(&block) ; set_enabled(true , &block); end
mapped_full_name(database, collection) click to toggle source
# File lib/moped_mapping.rb, line 62
def mapped_full_name(database, collection)
  return collection unless MopedMapping.enabled
  db, col = collection.split(/\./, 2)
  return collection unless col
  mapping = db_collection_map[db]
  return collection unless mapping
  mapped = mapping[col]
  mapped ? "#{db}.#{mapped}" : collection
end
mapped_name(database, collection) click to toggle source
# File lib/moped_mapping.rb, line 55
def mapped_name(database, collection)
  return collection unless MopedMapping.enabled
  mapping = db_collection_map[database]
  return collection unless mapping
  return mapping[collection] || collection
end
update_main_collection_map(db_name, mapping) click to toggle source

mainスレッド以外からmainスレッドのデータを書き換えるときに使用します

# File lib/moped_mapping.rb, line 42
def update_main_collection_map(db_name, mapping)
  Mutex.new.synchronize do
    mt = Thread.main
    mt[:MopedMapping_db_collection_map] ||= {}
    mt[:MopedMapping_db_collection_map][db_name] = mapping
    ct = Thread.current
    unless ct == mt
      ct[:MopedMapping_db_collection_map] ||= {}
      ct[:MopedMapping_db_collection_map][db_name] = mapping
    end
  end
end

Private Instance Methods

set_enabled(value) { || ... } click to toggle source
# File lib/moped_mapping.rb, line 13
def set_enabled(value)
  if block_given?
    bak, @enabled = @enabled, value
    begin
      yield
    ensure
      @enabled = bak
    end
  else
    @enabled = value
  end
end