Module: CommandSearch::Mongoer

Defined in:
lib/command_search/mongoer.rb

Class Method Summary collapse

Class Method Details

.build_command(ast_node, command_types) ⇒ Object



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
# File 'lib/command_search/mongoer.rb', line 41

def build_command(ast_node, command_types)
  (field_node, search_node) = ast_node[:value]
  key = field_node[:value]
  raw_type = command_types[key.to_sym]
  type = raw_type

  raw_val = search_node[:value]
  search_type = search_node[:type]

  if raw_type.is_a?(Array)
    is_bool = raw_type.include?(:allow_existence_boolean) && is_bool_str?(raw_val) && search_type != :quoted_str
    type = (raw_type - [:allow_existence_boolean]).first
  else
    is_bool = false
    type = raw_type
  end

  if type == Boolean
    bool = make_boolean(raw_val)
    bool = !bool if field_node[:negate]
    val = [
      { key => { '$exists' => true } },
      { key => { '$ne' => !bool } }
    ]
    key = '$and'
  elsif is_bool
    # This returns true for empty arrays, when it probably should not.
    # Alternativly, something like tags>5 could return things that have more
    # than 5 tags in the array.
    # https://stackoverflow.com/questions/22367335/mongodb-check-if-value-exists-for-a-field-in-a-document
    # val = { '$exists' => make_boolean(raw_val) }
    bool = make_boolean(raw_val)
    bool = !bool if field_node[:negate]
    if bool
      val = [
        { key => { '$exists' => true } },
        { key => { '$ne' => false } }
      ]
      key = '$and'
    else
      val = { '$exists' => false }
    end
  elsif type == String
    if search_type == :quoted_str
      val = /\b#{Regexp.escape(raw_val)}\b/
      val = '' if raw_val == ''
      if raw_val[/(^\W)|(\W$)/]
        head_border = '(?<=^|[^:+\w])'
        tail_border = '(?=$|[^:+\w])'
        val = Regexp.new(head_border + Regexp.escape(raw_val) + tail_border)
      end
    else
      val = /#{Regexp.escape(raw_val)}/i
    end
  elsif [Numeric, Integer].include?(type)
    if raw_val == raw_val.to_i.to_s
      val = raw_val.to_i
    elsif raw_val.to_f != 0 || raw_val[/\A[\.0]*0\Z/]
      val = raw_val.to_f
    else
      val = raw_val
    end
  elsif [Date, Time, DateTime].include?(type)
    time_str = raw_val.tr('_.-', ' ')
    if time_str == time_str.to_i.to_s
      date_begin = Time.new(time_str)
      date_end = Time.new(time_str.to_i + 1).yesterday
    else
      date = Chronic.parse(time_str, guess: nil)
      date_begin = date.begin
      date_end = date.end
    end
    if field_node[:negate]
      val = [
        { key => { '$gt' => date_end   } },
        { key => { '$lt' => date_begin } }
      ]
      key = '$or'
    else
      val = [
        { key => { '$gte' => date_begin } },
        { key => { '$lte' => date_end   } }
      ]
      key = '$and'
    end
  end

  if field_node[:negate] && (type == Numeric || type == String)
    { key => { '$not' => val } }
  else
    { key => val }
  end
end

.build_compare(ast_node, command_types) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/command_search/mongoer.rb', line 135

def build_compare(ast_node, command_types)
  flip_ops = {
    '<' => '>',
    '>' => '<',
    '<=' => '>=',
    '>=' => '<='
  }
  reverse_ops = {
    '<' => '>=',
    '<=' => '>',
    '>' => '<=',
    '>=' => '<'
  }
  mongo_op_map = {
    '<' => '$lt',
    '>' => '$gt',
    '<=' => '$lte',
    '>=' => '$gte'
  }

  keys = command_types.keys
  (first_node, last_node) = ast_node[:value]
  key = first_node[:value]
  val = last_node[:value]
  op = ast_node[:nest_op]
  op = reverse_ops[op] if first_node[:negate]

  if keys.include?(val.to_sym)
    (key, val) = [val, key]
    op = flip_ops[op]
  end

  mongo_op = mongo_op_map[op]
  raw_type = command_types[key.to_sym]

  if raw_type.is_a?(Array)
    type = (raw_type - [:allow_boolean]).first
  else
    type = raw_type
  end

  if command_types[val.to_sym]
    val = '$' + val
    key = '$' + key
    val = [key, val]
    key = '$expr'
  elsif type == Numeric
    if val == val.to_i.to_s
      val = val.to_i
    else
      val = val.to_f
    end
  elsif [Date, Time, DateTime].include?(type)
    # foo <  day | day.start
    # foo <= day | day.end
    # foo >  day | day.end
    # foo >= day | day.start
    date_start_map = {
      '<' => :start,
      '>' => :end,
      '<=' => :end,
      '>=' => :start
    }
    date_pick = date_start_map[op]
    time_str = val.tr('_.-', ' ')

    if time_str == time_str.to_i.to_s
      date = [Time.new(time_str), Time.new(time_str.to_i + 1).yesterday]
    else
      date = Chronic.parse(time_str, guess: nil)
    end

    if date_pick == :start
      val = date.first
    elsif date_pick == :end
      val = date.last
    end
  end
  { key => { mongo_op => val } }
