tanker 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,8 +5,7 @@
5
5
  IndexTank is a great search indexing service, this gem tries to make
6
6
  any orm keep in sync with indextank with ease. This gem has a simple
7
7
  but powerful approach to integrating IndexTank to any orm. The gem
8
- also supports pagination using the +will_paginate/collection+. Its dead
9
- simple to integrate with Rails 3 basically you just need to
8
+ also supports pagination using the +will_paginate/collection+.
10
9
 
11
10
  There are very few things needed to integrate Tanker to any orm.
12
11
  Basically the orm instance must respond to +id+ and any attributes
@@ -41,7 +40,7 @@ in the IndexTank Dashboard
41
40
  include Tanker
42
41
 
43
42
  # define the index by supplying the index name and the fields to index
44
- tankit 'lgbt' do
43
+ tankit 'my_index' do
45
44
  indexes :title
46
45
  indexes :content
47
46
  indexes :tag_list #NOTICE this is an array of Tags! Awesome!
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.5.0
@@ -3,7 +3,7 @@ module Tanker
3
3
  module Configuration
4
4
 
5
5
  def configuration
6
- @@configuration || raise(NotConfigured, "Please configure Tanker. Set Tanker.configuration = {:url => ''}")
6
+ @@configuration || raise(NotConfigured, "Please configure Tanker. Set Tanker.configuration = {:url => ''}")
7
7
  end
8
8
 
9
9
  def configuration=(new_configuration)
@@ -0,0 +1,16 @@
1
+ require 'rails'
2
+
3
+ module Tanker
4
+ class Railtie < Rails::Railtie
5
+ config.index_tank_url = ''
6
+
7
+ config.after_initialize do
8
+ Tanker.configuration = {:url => config.index_tank_url }
9
+ end
10
+
11
+ rake_tasks do
12
+ load "tanker/tasks/tanker.rake"
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ namespace :tanker do
2
+
3
+ desc "Reindex all models"
4
+ task :reindex => :environment do
5
+ puts "reinexing all models"
6
+ load_models
7
+ Tanker::Utilities.reindex_all_models
8
+ end
9
+
10
+ desc "Clear all Index Tank indexes"
11
+ task :clear_indexes => :environment do
12
+ puts "clearing all indexes"
13
+ load_models
14
+ Tanker::Utilities.clear_all_indexes
15
+ end
16
+
17
+ def load_models
18
+ app_root = Rails.root
19
+ dirs = ["#{app_root}/app/models/"] + Dir.glob("#{app_root}/vendor/plugins/*/app/models/")
20
+
21
+ dirs.each do |base|
22
+ Dir["#{base}**/*.rb"].each do |file|
23
+ model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1')
24
+ next if model_name.nil?
25
+ model_name.camelize.constantize
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ module Tanker
2
+ module Utilities
3
+ class << self
4
+ def get_model_classes
5
+ Tanker.included_in ? Tanker.included_in : []
6
+ end
7
+
8
+ def get_available_indexes
9
+ get_model_classes.map{|model| model.index_name}.uniq.compact
10
+ end
11
+
12
+ def clear_all_indexes
13
+ get_available_indexes.each do |index_name|
14
+ begin
15
+ index = Tanker.api.get_index(index_name)
16
+
17
+ if index.exists?
18
+ puts "Deleting #{index_name} index"
19
+ index.delete_index()
20
+ end
21
+ puts "Creating #{index_name} index"
22
+ index.create_index()
23
+ puts "Waiting for the index to be ready"
24
+ while not index.running?
25
+ sleep 0.5
26
+ end
27
+ rescue => e
28
+ puts "There was an error clearing or creating the #{index_name} index: #{e.to_s}"
29
+ end
30
+ end
31
+ end
32
+
33
+ def reindex_all_models
34
+ get_model_classes.each do |klass|
35
+ puts "Indexing #{klass.to_s} model"
36
+ klass.all.each do |model_instance|
37
+ model_instance.update_tank_indexes
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
data/lib/tanker.rb CHANGED
@@ -1,11 +1,17 @@
1
1
  require "rubygems"
2
2
  require "bundler"
3
- require 'indextank_client'
4
3
 
4
+ Bundler.setup :default
5
+
6
+ require 'indextank_client'
5
7
  require 'tanker/configuration'
8
+ require 'tanker/utilities'
6
9
  require 'will_paginate/collection'
7
10
 
8
- Bundler.setup :default
11
+
12
+ if defined? Rails
13
+ require 'tanker/railtie'
14
+ end
9
15
 
10
16
  module Tanker
11
17
 
