Class: Pose::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/pose/query.rb

Overview

Represents a search query.

Provides convenient access to all elements of the search query:

* fulltext
* classes to search in
* additional JOINs
* additional WHEREs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(classes, text, options = {}) ⇒ Query

Returns a new instance of Query.



15
16
17
18
19
# File 'lib/pose/query.rb', line 15

def initialize classes, text, options = {}
  @classes = Array(classes).flatten
  @text = text
  @options = options
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



12
13
14
# File 'lib/pose/query.rb', line 12

def classes
  @classes
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/pose/query.rb', line 12

def options
  @options
end

#textObject (readonly)

Returns the value of attribute text.



12
13
14
# File 'lib/pose/query.rb', line 12

def text
  @text
end

Class Method Details

.is_url?(word) ⇒ Boolean

Returns whether the given string is a URL.

Parameters:

  • word (String)

    The string to check.

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/pose/query.rb', line 59

def self.is_url? word

  # Handle localhost separately.
  return true if /^http:\/\/localhost(:\d+)?/ =~ word

  /^https?:\/\/([\w\.])+\.([\w\.])+/ =~ word
end

.query_words(query_string) ⇒ Object



86
87
88
# File 'lib/pose/query.rb', line 86

def self.query_words query_string
  query_string.split(' ').map{|query_word| Query.root_word query_word}.flatten.uniq
end

.root_word(raw_word) ⇒ String

Simplifies the given word to a generic search form.

Parameters:

  • raw_word (String)

    The word to make searchable.

Returns:

  • (String)

    The stemmed version of the word.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pose/query.rb', line 96

def self.root_word raw_word
  result = []
  raw_word_copy = raw_word[0..-1]
  raw_word_copy.gsub! '%20', ' '
  raw_word_copy.gsub! /[()*<>'",;\?\-\=&%#]/, ' '
  raw_word_copy.gsub! /\s+/, ' '
  raw_word_copy.split(' ').each do |word|
    if Query.is_url?(word)
      result.concat word.split(/[\.\/\:]/).delete_if(&:blank?)
    else
      word.gsub! /[\-\/\._:]/, ' '
      word.gsub! /\s+/, ' '
      word.split(' ').each do |w|
        stemmed_word =  w.parameterize.singularize
        result.concat stemmed_word.split ' '
      end
    end
  end
  result.uniq
end

Instance Method Details

#class_namesArray<String>

The names of the classes to search in.

Returns:

  • (Array<String>)


24
25
26
# File 'lib/pose/query.rb', line 24

def class_names
  classes.map &:name
end

#has_joins?Boolean

Returns whether this query contains custom JOIN expressions.

Returns:

  • (Boolean)


30
31
32
# File 'lib/pose/query.rb', line 30

def has_joins?
  @options[:joins].present?
end

#has_limit?Boolean

Returns whether the query defines a limit on the number of results.

Returns:

  • (Boolean)


36
37
38
# File 'lib/pose/query.rb', line 36

def has_limit?
  @options[:limit].present?
end

#has_where?Boolean

Returns whether this query contains WHERE clauses.

Returns:

  • (Boolean)


42
43
44
# File 'lib/pose/query.rb', line 42

def has_where?
  @options[:where].present?
end

#ids_requested?Boolean

Returns whether only result ids are requested, opposed to full objects.

Returns:

  • (Boolean)


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

def ids_requested?
  @options[:result_type] == :ids
end

#joinsObject

Returns the custom JOIN expressions of this query.



69
70
71
# File 'lib/pose/query.rb', line 69

def joins
  @joins ||= Array(@options[:joins]).flatten.compact
end

#limitObject

Returns the limitation on the number of results.



75
76
77
# File 'lib/pose/query.rb', line 75

def limit
  @options[:limit]
end

#query_wordsObject

Returns the search terms that are contained in the given query.



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

def query_words
  @query_words ||= Query.query_words @text
end

#whereObject

Returns the WHERE clause of this query.



119
120
121
122
123
# File 'lib/pose/query.rb', line 119

def where
  return [] unless has_where?
  return [ @options[:where] ] if @options[:where].size == 2 && @options[:where][0].is_a?(String)
  @options[:where]
end