Module: CommandSearch::Normalizer
- Defined in:
- lib/command_search/normalizer.rb
Class Method Summary collapse
- .cast_bool(type, node) ⇒ Object
- .cast_regex(node) ⇒ Object
- .cast_time(node) ⇒ Object
- .dealias!(ast, cmd_fields) ⇒ Object
- .dealias_key(key, cmd_fields) ⇒ Object
- .flip_operator!(node, cmd_fields) ⇒ Object
- .normalize!(ast, cmd_fields) ⇒ Object
Class Method Details
.cast_bool(type, node) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/command_search/normalizer.rb', line 7 def cast_bool(type, node) if type == Boolean node[:type] = Boolean node[:value] = !!node[:value][0][/t/i] return end return unless type.is_a?(Array) && type.include?(:allow_existence_boolean) return unless node[:type] == :str && node[:value][/\Atrue\Z|\Afalse\Z/i] node[:type] = :existence node[:value] = !!node[:value][0][/t/i] end |
.cast_regex(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/command_search/normalizer.rb', line 46 def cast_regex(node) type = node[:type] return unless type == :str || type == :quoted_str || type == :number raw = node[:value] str = Regexp.escape(raw) return node[:value] = /#{str}/i unless type == :quoted_str return node[:value] = '' if raw == '' return node[:value] = /\b#{str}\b/ unless raw[/(^\W)|(\W$)/] border_a = '(^|\s|[^:+\w])' border_b = '($|\s|[^:+\w])' node[:value] = Regexp.new(border_a + str + border_b) end |
.cast_time(node) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/command_search/normalizer.rb', line 19 def cast_time(node) search_node = node[:value][1] search_node[:type] = Time str = search_node[:value] if str == str.to_i.to_s search_node[:value] = [Time.new(str), Time.new(str.to_i + 1)] else time_str = str.tr('._-', ' ') times = Chronic.parse(time_str, { guess: nil }) times ||= Chronic.parse(str, { guess: nil }) if times search_node[:value] = [times.first, times.last] else search_node[:value] = nil return end end return unless node[:nest_type] == :compare op = node[:nest_op] if op == '<' || op == '>=' search_node[:value] = search_node[:value].first else search_node[:value] = search_node[:value].last search_node[:value] -= 1 end end |
.dealias!(ast, cmd_fields) ⇒ Object
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 |
# File 'lib/command_search/normalizer.rb', line 73 def dealias!(ast, cmd_fields) ast.map! do |node| nest = node[:nest_type] unless nest node[:number_value] = node[:value] if node[:type] == :number cast_regex(node) next node end unless nest == :colon || nest == :compare dealias!(node[:value], cmd_fields) next node end flip_operator!(node, cmd_fields) if nest == :compare (key_node, search_node) = node[:value] new_key = dealias_key(key_node[:value], cmd_fields) type = cmd_fields[new_key.to_sym] node[:value][0][:value] = new_key if type cast_bool(type, search_node) type = (type - [:allow_existence_boolean]).first if type.is_a?(Array) cast_time(node) if [Time, Date, DateTime].include?(type) cast_regex(search_node) if type == String next node end str_values = "#{new_key}#{node[:nest_op]}#{search_node[:value]}" node = { type: :str, value: str_values } cast_regex(node) node end end |
.dealias_key(key, cmd_fields) ⇒ Object
68 69 70 71 |
# File 'lib/command_search/normalizer.rb', line 68 def dealias_key(key, cmd_fields) key = cmd_fields[key.to_sym] while cmd_fields[key.to_sym].is_a?(Symbol) key.to_s end |
.flip_operator!(node, cmd_fields) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/command_search/normalizer.rb', line 59 def flip_operator!(node, cmd_fields) val = node[:value] return if cmd_fields[val[0][:value].to_sym] return unless cmd_fields[val[1][:value].to_sym] flip_ops = { '<' => '>', '>' => '<', '<=' => '>=', '>=' => '<=' } node[:nest_op] = flip_ops[node[:nest_op]] node[:value].reverse! end |
.normalize!(ast, cmd_fields) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/command_search/normalizer.rb', line 104 def normalize!(ast, cmd_fields) dealias!(ast, cmd_fields) clean = {} cmd_fields.each do |k, v| next if v.is_a?(Symbol) if v.is_a?(Array) clean[k] = (v - [:allow_existence_boolean]).first next end v = Numeric if v == Integer v = Time if v == Date v = Time if v == DateTime next clean[k] = v end clean end |