Sha256: eec71f5341a1b0a73c686ad3acdeed51dc6b00e1757ec230f53b8143ba7ed199
Contents?: true
Size: 1.77 KB
Versions: 1
Compression:
Stored size: 1.77 KB
Contents
module SimpleMetrics class InstrumentRepository class << self def find_one(id) instrument(collection.find_one(BSON::ObjectId.from_string(id))) end def find_one_by_name(name) result = collection.find({ :name => name }).to_a.first instrument(result) if result end def find_all results = collection.find.sort([['name', ::Mongo::ASCENDING]]).to_a instruments(results) if results end 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 def update(instrument) collection.update({ "_id" => instrument.id }, "$set" => instrument.attributes.merge(:updated_at => Time.now.utc).reject { |k, v| k == 'id' }) end def remove(id) collection.remove("_id" => BSON::ObjectId.from_string(id)) end def truncate_collections collection.remove end def ensure_index collection.ensure_index([['created_at', ::Mongo::ASCENDING]]) collection.ensure_index([['updated_at', ::Mongo::ASCENDING]]) collection.ensure_index([['name', ::Mongo::ASCENDING]]) end private def collection Repository.db.collection('instruments') end def instrument(result) Instrument.new(:id => result["_id"], :name => result["name"], :metrics => result["metrics"], :created_at => result["created_at"], :updated_at => result["updated_at"]) end def instruments(results) results.inject([]) { |result, a| result << instrument(a); } end end # class << self end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
simple_metrics-0.4.2 | lib/simple_metrics/instrument_repository.rb |