Module: Doing::ItemQuery

Included in:
Item
Defined in:
lib/doing/item/query.rb

Overview

Tag and search filtering for a Doing entry

Instance Method Summary collapse

Instance Method Details

#highlight_search(search, distance: nil, negate: false, case_type: nil) ⇒ Object



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
# File 'lib/doing/item/query.rb', line 72

def highlight_search(search, distance: nil, negate: false, case_type: nil)
  prefs = Doing.setting('search', {})
  matching = prefs.fetch('matching', 'pattern').normalize_matching
  distance ||= prefs.fetch('distance', 3).to_i
  case_type ||= prefs.fetch('case', 'smart').normalize_case
  new_note = Note.new

  if search.rx? || matching == :fuzzy
    rx = search.to_rx(distance: distance, case_type: case_type)
    new_title = @title.gsub(rx) { |m| yellow(m) }
    new_note.add(@note.to_s.gsub(rx) { |m| yellow(m) })
  else
    query = search.strip.to_phrase_query

    if query[:must].nil? && query[:must_not].nil?
      query[:must] = query[:should]
      query[:should] = []
    end
    query[:must].concat(query[:should]).each do |s|
      rx = Regexp.new(s.wildcard_to_rx, ignore_case(s, case_type))
      new_title = @title.gsub(rx) { |m| yellow(m) }
      new_note.add(@note.to_s.gsub(rx) { |m| yellow(m) })
    end
  end

  Item.new(@date, new_title, @section, new_note)
end

#ignore_case(search, case_type) ⇒ Boolean

Determine if case should be ignored for searches

Parameters:

  • search (String)

    The search string

  • case_type (Symbol)

    The case type

Returns:

  • (Boolean)

    case should be ignored



68
69
70
# File 'lib/doing/item/query.rb', line 68

def ignore_case(search, case_type)
  (case_type == :smart && search !~ /[A-Z]/) || case_type == :ignore
end

#keep_item?(opt) ⇒ Boolean

Used by filter_items determine whether an item matches a set of criteria

Parameters:

  • opt (Hash)

    filter parameters

Returns:

  • (Boolean)

    whether the item matches all filter criteria



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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/doing/item/query.rb', line 162

def keep_item?(opt)
  item = dup
  time_rx = /^(\d{1,2}+(:\d{1,2}+)?( *(am|pm))?|midnight|noon)$/i

  keep = true
  if opt[:unfinished]
    finished = item.tags?('done', :and)
    finished = opt[:not] ? !finished : finished
    keep = false if finished
  end

  if keep && opt[:val]&.count&.positive?
    bool = opt[:bool].normalize_bool if opt[:bool]
    bool ||= :and
    bool = :and if bool == :pattern

    val_match = opt[:val].nil? || opt[:val].empty? ? true : item.tag_values?(opt[:val], bool)
    keep = false unless val_match
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:tag]
    opt[:tag_bool] = opt[:bool].normalize_bool if opt[:bool]
    opt[:tag_bool] ||= :and
    tag_match = opt[:tag].nil? || opt[:tag].empty? ? true : item.tags?(opt[:tag], opt[:tag_bool])
    keep = false unless tag_match
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:search]
    search_match = if opt[:search].nil? || opt[:search].empty?
                     true
                   else
                     item.search(opt[:search], case_type: opt[:case].normalize_case)
                   end

    keep = false unless search_match
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:date_filter]&.length == 2
    start_date = opt[:date_filter][0]
    end_date = opt[:date_filter][1]

    in_date_range = if end_date
                      item.date >= start_date && item.date <= end_date
                    else
                      item.date.strftime('%F') == start_date.strftime('%F')
                    end
    keep = false unless in_date_range
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:time_filter][0] || opt[:time_filter][1]
    opt[:time_filter].map! { |v| v =~ /(12 *am|midnight)/i ? '00:00' : v }

    start_string = if opt[:time_filter][0].nil?
                     "#{item.date.strftime('%Y-%m-%d')} 00:00"
                   else
                     "#{item.date.strftime('%Y-%m-%d')} #{opt[:time_filter][0]}"
                   end
    start_time = start_string.chronify(guess: :begin)

    end_string = if opt[:time_filter][1].nil?
                   "#{item.date.to_datetime.next_day.strftime('%Y-%m-%d')} 00:00"
                 else
                   "#{item.date.strftime('%Y-%m-%d')} #{opt[:time_filter][1]}"
                 end
    end_time = end_string.chronify(guess: :end) || Time.now

    in_time_range = item.date >= start_time && item.date <= end_time

    keep = false unless in_time_range
    keep = opt[:not] ? !keep : keep
  end

  keep = false if keep && opt[:only_timed] && !item.interval

  if keep && opt[:tag_filter]
    keep = item.tags?(opt[:tag_filter]['tags'], opt[:tag_filter]['bool'])
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:before]
    before = opt[:before]
    cutoff = if before.is_a?(String) && before =~ time_rx
               "#{item.date.strftime('%Y-%m-%d')} #{before}".chronify(guess: :begin)
             elsif before.is_a?(String)
               before.chronify(guess: :begin)
             else
               before
             end
    keep = cutoff && item.date <= cutoff
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:after]
    after = opt[:after]
    cutoff = if after.is_a?(String) && after =~ time_rx
               "#{item.date.strftime('%Y-%m-%d')} #{after}".chronify(guess: :end)
             elsif after.is_a?(String)
               after.chronify(guess: :end)
             else
               after
             end
    keep = cutoff && item.date >= cutoff
    keep = opt[:not] ? !keep : keep
  end

  if keep && opt[:today]
    keep = item.date >= Date.today.to_time && item.date < Date.today.next_day.to_time
    keep = opt[:not] ? !keep : keep
  elsif keep && opt[:yesterday]
    keep = item.date >= Date.today.prev_day.to_time && item.date < Date.today.to_time
    keep = opt[:not] ? !keep : keep
  end

  keep
