Class: MongoModel::InstrumentedCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/support/instrumented_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ InstrumentedCollection

Returns a new instance of InstrumentedCollection



45
46
47
# File 'lib/mongomodel/support/instrumented_collection.rb', line 45

def initialize(collection)
  @collection = collection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



120
121
122
# File 'lib/mongomodel/support/instrumented_collection.rb', line 120

def method_missing(method, *args, &block)
  collection.send(method, *args, &block)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection



43
44
45
# File 'lib/mongomodel/support/instrumented_collection.rb', line 43

def collection
  @collection
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/mongomodel/support/instrumented_collection.rb', line 49

def ==(other)
  case other
  when self.class
    collection == other.collection
  else
    collection == other
  end
end

#create_index(spec, options = {}) ⇒ Object



95
96
97
98
99
# File 'lib/mongomodel/support/instrumented_collection.rb', line 95

def create_index(spec, options={})
  instrument("create_index(#{spec.inspect})") do
    collection.create_index(spec, options)
  end
end

#distinct(key, query = nil) ⇒ Object



107
108
109
110
111
# File 'lib/mongomodel/support/instrumented_collection.rb', line 107

def distinct(key, query=nil)
  instrument("distinct(#{key.inspect}#{query.present? ? ', ' + query.inspect : ''})") do
    collection.distinct(key, query)
  end
end

#find(selector = {}, options = {}) ⇒ Object



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

def find(selector={}, options={})
  cursor = InstrumentedCursor.new(collection.find(selector, options))

  if block_given?
    yield cursor
    cursor.close
    nil
  else
    cursor
  end
end

#group(options, condition = {}, initial = {}, reduce = nil, finalize = nil) ⇒ Object



101
102
103
104
105
# File 'lib/mongomodel/support/instrumented_collection.rb', line 101

def group(options, condition={}, initial={}, reduce=nil, finalize=nil)
  instrument("group(#{options.inspect})") do
    collection.group(options, condition, initial, reduce, finalize)
  end
end

#map_reduce(map, reduce, options = {}) ⇒ Object



113
114
115
116
117
# File 'lib/mongomodel/support/instrumented_collection.rb', line 113

def map_reduce(map, reduce, options={})
  instrument("map_reduce(#{options.inspect})") do
    collection.map_reduce(map, reduce, options)
  end
end

#remove(selector = {}, options = {}) ⇒ Object



83
84
85
86
87
# File 'lib/mongomodel/support/instrumented_collection.rb', line 83

def remove(selector={}, options={})
  instrument("remove(#{selector.inspect})") do
    collection.remove(selector, options)
  end
end

#save(doc, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mongomodel/support/instrumented_collection.rb', line 70

def save(doc, options={})
  if doc.has_key?(:_id) || doc.has_key?('_id')
    selector = { '_id' => doc[:_id] || doc['_id'] }
    instrument("update(#{selector.inspect}, #{doc.inspect})") do
      collection.save(doc, options)
    end
  else
    instrument("insert(#{doc})") do
      collection.insert(doc, options)
    end
  end
end

#update(selector, document, options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/mongomodel/support/instrumented_collection.rb', line 89

def update(selector, document, options={})
  instrument("update(#{selector.inspect}, #{document.inspect})") do
    collection.update(selector, document, options)
  end
end