Class: Queries::Query

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

Direct Known Subclasses

BiologicalCollectionObjectAutocompleteQuery, CollectionObjectFilterQuery, ContainerAutocompleteQuery, ControlledVocabularyTermAutocompleteQuery, GeographicAreaAutocompleteQuery, OtuAutocompleteQuery, SourceAutocompleteQuery, TagAutocompleteQuery, TaxonNameAutocompleteQuery, TypeMaterialAutocompleteQuery

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Returns a new instance of Query



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

def initialize(string, project_id: nil)
  @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



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

def dynamic_limit
  @dynamic_limit
end

- (Object) project_id

Returns the value of attribute project_id



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

def project_id
  @project_id
end

- (Object) query_string

Returns the value of attribute query_string



14
15
16
# File 'lib/queries.rb', line 14

def query_string
  @query_string
end

- (Object) terms

Returns the value of attribute terms



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

def terms
  @terms
end

Instance Method Details

- (Object) build_terms

Replace with a full text indexing approach



77
78
79
# File 'lib/queries.rb', line 77

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

- (Object) end_wildcard



47
48
49
# File 'lib/queries.rb', line 47

def end_wildcard
  query_string + '%'
end

- (Object) identifier_table



133
134
135
# File 'lib/queries.rb', line 133

def identifier_table
  Identifier.arel_table
end

- (Object) integers



59
60
61
# File 'lib/queries.rb', line 59

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

- (Object) named



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

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

- (Object) no_digits



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

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

- (Object) parent



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

def parent 
  table.alias 
end

- (Object) parent_child_join

generic multi-use bits



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

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“



103
104
105
106
107
# File 'lib/queries.rb', line 103

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



29
30
31
# File 'lib/queries.rb', line 29

def result
  []
end

- (Object) scope



33
34
35
# File 'lib/queries.rb', line 33

def scope
  where('1 = 2') 
end

- (Object) start_and_end_wildcard



51
52
53
# File 'lib/queries.rb', line 51

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

- (Object) start_wildcard



43
44
45
# File 'lib/queries.rb', line 43

def start_wildcard
  '%' + query_string 
end

- (Object) strings



67
68
69
70
# File 'lib/queries.rb', line 67

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

- (Object) wildcard_wrapped_integers



63
64
65
# File 'lib/queries.rb', line 63

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

- (Object) with_id



109
110
111
112
113
114
115
# File 'lib/queries.rb', line 109

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

- (Object) with_identifier_like



137
138
139
# File 'lib/queries.rb', line 137

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

- (Object) with_project_id



125
126
127
128
129
130
131
# File 'lib/queries.rb', line 125

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



72
73
74
# File 'lib/queries.rb', line 72

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