Class: Queries::Query

Inherits:
Object
  • Object
show all
Includes:
Arel::Nodes
Defined in:
lib/queries.rb

Direct Known Subclasses

BiologicalCollectionObjectAutocompleteQuery, CollectionObjectFilterQuery, ControlledVocabularyTermAutocompleteQuery, DocumentAutocompleteQuery, GeographicAreaAutocompleteQuery, OtuAutocompleteQuery, SourceFilterQuery, TagAutocompleteQuery, TaxonNameAutocompleteQuery, TypeMaterialAutocompleteQuery

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Query) initialize(string, project_id: nil, **keyword_args)

Returns a new instance of Query



23
24
25
26
27
# File 'lib/queries.rb', line 23

def initialize(string, project_id: nil, **keyword_args)
  @query_string = string
  @project_id = project_id
  build_terms
end

Instance Attribute Details

- (Object) dynamic_limit

limit based on size and potentially properties of terms



21
22
23
# File 'lib/queries.rb', line 21

def dynamic_limit
  @dynamic_limit
end

- (Object) project_id

Returns the value of attribute project_id



18
19
20
# File 'lib/queries.rb', line 18

def project_id
  @project_id
end

- (Object) query_string

Returns the value of attribute query_string



16
17
18
# File 'lib/queries.rb', line 16

def query_string
  @query_string
end

- (Object) terms

Returns the value of attribute terms



17
18
19
# File 'lib/queries.rb', line 17

def terms
  @terms
end

Instance Method Details

- (Object) build_terms

Replace with a full text indexing approach



95
96
97
# File 'lib/queries.rb', line 95

def build_terms
  @terms = [end_wildcard, start_and_end_wildcard]  # query_string.split(/\s+/).compact.collect{|t| [t, "#{t}%", "%#{t}%"]}.flatten
end

- (ActiveRecord::Relation?) cached

cached matches full query string wildcarded

Returns:

  • (ActiveRecord::Relation, nil)


177
178
179
180
181
182
183
# File 'lib/queries.rb', line 177

def cached
  if !terms.empty?
    table[:cached].matches_any(terms)
  else
    nil
  end
end

- (Object) end_wildcard



49
50
51
# File 'lib/queries.rb', line 49

def end_wildcard
  query_string + '%'
end

- (Object) exactly_named



151
152
153
# File 'lib/queries.rb', line 151

def exactly_named
  table[:name].eq(query_string)
end

- (Array) fragments

Returns if 5 or fewer strings, those strings wrapped in wildcards, else none

Returns:

  • (Array)

    if 5 or fewer strings, those strings wrapped in wildcards, else none



86
87
88
89
90
91
92
# File 'lib/queries.rb', line 86

def fragments
  if strings && strings.count < 6
    strings.collect{|a| "%#{a}%"}
  else
    []
  end
end

- (Object) identifier_table



167
168
169
# File 'lib/queries.rb', line 167

def identifier_table
  Identifier.arel_table
end

- (Object) integers



61
62
63
# File 'lib/queries.rb', line 61

def integers
  query_string.split(/\s+/).select{|t| Utilities::Strings.is_i?(t)}
end

- (Object) named



147
148
149
# File 'lib/queries.rb', line 147

def named
  table[:name].matches_any(terms)
end

- (Object) no_digits



99
100
101
# File 'lib/queries.rb', line 99

def no_digits 
  query_string.gsub(/\d/, '').strip
end

- (Query?) only_ids

Returns used in or_clauses, match on id only if integers alone provided.

Returns:

  • (Query, nil)

    used in or_clauses, match on id only if integers alone provided.



139
140
141
142
143
144
145
# File 'lib/queries.rb', line 139

def only_ids
  if only_integers?
   with_id
  else
   nil
  end 
end

- (Boolean) only_integers?

Returns true if the query string only contains integers

Returns:

  • (Boolean)

    true if the query string only contains integers



80
81
82
# File 'lib/queries.rb', line 80

def only_integers?
  !(query_string =~ /[^\d\s]/i) && !integers.empty? 
end

- (Object) parent



155
156
157
# File 'lib/queries.rb', line 155

def parent 
  table.alias 
end

- (Object) parent_child_join

generic multi-use bits



116
117
118
# File 'lib/queries.rb', line 116

def parent_child_join
  table.join(parent).on(table[:parent_id].eq(parent[:id])).join_sources # !! join_sources ftw
end

- (Object) parent_child_where

Match at two levels, for example, 'wa te“ will match ”Washington Co., Texas“



121
122
123
124
125
# File 'lib/queries.rb', line 121

def parent_child_where
  a,b = query_string.split(/\s+/, 2)
  return table[:id].eq(-1) if a.nil? || b.nil?
  table[:name].matches("#{a}%").and(parent[:name].matches("#{b}%"))
end

- (Array) result

Returns the results of the query as an array

Returns:

  • (Array)

    the results of the query as an array



31
32
33
# File 'lib/queries.rb', line 31

def result
  []
end

- (Object) scope



35
36
37
# File 'lib/queries.rb', line 35

def scope
  where('1 = 2') 
end

- (Object) start_and_end_wildcard



53
54
55
# File 'lib/queries.rb', line 53

def start_and_end_wildcard
  '%' + query_string + '%'
end

- (Object) start_wildcard



45
46
47
# File 'lib/queries.rb', line 45

def start_wildcard
  '%' + query_string 
end

- (Object) strings



69
70
71
72
# File 'lib/queries.rb', line 69

def strings
  a = query_string.split(/\s+/).select{|t| !(t =~ /\d+/)} 
  a.empty? ? [ query_string] : a
end

- (Object) wildcard_wrapped_integers



65
66
67
# File 'lib/queries.rb', line 65

def wildcard_wrapped_integers
  integers.collect{|i| "%#{i}%"}
end

- (Query?) with_id

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



129
130
131
132
133
134
135
# File 'lib/queries.rb', line 129

def with_id
  if integers.any?
    table[:id].eq_any(integers)
  else
    nil
  end
end

- (Object) with_identifier_like



171
172
173
# File 'lib/queries.rb', line 171

def with_identifier_like
  identifier_table[:cached].matches(start_and_end_wildcard).or(identifier_table[:cached].matches_any(wildcard_wrapped_integers))
end

- (Object) with_project_id



159
160
161
162
163
164
165
# File 'lib/queries.rb', line 159

def with_project_id
  if project_id 
    table[:project_id].eq(project_id)
  else
    table[:project_id].matches('%') # could be optimized for sure
  end
end

- (Object) years



74
75
76
# File 'lib/queries.rb', line 74

def years
  integers.select{|a| a =~ /\b\d{4}\b/}.map(&:to_s).compact
end