@@ -13,11 +19,20 @@ module Tanker
13
19
  class NoBlockGiven < StandardError; end
14
20
 
15
21
  autoload :Configuration, 'tanker/configuration'
16
-
17
22
  extend Configuration
18
23
 
19
24
  class << self
25
+ attr_reader :included_in
26
+
27
+ def api
28
+ @api ||= IndexTank::ApiClient.new(Tanker.configuration[:url])
29
+ end
30
+
20
31
  def included(klass)
32
+ @included_in ||= []
33
+ @included_in << klass
34
+ @included_in.uniq!
35
+
21
36
  klass.instance_variable_set('@tanker_configuration', configuration)
22
37
  klass.instance_variable_set('@tanker_indexes', [])
23
38
  klass.send :include, InstanceMethods
@@ -26,7 +41,6 @@ module Tanker
26
41
  class << klass
27
42
  define_method(:per_page) { 10 } unless respond_to?(:per_page)
28
43
  end
29
-
30
44
  end
31
45
  end
32
46
 
@@ -48,15 +62,12 @@ module Tanker
48
62
  @tanker_indexes << field
49
63
  end
50
64
 
51
- def api
52
- @api ||= IndexTank::ApiClient.new(Tanker.configuration[:url])
53
- end
54
-
55
65
  def index
56
- @index ||= api.get_index(self.index_name)
66
+ @index ||= Tanker.api.get_index(self.index_name)
57
67
  end
58
68
 
59
69
  def search_tank(query, options = {})
70
+ ids = []
60
71
  page = options.delete(:page) || 1
61
72
  per_page = options.delete(:per_page) || self.per_page
62
73
 
@@ -72,13 +83,12 @@ module Tanker
72
83
 
73
84
  results = index.search(query, options)
74
85
 
75
- unless results[:results].empty?
76
- ids = results[:results].map{|res| res[:docid].split(" ", 2)}
86
+ unless results["results"].empty?
87
+ ids = results["results"].map{|res| res["docid"].split(" ", 2)[1]}
77
88
  else
78
89
  return nil
79
90
  end
80
91
 
81
-
82
92
  @entries = WillPaginate::Collection.create(page, per_page) do |pager|
83
93
  result = self.find(ids)
84
94
  # inject the result array into the paginated collection:
@@ -86,20 +96,21 @@ module Tanker
86
96
 
87
97
  unless pager.total_entries
88
98
  # the pager didn't manage to guess the total count, do it manually
89
- pager.total_entries = results[:matches]
99
+ pager.total_entries = results["matches"]
90
100
  end
91
101
  end
92
102
  end
93
103
 
94
104
  end
95
105
 
96
- # these are the instace methods included que
106
+ # these are the instance methods included
97
107
  module InstanceMethods
98
108
 
99
109
  def tanker_indexes
100
110
  self.class.tanker_indexes
101
111
  end
102
112
 
113
+ # update a create instance from index tank
103
114
  def update_tank_indexes
104
115
  data = {}
105
116
 
@@ -114,13 +125,15 @@ module Tanker
114
125
  self.class.index.add_document(it_doc_id, data)
115
126
  end
116
127
 
128
+ # delete instance from index tank
117
129
  def delete_tank_indexes
118
130
  self.class.index.delete_document(it_doc_id)
119
131
  end
120
132
 
133
+ # create a unique index based on the model name and unique id
121
134
  def it_doc_id
122
135
  self.class.name + ' ' + self.id.to_s
123
136
  end
124
-
125
137
  end
126
138
  end
139
+
data/spec/spec_helper.rb CHANGED
@@ -9,3 +9,50 @@ require 'rspec'
9
9
  Rspec.configure do |c|
10
10
  c.mock_with :rspec
11
11
  end
12
+
13
+ Tanker.configuration = {:url => 'http://api.indextank.com'}
14
+
15
+ class Dummy
16
+
17
+ end
18
+
19
+ class Person
20
+ include Tanker
21
+
22
+ tankit 'people' do
23
+ indexes :name
24
+ indexes :last_name
25
+ end
26
+
27
+ def id
28
+ 1
29
+ end
30
+
31
+ def name
32
+ 'paco'
33
+ end
34
+
35
+ def last_name
36
+ 'viramontes'
37
+ end
38
+ end
39
+
40
+ class Dog
41
+ include Tanker
42
+
43
+ tankit 'animals' do
44
+ indexes :name
45
+ end
46
+
47
+ end
48
+
49
+ class Cat
50
+ include Tanker
51
+
52
+ tankit 'animals' do
53
+ indexes :name
54
+ end
55
+
56
+ end
57
+
58
+
data/spec/tanker_spec.rb CHANGED
@@ -1,33 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- Tanker.configuration = {:url => 'http://api.indextank.com'}
4
-
5
- class Dummy
6
-
7
- end
8
-
9
-
10
- class Person
11
- include Tanker
12
-
13
- tankit 'persons' do
14
- indexes :name
15
- indexes :last_name
16
- end
17
-
18
- def id
19
- 1
20
- end
21
-
22
- def name
23
- 'paco'
24
- end
25
-
26
- def last_name
27
- 'viramontes'
28
- end
29
- end
30
-
31
3
  describe Tanker do
