37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/visual_condition_builder/converter.rb', line 37
def self.to_mongo(params)
query = []
conditions = params.is_a?(Array) ? params : JSON.parse(params ||= '[]')
conditions.map do |p|
tmp = {}
case p[1].to_s.downcase.to_sym
when :eq
tmp[p[0]] = p[2]
when :not_eq
tmp[p[0]] = Hash["$ne", p[2]]
when :matches
tmp[p[0]] = Hash["$regex",p[2]]
when :does_not_match
tmp[p[0]] = Hash["$regex","^((?!#{p[2]}).)*$"]
tmp[p[0]]["$options"] = "s"
when :lt
tmp[p[0]] = Hash["$lt", p[2]]
when :gt
tmp[p[0]] = Hash["$gt", p[2]]
when :lteq
tmp[p[0]] = Hash["$lte", p[2]]
when :gteq
tmp[p[0]] = Hash["$gte", p[2]]
when :in
tmp[p[0]] = Hash["$in", p[2]]
when :not_in
tmp[p[0]] = Hash["$nin", p[2]]
when :cont
tmp[p[0]] = Hash["$regex",p[2]]
when :not_cont
tmp[p[0]] = Hash["$regex","^((?!#{p[2]}).)*$"]
tmp[p[0]]["$options"] = "s"
when :cont_any
if p[2].is_a?(Array)
tmpAny = []
p[2].each do |v|
tmpAny << Hash[tmp[p[0]], Hash["$regex", v]]
end
tmp["$or"] = tmpAny
end
when :not_cont_any
if p[2].is_a?(Array)
tmpAny = []
p[2].each do |v|
tmpAny << Hash[tmp[p[0]], Hash["$regex", "^((?!#{v}).)*$", "$options" => "s"]]
end
tmp["$or"] = tmpAny
end
when :cont_all
if p[2].is_a?(Array)
tmpAny = []
p[2].each do |v|
tmpAny << Hash[tmp[p[0]], Hash["$regex", v]]
end
tmp["$and"] = tmpAny
end
when :not_cont_all
if p[2].is_a?(Array)
tmpAny = []
p[2].each do |v|
tmpAny << Hash[tmp[p[0]], Hash["$regex", "^((?!#{v}).)*$", "$options" => "s"]]
end
tmp["$and"] = tmpAny
end
when :start
tmp[p[0]] = Hash["$regex", "^#{p[2]}"]
when :not_start
tmp[p[0]] = Hash["$regex", "^(?!#{p[2]})"]
when :end
tmp[p[0]] = Hash["$regex", "#{p[2]}$"]
when :not_end
tmp[p[0]] = Hash["$regex", "(?<!#{p[2]})$"]
when :true
tmp[p[0]] = 'true'
when :not_true
tmp[p[0]] = Hash["$ne", 'true']
when :false
tmp[p[0]] = 'false'
when :not_false
tmp[p[0]] = Hash["$ne", 'false']
when :present
tmp[p[0]] = Hash["$ne", '']
when :blank
tmp[p[0]] = ''
when :null
tmp[p[0]] = 'null'
when :not_null
tmp[p[0]] = Hash["$ne", 'null']
when :between
if p[2].is_a?(Array)
tmp[p[0]]["$gte"] = p[2][0] if p[2][0]
tmp[p[0]]["$lte"] = p[2][1] if p[2][1]
end
when :today
tmp[p[0]] = Date.today
when :yesterday
tmp[p[0]] = Date.yesterday
when :tomorrow
tmp[p[0]] = Date.tomorrow
when :this_week
tmp[p[0]] = Hash["$gte", Date.today.beginning_of_week]
tmp[p[0]]["$lte"] = Date.today.end_of_week
when :last_week
tmp[p[0]] = Hash["$gte", 1.week.ago.beginning_of_week]
tmp[p[0]]["$lte"] = 1.week.ago.end_of_week
when :next_week
tmp[p[0]] = Hash["$gte", Date.today.next_week.beginning_of_week]
tmp[p[0]]["$lte"] = Date.today.next_week.end_of_week
else
tmp[p[0]] = Hash[p[1],p[2]]
end
query << tmp if tmp.present?
end
query.present? ? Hash["$and", query] : {}
end
|