Class: Datagrid::Drivers::Mongoid
Overview
Constant Summary
AbstractDriver::TIMESTAMP_CLASSES
Class Method Summary
collapse
Instance Method Summary
collapse
-
#asc(scope, order) ⇒ Object
-
#batch_each(scope, batch_size, &block) ⇒ Object
-
#column_names(scope) ⇒ Object
-
#contains(scope, field, value) ⇒ Object
-
#default_cache_key(asset) ⇒ Object
-
#default_order(scope, column_name) ⇒ Object
-
#desc(scope, order) ⇒ Object
-
#greater_equal(scope, field, value) ⇒ Object
-
#has_column?(scope, column_name) ⇒ Boolean
-
#is_timestamp?(scope, column_name) ⇒ Boolean
-
#less_equal(scope, field, value) ⇒ Object
-
#normalized_column_type(scope, field) ⇒ Object
-
#to_scope(scope) ⇒ Object
-
#where(scope, attribute, value) ⇒ Object
#append_column_queries, guess_driver, inherited, #match?, #reverse_order, #where_by_timestamp_gotcha
Class Method Details
.match?(scope) ⇒ Boolean
5
6
7
8
9
10
11
12
|
# File 'lib/datagrid/drivers/mongoid.rb', line 5
def self.match?(scope)
return false unless defined?(::Mongoid)
if scope.is_a?(Class)
scope.ancestors.include?(::Mongoid::Document)
else
scope.is_a?(::Mongoid::Criteria)
end
end
|
Instance Method Details
#asc(scope, order) ⇒ Object
25
26
27
|
# File 'lib/datagrid/drivers/mongoid.rb', line 25
def asc(scope, order)
scope.asc(order)
end
|
#batch_each(scope, batch_size, &block) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/datagrid/drivers/mongoid.rb', line 82
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
|
#column_names(scope) ⇒ Object
58
59
60
|
# File 'lib/datagrid/drivers/mongoid.rb', line 58
def column_names(scope)
to_scope(scope).klass.fields.keys
end
|
#contains(scope, field, value) ⇒ Object
54
55
56
|
# File 'lib/datagrid/drivers/mongoid.rb', line 54
def contains(scope, field, value)
scope.where(field => Regexp.compile(Regexp.escape(value)))
end
|
#default_cache_key(asset) ⇒ Object
94
95
96
|
# File 'lib/datagrid/drivers/mongoid.rb', line 94
def default_cache_key(asset)
asset.id || raise(NotImplementedError)
end
|
#default_order(scope, column_name) ⇒ Object
33
34
35
|
# File 'lib/datagrid/drivers/mongoid.rb', line 33
def default_order(scope, column_name)
has_column?(scope, column_name) ? column_name : nil
end
|
#desc(scope, order) ⇒ Object
29
30
31
|
# File 'lib/datagrid/drivers/mongoid.rb', line 29
def desc(scope, order)
scope.desc(order)
end
|
#greater_equal(scope, field, value) ⇒ Object
37
38
39
|
# File 'lib/datagrid/drivers/mongoid.rb', line 37
def greater_equal(scope, field, value)
scope.where(field => {"$gte" => value})
end
|
#has_column?(scope, column_name) ⇒ Boolean
45
46
47
|
# File 'lib/datagrid/drivers/mongoid.rb', line 45
def has_column?(scope, column_name)
column_names(scope).include?(column_name.to_s)
end
|
#is_timestamp?(scope, column_name) ⇒ Boolean
49
50
51
52
|
# File 'lib/datagrid/drivers/mongoid.rb', line 49
def is_timestamp?(scope, column_name)
has_column?(scope, column_name) &&
timestamp_class?(to_scope(scope).klass.fields[column_name.to_s].type)
end
|
#less_equal(scope, field, value) ⇒ Object
41
42
43
|
# File 'lib/datagrid/drivers/mongoid.rb', line 41
def less_equal(scope, field, value)
scope.where(field => {"$lte" => value})
end
|
#normalized_column_type(scope, field) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/datagrid/drivers/mongoid.rb', line 62
def normalized_column_type(scope, field)
type = to_scope(scope).klass.fields[field.to_s].try(:type)
return nil unless type
{
[BigDecimal , String, Symbol, Range, Array, Hash, ] => :string,
[Boolean] => :boolean,
[Date] => :date,
TIMESTAMP_CLASSES => :timestamp,
[Float] => :fload,
[Integer] => :integer,
}.each do |keys, value|
return value if keys.include?(type)
end
return :string
end
|
#to_scope(scope) ⇒ Object
14
15
16
|
# File 'lib/datagrid/drivers/mongoid.rb', line 14
def to_scope(scope)
scope.where(nil)
end
|
#where(scope, attribute, value) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/datagrid/drivers/mongoid.rb', line 18
def where(scope, attribute, value)
if value.is_a?(Range)
value = {"$gte" => value.first, "$lte" => value.last}
end
scope.where(attribute => value)
end
|