end

.build_query(ast, fields, command_types = {}) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/command_search/mongoer.rb', line 268

def build_query(ast, fields, command_types = {})
  out = ast
  out = decompose_nots(out)
  build_searches!(out, fields, command_types)
  build_tree!(out)
  collapse_ors!(out)
  out = {} if out == []
  out = out.first if out.count == 1
  out = { '$and' => out } if out.count > 1
  out
end

.build_search(ast_node, fields) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/command_search/mongoer.rb', line 7

def build_search(ast_node, fields)
  str = ast_node[:value] || ''
  fields = [fields] unless fields.is_a?(Array)
  if ast_node[:type] == :quoted_str
    regex = /\b#{Regexp.escape(str)}\b/
    if str[/(^\W)|(\W$)/]
      head_border = '(?<=^|[^:+\w])'
      tail_border = '(?=$|[^:+\w])'
      regex = Regexp.new(head_border + Regexp.escape(str) + tail_border)
    end
  else
    regex = /#{Regexp.escape(str)}/i
  end
  if ast_node[:negate]
    forms = fields.map { |f| { f => { '$not' => regex } } }
  else
    forms = fields.map { |f| { f => regex } }
  end
  return forms if forms.count < 2
  if ast_node[:negate]
    { '$and' => forms }
  else
    { '$or' => forms }
  end
end

.build_searches!(ast, fields, command_types) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/command_search/mongoer.rb', line 216

def build_searches!(ast, fields, command_types)
  ast.map! do |x|
    type = x[:nest_type]
    if type == :colon
      build_command(x, command_types)
    elsif type == :compare
      build_compare(x, command_types)
    elsif [:paren, :pipe, :minus].include?(type)
      build_searches!(x[:value], fields, command_types)
      x
    else
      build_search(x, fields)
    end
  end
  ast.flatten!
end

.build_tree!(ast) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/command_search/mongoer.rb', line 233

def build_tree!(ast)
  mongo_types = { paren: '$and', pipe: '$or', minus: '$not' }
  ast.each do |x|
    next x unless x[:nest_type]
    build_tree!(x[:value])
    key = mongo_types[x[:nest_type]]
    x[key] = x[:value]
    x.delete(:nest_type)
    x.delete(:nest_op)
    x.delete(:value)
    x.delete(:type)
  end
end

.collapse_ors!(ast) ⇒ Object



247
248
249
250
251
252
# File 'lib/command_search/mongoer.rb', line 247

def collapse_ors!(ast)
  ast.each do |x|
    next unless x['$or']
    x['$or'].map! { |kid| kid['$or'] || kid }.flatten!
  end
end

.decompose_nots(ast, not_depth = 0) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/command_search/mongoer.rb', line 254

def decompose_nots(ast, not_depth = 0)
  ast.flat_map do |x|
    if x[:nest_type] == :minus
      decompose_nots(x[:value], not_depth + 1)
    elsif x[:nest_type]
      x[:value] = decompose_nots(x[:value], not_depth)
      x
    else
      x[:negate] = not_depth.odd?
      x
    end
  end
end

.is_bool_str?(str) ⇒ Boolean

Returns:



33
34
35
# File 'lib/command_search/mongoer.rb', line 33

def is_bool_str?(str)
  str[/\Atrue\Z|\Afalse\Z/i]
end

.make_boolean(str) ⇒ Object



37
38
39
# File 'lib/command_search/mongoer.rb', line 37

def make_boolean(str)
  str[/\Atrue\Z/i]
end