tanker 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/tanker.rb +48 -19
- data/spec/tanker_spec.rb +9 -15
- data/tanker.gemspec +1 -1
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/lib/tanker.rb
CHANGED
@@ -48,30 +48,39 @@ module Tanker
|
|
48
48
|
@tanker_indexes << field
|
49
49
|
end
|
50
50
|
|
51
|
-
end
|
52
|
-
|
53
|
-
# these are the instace methods included que
|
54
|
-
module InstanceMethods
|
55
|
-
|
56
|
-
def tanker_indexes
|
57
|
-
self.class.tanker_indexes
|
58
|
-
end
|
59
|
-
|
60
51
|
def api
|
61
52
|
@api ||= IndexTank::ApiClient.new(Tanker.configuration[:url])
|
62
53
|
end
|
63
54
|
|
64
55
|
def index
|
65
|
-
@index ||= api.get_index(self.
|
56
|
+
@index ||= api.get_index(self.index_name)
|
66
57
|
end
|
67
58
|
|
68
|
-
def search_tank(query,
|
59
|
+
def search_tank(query, options = {})
|
60
|
+
page = options.delete(:page) || 1
|
61
|
+
per_page = options.delete(:per_page) || self.per_page
|
62
|
+
|
63
|
+
# transform fields in query
|
64
|
+
if options.has_key? :conditions
|
65
|
+
options[:conditions].each do |field,value|
|
66
|
+
query += " #{field}:(#{value})"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
query = "__any:(#{query.to_s}) __type:#{self.name}"
|
71
|
+
options = { :start => page - 1, :len => per_page }.merge(options)
|
72
|
+
|
73
|
+
results = index.search(query, options)
|
74
|
+
|
75
|
+
unless results[:results].empty?
|
76
|
+
ids = results[:results].map{|res| res[:docid].split(" ", 2)}
|
77
|
+
else
|
78
|
+
return nil
|
79
|
+
end
|
69
80
|
|
70
|
-
results = index.search(query, :start => page, :len => per_page )
|
71
|
-
ids = results[:results].map{|res| res[:docid]}
|
72
81
|
|
73
82
|
@entries = WillPaginate::Collection.create(page, per_page) do |pager|
|
74
|
-
result = self.
|
83
|
+
result = self.find(ids)
|
75
84
|
# inject the result array into the paginated collection:
|
76
85
|
pager.replace(result)
|
77
86
|
|
@@ -81,16 +90,36 @@ module Tanker
|
|
81
90
|
end
|
82
91
|
end
|
83
92
|
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
# these are the instace methods included que
|
97
|
+
module InstanceMethods
|
98
|
+
|
99
|
+
def tanker_indexes
|
100
|
+
self.class.tanker_indexes
|
101
|
+
end
|
102
|
+
|
84
103
|
def update_tank_indexes
|
85
|
-
|
86
|
-
|
87
|
-
|
104
|
+
data = {}
|
105
|
+
|
106
|
+
tanker_indexes.each do |field|
|
107
|
+
val = self.instance_eval(field.to_s)
|
108
|
+
data[field.to_s] = val.to_s unless val.nil?
|
88
109
|
end
|
89
|
-
|
110
|
+
|
111
|
+
data[:__any] = data.values.join " . "
|
112
|
+
data[:__type] = self.class.name
|
113
|
+
|
114
|
+
self.class.index.add_document(it_doc_id, data)
|
90
115
|
end
|
91
116
|
|
92
117
|
def delete_tank_indexes
|
93
|
-
index.delete_document(
|
118
|
+
self.class.index.delete_document(it_doc_id)
|
119
|
+
end
|
120
|
+
|
121
|
+
def it_doc_id
|
122
|
+
self.class.name + ' ' + self.id.to_s
|
94
123
|
end
|
95
124
|
|
96
125
|
end
|
data/spec/tanker_spec.rb
CHANGED
@@ -61,36 +61,30 @@ describe Tanker do
|
|
61
61
|
|
62
62
|
describe 'tanker instance' do
|
63
63
|
it 'should create an api instance' do
|
64
|
-
|
65
|
-
|
66
|
-
person.api.class.should == IndexTank::ApiClient
|
64
|
+
Person.api.class.should == IndexTank::ApiClient
|
67
65
|
end
|
68
66
|
|
69
67
|
it 'should create a connexion to index tank' do
|
70
|
-
|
71
|
-
|
72
|
-
person.index.class.should == IndexTank::IndexClient
|
68
|
+
Person.index.class.should == IndexTank::IndexClient
|
73
69
|
end
|
74
70
|
|
75
71
|
it 'should be able to perform a seach query' do
|
76
|
-
|
77
|
-
|
78
|
-
person.index.should_receive(:search).and_return(
|
72
|
+
Person.index.should_receive(:search).and_return(
|
79
73
|
{
|
80
74
|
:matches => 1,
|
81
75
|
:results => [{
|
82
|
-
:docid =>
|
76
|
+
:docid => Person.new.it_doc_id,
|
83
77
|
:name => 'pedro'
|
84
78
|
}],
|
85
79
|
:search_time => 1
|
86
80
|
}
|
87
81
|
)
|
88
82
|
|
89
|
-
|
90
|
-
[
|
83
|
+
Person.should_receive(:find).and_return(
|
84
|
+
[Person.new]
|
91
85
|
)
|
92
86
|
|
93
|
-
collection =
|
87
|
+
collection = Person.search_tank('hey!')
|
94
88
|
collection.class.should == WillPaginate::Collection
|
95
89
|
collection.total_entries.should == 1
|
96
90
|
collection.total_pages.should == 1
|
@@ -101,7 +95,7 @@ describe Tanker do
|
|
101
95
|
it 'should be able to update the index' do
|
102
96
|
person = Person.new
|
103
97
|
|
104
|
-
|
98
|
+
Person.index.should_receive(:add_document)
|
105
99
|
|
106
100
|
person.update_tank_indexes
|
107
101
|
end
|
@@ -109,7 +103,7 @@ describe Tanker do
|
|
109
103
|
it 'should be able to delete de document from the index' do
|
110
104
|
person = Person.new
|
111
105
|
|
112
|
-
|
106
|
+
Person.index.should_receive(:delete_document)
|
113
107
|
|
114
108
|
person.delete_tank_indexes
|
115
109
|
end
|
data/tanker.gemspec
CHANGED