Module: CommandSearch::Mongoer
- Defined in:
- lib/command_search/mongoer.rb
Class Method Summary collapse
- .build_command(ast_node, command_types) ⇒ Object
- .build_compare(ast_node, command_types) ⇒ Object
- .build_query(ast, fields, command_types = {}) ⇒ Object
- .build_search(ast_node, fields) ⇒ Object
- .build_searches(ast, fields, command_types) ⇒ Object
- .build_tree(ast) ⇒ Object
- .collapse_ors(ast) ⇒ Object
- .decompose_nots(ast, not_depth = 0) ⇒ Object
- .is_bool_str?(str) ⇒ Boolean
- .make_boolean(str) ⇒ Object
Class Method Details
.build_command(ast_node, command_types) ⇒ Object
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 |
# File 'lib/command_search/mongoer.rb', line 45 def build_command(ast_node, command_types) # aliasing will is done before ast gets to mongoer.rb (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 defined?(Boolean) && type == Boolean # val = make_boolean(raw_val) 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/ 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[/^[\.0]*0$/] val = raw_val.to_f else val = raw_val end elsif type == Time time_str = raw_val.tr('_.-', ' ') date = Chronic.parse(time_str, guess: nil) 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
133 134 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 |
# File 'lib/command_search/mongoer.rb', line 133 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 type == Time # 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('_.-', ' ') date = Chronic.parse(time_str, guess: nil) 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
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/command_search/mongoer.rb', line 259 def build_query(ast, fields, command_types = {}) # Numbers are searched as strings unless part of a compare/command out = ast out = decompose_nots(out) out = build_searches(out, fields, command_types) out = build_tree(out) 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 32 33 |
# 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 # TODO: This OR check is only needed due to mutable objects between subclasses of command_search, # and is only needed for outside use or benchmarking. 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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/command_search/mongoer.rb', line 208 def build_searches(ast, fields, command_types) ast.flat_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) x[:value] = build_searches(x[:value], fields, command_types) x else build_search(x, fields) end end end |
.build_tree(ast) ⇒ Object
224 225 226 227 228 229 230 231 |
# File 'lib/command_search/mongoer.rb', line 224 def build_tree(ast) ast.flat_map do |x| next x unless x[:nest_type] mongo_types = { paren: '$and', pipe: '$or', minus: '$not' } key = mongo_types[x[:nest_type]] { key => build_tree(x[:value]) } end end |
.collapse_ors(ast) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/command_search/mongoer.rb', line 233 def collapse_ors(ast) ast.flat_map do |x| ['$and', '$or', '$not'].map do |key| next unless x[key] x[key] = collapse_ors(x[key]) end next x unless x['$or'] val = x['$or'].flat_map { |kid| kid['$or'] || kid } { '$or' => val } end end |
.decompose_nots(ast, not_depth = 0) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/command_search/mongoer.rb', line 245 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
35 36 37 38 |
# File 'lib/command_search/mongoer.rb', line 35 def is_bool_str?(str) return true if str[/^true$|^false$/i] false end |
.make_boolean(str) ⇒ Object
40 41 42 43 |
# File 'lib/command_search/mongoer.rb', line 40 def make_boolean(str) return true if str[/^true$/i] false end |