end

#search(search, distance: nil, negate: false, case_type: nil) ⇒ Boolean

Test if item matches search string

Parameters:

  • search (String)

    The search string

  • negate (Boolean) (defaults to: false)

    negate results

  • case_type (Symbol) (defaults to: nil)

    The case-sensitivity type (:sensitive, :ignore, :smart)

Returns:

  • (Boolean)

    matches search criteria



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
152
153
# File 'lib/doing/item/query.rb', line 111

def search(search, distance: nil, negate: false, case_type: nil)
  prefs = Doing.setting('search', {})
  matching = prefs.fetch('matching', 'pattern').normalize_matching
  distance ||= prefs.fetch('distance', 3).to_i
  case_type ||= prefs.fetch('case', 'smart').normalize_case

  if search.rx? || matching == :fuzzy
    matches = @title + @note.to_s =~ search.to_rx(distance: distance, case_type: case_type)
  else
    query = search.strip.to_phrase_query

    if query[:must].nil? && query[:must_not].nil?
      query[:must] = query[:should]
      query[:should] = []
    end
    matches = no_searches?(query[:must_not], case_type: case_type)
    matches &&= all_searches?(query[:must], case_type: case_type)
    matches &&= any_searches?(query[:should], case_type: case_type)
  end
  # if search =~ /(?<=\A| )[+-]\S/
  # else
  #   text = @title + @note.to_s
  #   matches = text =~ search.to_rx(distance: distance, case_type: case_type)
  # end

  # if search.rx? || !fuzzy
  #   matches = text =~ search.to_rx(distance: distance, case_type: case_type)
  # else
  #   distance = 0.25 if distance > 1
  #   score = if (case_type == :smart && search !~ /[A-Z]/) || case_type == :ignore
  #             text.downcase.pair_distance_similar(search.downcase)
  #           else
  #             score = text.pair_distance_similar(search)
  #           end

  #   if score >= distance
  #     matches = true
  #     Doing.logger.debug('Fuzzy Match:', %(#{@title}, "#{search}" #{score}))
  #   end
  # end

  negate ? !matches : matches
end

#tag_values?(queries, bool = :and, negate: false) ⇒ Boolean

Test if item matches tag values

Parameters:

  • queries (Array)

    The tag value queries to test

  • bool (Symbol) (defaults to: :and)

    The boolean to use for multiple tags (:and, :or, :not)

  • negate (Boolean) (defaults to: false)

    negate the result?

Returns:

  • (Boolean)

    true if tag/bool combination passes



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/doing/item/query.rb', line 46

def tag_values?(queries, bool = :and, negate: false)
  bool = bool.normalize_bool

  matches = case bool
            when :and
              all_values?(queries)
            when :not
              no_values?(queries)
            else
              any_values?(queries)
            end
  negate ? !matches : matches
end

#tags?(tags, bool = :and, negate: false) ⇒ Boolean

Test if item contains tag(s)

Parameters:

  • tags (Array or String)

    The tags to test. Can be an array or a comma-separated string.

  • bool (Symbol) (defaults to: :and)

    The boolean to use for multiple tags (:and, :or, :not)

  • negate (Boolean) (defaults to: false)

    negate the result?

Returns:

  • (Boolean)

    true if tag/bool combination passes



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/doing/item/query.rb', line 15

def tags?(tags, bool = :and, negate: false)
  if bool == :pattern
    tags = tags.to_tags.tags_to_array.join(' ')
    matches = tag_pattern?(tags)

    return negate ? !matches : matches
  end

  tags = split_tags(tags)
  bool = bool.normalize_bool

  matches = case bool
            when :and
              all_tags?(tags)
            when :not
              no_tags?(tags)
            else
              any_tags?(tags)
            end
  negate ? !matches : matches
end