Sha256: 31ebbbe67b6787f7eccce0af4544586d2045513ce2544c652f47a74a0a2a374d

Contents?: true

Size: 1.9 KB

Versions: 14

Compression:

Stored size: 1.9 KB

Contents

module Datagrid
  module Drivers
    class MongoMapper < AbstractDriver #:nodoc:

      def self.match?(scope)
        return false unless defined?(::MongoMapper)
        if scope.is_a?(Class)
          scope.ancestors.include?(::MongoMapper::Document)
        else
          scope.is_a?(::Plucky::Query)
        end
      end

      def to_scope(scope)
        scope.where
      end

      def where(scope, attribute, value)
        scope.where(attribute => value)
      end

      def asc(scope, order)
        scope.sort(order.asc)
      end

      def desc(scope, order)
        scope.sort("#{order} desc")
      end

      def default_order(scope, column_name)
        has_column?(scope, column_name) ? column_name : nil
      end

      def greater_equal(scope, field, value)
        scope.where(field => {"$gte" => value})
      end

      def less_equal(scope, field, value)
        scope.where(field => {"$lte" => value})
      end

      def has_column?(scope, column_name)
        scope.key?(column_name)
      end

      def is_timestamp?(scope, column_name)
        #TODO implement the support
        false
      end

      def contains(scope, field, value)
        scope(field => Regexp.compile(Regexp.escape(value)))
      end

      def column_names(scope)
        [] # TODO: implement support
      end

      def batch_each(scope, batch_size, &block)
        current_page = 0
        loop do
          batch = scope.skip(current_page * batch_size).limit(batch_size).to_a
          return if batch.empty?
          scope.skip(current_page * batch_size).limit(batch_size).each do |item|
            yield(item)
          end
          current_page+=1
        end
      end

      def default_cache_key(asset)
        raise NotImplementedError
      end

      def default_preload(scope, value)
        raise NotImplementedError
      end

      def can_preload?(scope, association)
        false
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
datagrid-1.6.3 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.6.2 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.6.1 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.6.0 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.9 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.8 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.7 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.6 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.5 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.4 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.3 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.2 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.1 lib/datagrid/drivers/mongo_mapper.rb
datagrid-1.5.0 lib/datagrid/drivers/mongo_mapper.rb