Class: SimpleMetrics::MetricRepository

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

Class Method Summary (collapse)

Class Method Details

+ (Object) ensure_index



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

+ (Object) find_all



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

+ (Object) find_one(id)



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

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

+ (Object) find_one_by_name(name)



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

+ (Object) save(metric)



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

+ (Object) truncate_collections



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

def truncate_collections
  collection.remove
end

+ (Object) update(metric)



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