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.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

Instance Method Details

#collection_map(db_name, mapping) ⇒ Object



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

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



28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



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

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

#enable(&block) ⇒ Object



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

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

#mapped_full_name(database, collection) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/moped_mapping.rb', line 48

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



41
42
43
44
45
46
# File 'lib/moped_mapping.rb', line 41

def mapped_name(database, collection)
  return collection unless MopedMapping.enabled
  mapping = db_collection_map[database]
  return collection unless mapping
  return mapping[collection] || collection
end