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
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 |
# File 'lib/command_search/mongoer.rb', line 43 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/ 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 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
129 130 131 132 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 |
# File 'lib/command_search/mongoer.rb', line 129 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
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/command_search/mongoer.rb', line 256 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
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/command_search/mongoer.rb', line 204 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
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/command_search/mongoer.rb', line 221 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
235 236 237 238 239 240 |
# File 'lib/command_search/mongoer.rb', line 235 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
242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/command_search/mongoer.rb', line 242 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
33 34 35 36 |
# File 'lib/command_search/mongoer.rb', line 33 def is_bool_str?(str) return true if str[/\Atrue\Z|\Afalse\Z/i] false end |
.make_boolean(str) ⇒ Object
38 39 40 41 |
# File 'lib/command_search/mongoer.rb', line 38 def make_boolean(str) return true if str[/\Atrue\Z/i] false end |