Sha256: 20f8ad1818d27aff86df9812bade0454b8782fbdfa009e6865c6f8bdc1bc1a5d

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module SimpleMetrics
  class DashboardRepository

    class << self

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

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

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

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

      def update(dashboard)
        collection.update({ "_id" => dashboard.id }, { "$set" => dashboard.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('dashboards')
      end

      def dashboard(result)
        Dashboard.new(:id => result["_id"], :name => result["name"], :instruments => result["instruments"], :created_at => result["created_at"], :updated_at => result["updated_at"])
      end

      def dashboards(results)
        results.inject([]) { |result, a| result << dashboard(a); }
      end

    end # class << self

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple_metrics-0.4.6 lib/simple_metrics/dashboard_repository.rb
simple_metrics-0.4.5 lib/simple_metrics/dashboard_repository.rb
simple_metrics-0.4.4 lib/simple_metrics/dashboard_repository.rb
simple_metrics-0.4.3 lib/simple_metrics/dashboard_repository.rb
simple_metrics-0.4.2 lib/simple_metrics/dashboard_repository.rb