Class: Pose::Query
- Inherits:
-
Object
- Object
- Pose::Query
- 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
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
-
.is_url?(word) ⇒ Boolean
Returns whether the given string is a URL.
- .query_words(query_string) ⇒ Object
-
.root_word(raw_word) ⇒ String
Simplifies the given word to a generic search form.
Instance Method Summary collapse
-
#class_names ⇒ Array<String>
The names of the classes to search in.
-
#has_joins? ⇒ Boolean
Returns whether this query contains custom JOIN expressions.
-
#has_limit? ⇒ Boolean
Returns whether the query defines a limit on the number of results.
-
#has_where? ⇒ Boolean
Returns whether this query contains WHERE clauses.
-
#ids_requested? ⇒ Boolean
Returns whether only result ids are requested, opposed to full objects.
-
#initialize(classes, text, options = {}) ⇒ Query
constructor
A new instance of Query.
-
#joins ⇒ Object
Returns the custom JOIN expressions of this query.
-
#limit ⇒ Object
Returns the limitation on the number of results.
-
#query_words ⇒ Object
Returns the search terms that are contained in the given query.
-
#where ⇒ Object
Returns the WHERE clause of this query.
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, = {} @classes = Array(classes).flatten @text = text = end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
12 13 14 |
# File 'lib/pose/query.rb', line 12 def classes @classes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/pose/query.rb', line 12 def end |
#text ⇒ Object (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.
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.
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_names ⇒ Array<String>
The names of the classes to search in.
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.
30 31 32 |
# File 'lib/pose/query.rb', line 30 def has_joins? [:joins].present? end |
#has_limit? ⇒ Boolean
Returns whether the query defines a limit on the number of results.
36 37 38 |
# File 'lib/pose/query.rb', line 36 def has_limit? [:limit].present? end |
#has_where? ⇒ Boolean
Returns whether this query contains WHERE clauses.
42 43 44 |
# File 'lib/pose/query.rb', line 42 def has_where? [:where].present? end |
#ids_requested? ⇒ Boolean
Returns whether only result ids are requested, opposed to full objects.
49 50 51 |
# File 'lib/pose/query.rb', line 49 def ids_requested? [:result_type] == :ids end |
#joins ⇒ Object
Returns the custom JOIN expressions of this query.
69 70 71 |
# File 'lib/pose/query.rb', line 69 def joins @joins ||= Array([:joins]).flatten.compact end |
#limit ⇒ Object
Returns the limitation on the number of results.
75 76 77 |
# File 'lib/pose/query.rb', line 75 def limit [:limit] end |
#query_words ⇒ Object
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 |
#where ⇒ Object
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 [ [:where] ] if [:where].size == 2 && [:where][0].is_a?(String) [:where] end |