Module: Adapter::Mongo

Defined in:
lib/adapter/mongo.rb,
lib/adapter/mongo/version.rb

Constant Summary collapse

VERSION =
"0.8.1"

Instance Method Summary collapse

Instance Method Details

#clean(doc) ⇒ Object

Private



49
50
51
52
# File 'lib/adapter/mongo.rb', line 49

def clean(doc)
  doc.delete('_id')
  doc
end

#clear(options = nil) ⇒ Object

Public



43
44
45
46
# File 'lib/adapter/mongo.rb', line 43

def clear(options = nil)
  options = operation_options(options)
  client.remove({}, options)
end

#delete(key, options = nil) ⇒ Object

Public



37
38
39
40
# File 'lib/adapter/mongo.rb', line 37

def delete(key, options = nil)
  options = operation_options(options)
  client.remove({:_id => key}, options)
end

#operation_options(options) ⇒ Object

Private



55
56
57
# File 'lib/adapter/mongo.rb', line 55

def operation_options(options)
  write_concern.merge(options || {})
end

#read(key, options = nil) ⇒ Object

Public



8
9
10
11
12
# File 'lib/adapter/mongo.rb', line 8

def read(key, options = nil)
  if doc = client.find_one('_id' => key)
    clean(doc)
  end
end

#read_multiple(keys, options = nil) ⇒ Object

Public



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/adapter/mongo.rb', line 15

def read_multiple(keys, options = nil)
  ids = keys.map { |key| key }
  docs = client.find('_id' => {'$in' => ids}).to_a
  keys_and_values = docs.map { |doc| [doc.delete('_id'), doc] }

  docs_by_id = Hash[keys_and_values]

  result = {}
  keys.each do |key|
    key = key
    result[key] = docs_by_id[key]
  end
  result
end

#write(key, attributes, options = nil) ⇒ Object

Public



31
32
33
34
# File 'lib/adapter/mongo.rb', line 31

def write(key, attributes, options = nil)
  options = operation_options(options)
  client.save(attributes.merge('_id' => key), options)
end

#write_concernObject

Private



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/adapter/mongo.rb', line 60

def write_concern
  if options[:write_concern]
    options[:write_concern]
  else
    if options[:safe]
      {:w => 1}
    else
      {:w => 0}
    end
  end
end