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 start mstart 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



73
74
75
# File 'lib/ransack_mongo/mongo_adapter.rb', line 73

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



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

def gt_matcher(attr, value)
  append_sizeable_matcher('$gt', attr, value)
end

#gteq_matcher(attr, value) ⇒ Object



51
52
53
# File 'lib/ransack_mongo/mongo_adapter.rb', line 51

def gteq_matcher(attr, value)
  append_sizeable_matcher('$gte', attr, value)
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



47
48
49
# File 'lib/ransack_mongo/mongo_adapter.rb', line 47

def lt_matcher(attr, value)
  append_sizeable_matcher('$lt', attr, value)
end

#lteq_matcher(attr, value) ⇒ Object



55
56
57
# File 'lib/ransack_mongo/mongo_adapter.rb', line 55

def lteq_matcher(attr, value)
  append_sizeable_matcher('$lte', attr, value)
end

#mstart_matcher(attr, value) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ransack_mongo/mongo_adapter.rb', line 33

def mstart_matcher(attr, value)
  values = value.split(",").map do |current|
    if (current = current.strip).length > 0
      /^#{current}/i
    end
  end.compact

  @query[attr] = { '$in' => values }
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



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ransack_mongo/mongo_adapter.rb', line 59

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

#start_matcher(attr, value) ⇒ Object



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

def start_matcher(attr, value)
  @query[attr] = { '$in' => [/^#{value}/] }
end

#to_queryObject



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

def to_query
  @query
end