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

#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