32
4
 
33
5
  it 'sets configuration' do
@@ -66,7 +38,7 @@ describe Tanker do
66
38
 
67
39
  describe 'tanker instance' do
68
40
  it 'should create an api instance' do
69
- Person.api.class.should == IndexTank::ApiClient
41
+ Tanker.api.class.should == IndexTank::ApiClient
70
42
  end
71
43
 
72
44
  it 'should create a connexion to index tank' do
@@ -76,12 +48,12 @@ describe Tanker do
76
48
  it 'should be able to perform a seach query' do
77
49
  Person.index.should_receive(:search).and_return(
78
50
  {
79
- :matches => 1,
80
- :results => [{
81
- :docid => Person.new.it_doc_id,
82
- :name => 'pedro'
51
+ "matches" => 1,
52
+ "results" => [{
53
+ "docid" => Person.new.it_doc_id,
54
+ "name" => 'pedro'
83
55
  }],
84
- :search_time => 1
56
+ "search_time" => 1
85
57
  }
86
58
  )
87
59
 
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ class Dummy
4
+ include Tanker
5
+
6
+ tankit 'dummy index' do
7
+ indexes :name
8
+ end
9
+
10
+ end
11
+
12
+
13
+ describe Tanker::Utilities do
14
+
15
+ it "should get the models where Tanker module was included" do
16
+ (Tanker::Utilities.get_model_classes - [Dummy, Person, Dog, Cat]).should == []
17
+ end
18
+
19
+ it "should get the available indexes" do
20
+ Tanker::Utilities.get_available_indexes.should == ["people", "animals", "dummy index"]
21
+ end
22
+
23
+ end
data/tanker.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tanker}
8
- s.version = "0.3.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["@kidpollo"]
12
- s.date = %q{2010-10-04}
12
+ s.date = %q{2010-10-21}
13
13
  s.description = %q{IndexTank is a great search indexing service, this gem tries to make any orm keep in sync with indextank with ease}
14
14
  s.email = %q{kidpollo@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -29,8 +29,12 @@ Gem::Specification.new do |s|
29
29
  "lib/indextank_client.rb",
30
30
  "lib/tanker.rb",
31
31
  "lib/tanker/configuration.rb",
32
+ "lib/tanker/railtie.rb",
33
+ "lib/tanker/tasks/tanker.rake",
34
+ "lib/tanker/utilities.rb",
32
35
  "spec/spec_helper.rb",
33
36
  "spec/tanker_spec.rb",
37
+ "spec/utilities_spec.rb",
34
38
  "tanker.gemspec"
35
39
  ]
36
40
  s.homepage = %q{http://github.com/kidpollo/tanker}
@@ -40,7 +44,8 @@ Gem::Specification.new do |s|
40
44
  s.summary = %q{IndexTank integration to your favorite orm}
41
45
  s.test_files = [
42
46
  "spec/spec_helper.rb",
43
- "spec/tanker_spec.rb"
47
+ "spec/tanker_spec.rb",
48
+ "spec/utilities_spec.rb"
44
49
  ]
45
50
 
46
51
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 5
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "@kidpollo"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-04 00:00:00 -05:00
17
+ date: 2010-10-21 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -71,8 +71,12 @@ files:
71
71
  - lib/indextank_client.rb
72
72
  - lib/tanker.rb
73
73
  - lib/tanker/configuration.rb
74
+ - lib/tanker/railtie.rb
75
+ - lib/tanker/tasks/tanker.rake
76
+ - lib/tanker/utilities.rb
74
77
  - spec/spec_helper.rb
75
78
  - spec/tanker_spec.rb
79
+ - spec/utilities_spec.rb
76
80
  - tanker.gemspec
77
81
  has_rdoc: true
78
82
  homepage: http://github.com/kidpollo/tanker
@@ -109,3 +113,4 @@ summary: IndexTank integration to your favorite orm
109
113
  test_files:
110
114
  - spec/spec_helper.rb
111
115
  - spec/tanker_spec.rb
116
+ - spec/utilities_spec.rb