Class: RansackMongo::MongoAdapter

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

Constant Summary collapse

PREDICATES =
%w[null not_null eq not_eq cont cont_any 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



86
87
88
# File 'lib/ransack_mongo/mongo_adapter.rb', line 86

def self.predicates
  PREDICATES
end

Instance Method Details

#cont_any_matcher(attr, value_arr) ⇒ Object



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

def cont_any_matcher(attr, value_arr)
  value = Array(value_arr).join('|')
  cont_matcher(attr, value)
end

#cont_matcher(attr, value) ⇒ Object



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

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

#eq_matcher(attr, value) ⇒ Object



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

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

#gt_matcher(attr, value) ⇒ Object



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

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

#gteq_matcher(attr, value) ⇒ Object



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

def gteq_matcher(attr, value)
  append_sizeable_matcher('$gte', attr, value)
end

#in_matcher(attr, value) ⇒ Object



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

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

#lt_matcher(attr, value) ⇒ Object



60
61
62
# File 'lib/ransack_mongo/mongo_adapter.rb', line 60

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

#lteq_matcher(attr, value) ⇒ Object



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

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

#mstart_matcher(attr, value) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ransack_mongo/mongo_adapter.rb', line 46

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



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

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

#not_null_matcher(attr, value) ⇒ Object



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

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

#null_matcher(attr, value) ⇒ Object



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

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

#or_opObject

or operation



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ransack_mongo/mongo_adapter.rb', line 72

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



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

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