Class: SimpleMetrics::InstrumentRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_metrics/instrument_repository.rb

Class Method Summary collapse

Class Method Details

.ensure_indexObject



41
42
43
44
45
# File 'lib/simple_metrics/instrument_repository.rb', line 41

def ensure_index
  collection.ensure_index([['created_at', ::Mongo::ASCENDING]])
  collection.ensure_index([['updated_at', ::Mongo::ASCENDING]])
  collection.ensure_index([['name', ::Mongo::ASCENDING]])
end

.find_allObject



15
16
17
18
# File 'lib/simple_metrics/instrument_repository.rb', line 15

def find_all
  results = collection.find.sort([['name', ::Mongo::ASCENDING]]).to_a
  instruments(results) if results
end

.find_one(id) ⇒ Object



6
7
8
# File 'lib/simple_metrics/instrument_repository.rb', line 6

def find_one(id)
  instrument(collection.find_one(BSON::ObjectId.from_string(id)))
end

.find_one_by_name(name) ⇒ Object



10
11
12
13
# File 'lib/simple_metrics/instrument_repository.rb', line 10

def find_one_by_name(name)      
  result = collection.find({ :name => name }).to_a.first
  instrument(result) if result
end

.remove(id) ⇒ Object



33
34
35
# File 'lib/simple_metrics/instrument_repository.rb', line 33

def remove(id)
  collection.remove("_id" => BSON::ObjectId.from_string(id))
end

.save(instrument) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/simple_metrics/instrument_repository.rb', line 20

def save(instrument)
  instrument.created_at = Time.now.utc
  instrument.updated_at = Time.now.utc
  attributes = instrument.attributes.reject { |key, value| key.to_s == "id" }
  id = collection.insert(attributes)
  instrument.id = id
  id
end

.truncate_collectionsObject



37
38
39
# File 'lib/simple_metrics/instrument_repository.rb', line 37

def truncate_collections
  collection.remove
end

.update(instrument) ⇒ Object



29
30
31
# File 'lib/simple_metrics/instrument_repository.rb', line 29

def update(instrument)
  collection.update({ "_id" => instrument.id }, "$set" => instrument.attributes.merge(:updated_at => Time.now.utc).reject { |k, v| k == 'id' })
end