Class: Datagrid::Drivers::MongoMapper

Inherits:
AbstractDriver show all
Defined in:
lib/datagrid/drivers/mongo_mapper.rb

Overview

:nodoc:

Constant Summary

Constants inherited from AbstractDriver

AbstractDriver::TIMESTAMP_CLASSES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractDriver

#append_column_queries, guess_driver, inherited, #match?, #normalized_column_type, #reverse_order, #where_by_timestamp_gotcha

Class Method Details

.match?(scope) ⇒ Boolean

Returns:

  • (Boolean)

5
6
7
8
9
10
11
12
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 5

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

Instance Method Details

#asc(scope, order) ⇒ Object


22
23
24
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 22

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

#batch_each(scope, batch_size, &block) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 59

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

#can_preload?(scope, association) ⇒ Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 79

def can_preload?(scope, association)
  false
end

#column_names(scope) ⇒ Object


55
56
57
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 55

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

#contains(scope, field, value) ⇒ Object


51
52
53
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 51

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

#default_cache_key(asset) ⇒ Object

Raises:

  • (NotImplementedError)

71
72
73
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 71

def default_cache_key(asset)
  raise NotImplementedError
end

#default_order(scope, column_name) ⇒ Object


30
31
32
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 30

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

#default_preload(scope, value) ⇒ Object

Raises:

  • (NotImplementedError)

75
76
77
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 75

def default_preload(scope, value)
  raise NotImplementedError
end

#desc(scope, order) ⇒ Object


26
27
28
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 26

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

#greater_equal(scope, field, value) ⇒ Object


34
35
36
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 34

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

#has_column?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 42

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

#is_timestamp?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

46
47
48
49
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 46

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

#less_equal(scope, field, value) ⇒ Object


38
39
40
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 38

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

#to_scope(scope) ⇒ Object


14
15
16
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 14

def to_scope(scope)
  scope.where
end

#where(scope, attribute, value) ⇒ Object


18
19
20
# File 'lib/datagrid/drivers/mongo_mapper.rb', line 18

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