Class: RansackMongo::MongoAdapter
- Inherits:
-
Object
- Object
- RansackMongo::MongoAdapter
- 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
- #cont_matcher(attr, value) ⇒ Object
- #eq_matcher(attr, value) ⇒ Object
- #gt_matcher(attr, value) ⇒ Object
- #gteq_matcher(attr, value) ⇒ Object
- #in_matcher(attr, value) ⇒ Object
-
#initialize ⇒ MongoAdapter
constructor
A new instance of MongoAdapter.
- #lt_matcher(attr, value) ⇒ Object
- #lteq_matcher(attr, value) ⇒ Object
- #not_eq_matcher(attr, value) ⇒ Object
-
#or_op ⇒ Object
or operation.
- #sanitize(unsanitized) ⇒ Object
- #to_query ⇒ Object
Constructor Details
#initialize ⇒ MongoAdapter
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
.predicates ⇒ Object
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_op ⇒ Object
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_query ⇒ Object
9 10 11 |
# File 'lib/ransack_mongo/mongo_adapter.rb', line 9 def to_query @query end |