Class: SimpleMetrics::MetricRepository

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

Class Method Summary collapse

Class Method Details

.ensure_indexObject



32
33
34
35
36
# File 'lib/simple_metrics/metric_repository.rb', line 32

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/metric_repository.rb', line 15

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

.find_one(id) ⇒ Object



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

def find_one(id)
  metric(collection.find_one(id))
end

.find_one_by_name(name) ⇒ Object



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

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

.save(metric) ⇒ Object



20
21
22
# File 'lib/simple_metrics/metric_repository.rb', line 20

def save(metric)
  collection.insert(metric.attributes.merge(:created_at => Time.now.utc, :updated_at => Time.now.utc))
end

.truncate_collectionsObject



28
29
30
# File 'lib/simple_metrics/metric_repository.rb', line 28

def truncate_collections
  collection.remove
end

.update(metric) ⇒ Object



24
25
26
# File 'lib/simple_metrics/metric_repository.rb', line 24

def update(metric)
  collection.update({ "_id" => metric.id }, { "$set" => { :total => metric.total, :updated_at => Time.now.utc }})  
end