Class: RansackMongo::MongoAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ransack_mongo/mongo_adapter.rb

Constant Summary collapse

PREDICATES =
%w[eq not_eq cont in gt lt gteq lteq]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMongoAdapter

Returns a new instance of MongoAdapter.



5
6
7
# File 'lib/ransack_mongo/mongo_adapter.rb', line 5

def initialize
  @query = {}
end

Class Method Details

.predicatesObject



68
69
70
# File 'lib/ransack_mongo/mongo_adapter.rb', line 68

def self.predicates
  PREDICATES
end

Instance Method Details

#cont_matcher(attr, value) ⇒ Object



21
22
23
# File 'lib/ransack_mongo/mongo_adapter.rb', line 21

def cont_matcher(attr, value)
  @query[attr] = /#{value}/i
end

#eq_matcher(attr, value) ⇒ Object



13
14
15
# File 'lib/ransack_mongo/mongo_adapter.rb', line 13

def eq_matcher(attr, value)
  @query[attr] = value
end

#gt_matcher(attr, value) ⇒ Object



29
30
31
32
# File 'lib/ransack_mongo/mongo_adapter.rb', line 29

def gt_matcher(attr, value)
  @query[attr] ||= {}
  @query[attr]['$gt'] = value.to_f
end

#gteq_matcher(attr, value) ⇒ Object



39
40
41
42
# File 'lib/ransack_mongo/mongo_adapter.rb', line 39

def gteq_matcher(attr, value)
  @query[attr] ||= {}
  @query[attr]['$gte'] = value.to_f
end

#in_matcher(attr, value) ⇒ Object



25
26
27
# File 'lib/ransack_mongo/mongo_adapter.rb', line 25

def in_matcher(attr, value)
  @query[attr] = { '$in' => value }
end

#lt_matcher(attr, value) ⇒ Object



34
35
36
37
# File 'lib/ransack_mongo/mongo_adapter.rb', line 34

def lt_matcher(attr, value)
  @query[attr] ||= {}
  @query[attr]['$lt'] = value.to_f
end

#lteq_matcher(attr, value) ⇒ Object



44
45
46
47
# File 'lib/ransack_mongo/mongo_adapter.rb', line 44

def lteq_matcher(attr, value)
  @query[attr] ||= {}
  @query[attr]['$lte'] = value.to_f
end

#not_eq_matcher(attr, value) ⇒ Object



17
18
19
# File 'lib/ransack_mongo/mongo_adapter.rb', line 17

def not_eq_matcher(attr, value)
  @query[attr] = { '$ne' => value }
end

#or_opObject

or operation



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ransack_mongo/mongo_adapter.rb', line 49

def or_op # or operation
  return unless block_given?

  original_query = @query
  @query = {}

  yield

  original_query['$or'] ||= []
  original_query['$or'].concat @query.map { |attr, value| { attr => value } }

  @query = original_query
end

#sanitize(unsanitized) ⇒ Object



63
64
65
66
# File 'lib/ransack_mongo/mongo_adapter.rb', line 63

def sanitize(unsanitized)
  # http://docs.mongodb.org/manual/faq/developers/#how-does-mongodb-address-sql-or-query-injection
  unsanitized
end

#to_queryObject



9
10
11
# File 'lib/ransack_mongo/mongo_adapter.rb', line 9

def to_query
  @query
end