Class: Texticle::FullTextIndex
- Inherits:
-
Object
- Object
- Texticle::FullTextIndex
show all
- Defined in:
- lib/texticle/full_text_index.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, dictionary, model_class, &block) ⇒ FullTextIndex
Returns a new instance of FullTextIndex.
5
6
7
8
9
10
11
12
|
# File 'lib/texticle/full_text_index.rb', line 5
def initialize name, dictionary, model_class, &block
@name = name
@dictionary = dictionary
@model_class = model_class
@index_columns = {}
@string = nil
instance_eval(&block)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
49
50
51
52
|
# File 'lib/texticle/full_text_index.rb', line 49
def method_missing name, *args
weight = args.shift || 'none'
(index_columns[weight] ||= []) << name.to_s
end
|
Instance Attribute Details
#index_columns ⇒ Object
Returns the value of attribute index_columns.
3
4
5
|
# File 'lib/texticle/full_text_index.rb', line 3
def index_columns
@index_columns
end
|
Instance Method Details
#create ⇒ Object
14
15
16
|
# File 'lib/texticle/full_text_index.rb', line 14
def create
@model_class.connection.execute create_sql
end
|
#create_sql ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/texticle/full_text_index.rb', line 22
def create_sql
<<-eosql
CREATE index #{@name}
ON #{@model_class.table_name}
USING gin((#{to_s}))
eosql
end
|
#destroy ⇒ Object
18
19
20
|
# File 'lib/texticle/full_text_index.rb', line 18
def destroy
@model_class.connection.execute destroy_sql
end
|
#destroy_sql ⇒ Object
30
31
32
|
# File 'lib/texticle/full_text_index.rb', line 30
def destroy_sql
"DROP index IF EXISTS #{@name}"
end
|
#to_s ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/texticle/full_text_index.rb', line 34
def to_s
return @string if @string
vectors = []
@index_columns.sort_by { |k,v| k }.each do |weight, columns|
c = columns.map { |x| "coalesce(#{@model_class.table_name}.#{x}, '')" }
if weight == 'none'
vectors << "to_tsvector('#{@dictionary}', #{c.join(" || ' ' || ")})"
else
vectors <<
"setweight(to_tsvector('#{@dictionary}', #{c.join(" || ' ' || ")}), '#{weight}')"
end
end
@string = vectors.join(" || ' ' || ")
end
|