Module: MopedMapping

Extended by:
MopedMapping
Included in:
MopedMapping
Defined in:
lib/moped_mapping.rb,
lib/moped_mapping/version.rb,
lib/moped_mapping/node_ext.rb,
lib/moped_mapping/indexes_ext.rb,
lib/moped_mapping/session_context_ext.rb

Defined Under Namespace

Modules: IndexesExt, NodeExt, SessionContextExt

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/moped_mapping.rb', line 9

def enabled
  @enabled
end

Instance Method Details

#collection_map(db_name, mapping) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/moped_mapping.rb', line 73

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_mapObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moped_mapping.rb', line 29

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) ⇒ Object



12
# File 'lib/moped_mapping.rb', line 12

def disable(&block); set_enabled(false, &block); end

#enable(&block) ⇒ Object



11
# File 'lib/moped_mapping.rb', line 11

def enable(&block) ; set_enabled(true , &block); end

#mapped_full_name(database, collection) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/moped_mapping.rb', line 63

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) ⇒ Object



56
57
58
59
60
61
# File 'lib/moped_mapping.rb', line 56

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) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/moped_mapping.rb', line 43

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