delayed_sunspot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv*
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ruby-head
6
+ - jruby-19mode
7
+ - jruby-head
8
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delayed_sunspot.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Anthony Smith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # DelayedSunspot [![endorse](http://api.coderwall.com/anthonator/endorsecount.png)](http://coderwall.com/anthonator)
2
+
3
+ Adds support for indexing objects asynchronously using [Sunspot](https://github.com/sunspot/sunspot) and [Delayed Job](https://github.com/collectiveidea/delayed_job).
4
+
5
+ [![Build Status](https://secure.travis-ci.org/anthonator/delayed_sunspot.png)](http://travis-ci.org/anthonator/delayed_sunspot)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'delayed_sunspot'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install delayed_sunspot
20
+
21
+ ## Usage
22
+
23
+ You will need to configure Sunspot to use delayed_sunspot's session proxy in order to push Sunspot commands through Delayed Job.
24
+
25
+ ```ruby
26
+ Sunspot.session = Sunspot::SessionProxy::DelayedJobSessionProxy.new(Sunspot.session)
27
+ ```
28
+
29
+ If you're using Rails put the above line of code in an initializer.
30
+
31
+ ## ZOMG! WHY AREN'T MY OBJECTS BEING INDEXED?!
32
+
33
+ If you're using the sunspot_rails gem you might have noticed that the objects you've been indexing aren't coming back in your search results. This is due to the sunspot_rails gem being configured to autocommit indexes through a controller hook. This functionality doesn't exist when indexing outside of a controller.
34
+
35
+ You can enable autocommit by putting the following code in your solrconfig.xml under the updateHandler section:
36
+
37
+ ```xml
38
+ <autoCommit>
39
+ <maxDocs>10000</maxDocs> <!-- maximum uncommited docs before autocommit triggered -->
40
+ <maxTime>15000</maxTime> <!-- maximum time (in MS) after adding a doc before an autocommit is triggered -->
41
+ </autoCommit>
42
+ ```
43
+
44
+ To find out more about autocommits [read the Solr documentation](http://wiki.apache.org/solr/SolrConfigXml#Update_Handler_Section)
45
+
46
+ If you've already configured Solr for autocommits you're good to go.
47
+
48
+ ## Asynchronous Commands
49
+
50
+ batch
51
+ commit
52
+ commit_if_delete_dirty
53
+ commit_if_dirty
54
+ index
55
+ index!
56
+ remove
57
+ remove!
58
+ remove_all
59
+ remove_all!
60
+ remove_by_id
61
+ remove_by_id!
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'delayed_sunspot/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "delayed_sunspot"
8
+ gem.version = DelayedSunspot::VERSION
9
+ gem.authors = ["Anthony Smith"]
10
+ gem.email = ["anthony@sticksnleaves.com"]
11
+ gem.description = %q{Delayed Job support for Sunspot}
12
+ gem.summary = %q{Toss your Sunspot Solr commits into a background job.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'sunspot'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'sunspot_matchers'
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'sunspot'
2
+ require 'delayed_sunspot/delayed_job/sunspot_job'
3
+ require 'delayed_sunspot/sunspot/session_proxy/delayed_job_session_proxy'
4
+ require 'delayed_sunspot/sunspot/configuration'
5
+ require 'delayed_sunspot/version'
@@ -0,0 +1,24 @@
1
+ module DelayedSunspot
2
+ module DelayedJob
3
+ class SunspotJob
4
+ attr_reader :session, :proxy, :method
5
+
6
+ def initialize(proxy, method, *args, &block)
7
+ @proxy = proxy
8
+ @session = proxy.session
9
+ @method = method
10
+ @args = args
11
+ end
12
+
13
+ def perform
14
+ rebuild_config
15
+ @args ? @session.send(method, *@args) : @session.send(method)
16
+ end
17
+
18
+ private
19
+ def rebuild_config
20
+ @session.instance_variable_set("@config", DelayedSunspot::Sunspot::Configuration.new(session.config))
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module DelayedSunspot
2
+ module Sunspot
3
+ class Configuration
4
+ def initialize(config)
5
+ @config = config
6
+ singleton = (class <<self; self; end)
7
+ @properties = @config.instance_variable_get("@properties") || {}
8
+ @properties.keys.each do |key|
9
+ singleton.module_eval do
10
+ define_method key do
11
+ if @properties[key].is_a?(LightConfig::Configuration)
12
+ self.class.new(@properties[key])
13
+ else
14
+ @properties[key]
15
+ end
16
+ end
17
+ define_method "#{key}=" do |value|
18
+ @properties[key] = value
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,72 @@
1
+ module Sunspot
2
+ module SessionProxy
3
+ class DelayedJobSessionProxy < ::Sunspot::SessionProxy::AbstractSessionProxy
4
+ attr_reader :session
5
+
6
+ delegate :config, :delete_dirty?, :dirty?, :more_like_this, :new_more_like_this, :new_search,
7
+ :optimize, :search,
8
+ :to => :session
9
+
10
+ def initialize(session)
11
+ @session = session
12
+ end
13
+
14
+ def batch(&block)
15
+ enqueue(:batch, &block)
16
+ end
17
+
18
+ def commit
19
+ enqueue(:commit)
20
+ end
21
+
22
+ def commit_if_delete_dirty
23
+ commit if @session.delete_dirty?
24
+ end
25
+
26
+ def commit_if_dirty
27
+ commit if @session.dirty?
28
+ end
29
+
30
+ def index(*objects)
31
+ enqueue(:index, *objects)
32
+ end
33
+
34
+ def index!(*objects)
35
+ index(*objects)
36
+ commit
37
+ end
38
+
39
+ def remove(*objects, &block)
40
+ enqueue(:remove, *objects, &block)
41
+ end
42
+
43
+ def remove!(*objects)
44
+ remove(*objects)
45
+ commit
46
+ end
47
+
48
+ def remove_all(*classes)
49
+ enqueue(:remove_all, *classes)
50
+ end
51
+
52
+ def remove_all!(*classes)
53
+ remove_all(*classes)
54
+ commit
55
+ end
56
+
57
+ def remove_by_id(clazz, id)
58
+ enqueue(:remove_by_id, clazz, id)
59
+ end
60
+
61
+ def remove_by_id!(clazz, id)
62
+ remove_by_id(clazz, id)
63
+ commit
64
+ end
65
+
66
+ private
67
+ def enqueue(method, *args, &block)
68
+ Delayed::Job.enqueue(DelayedSunspot::DelayedJob::SunspotJob.new(self, method, *args, &block))
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module DelayedSunspot
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sunspot::SessionProxy::DelayedJobSessionProxy do
4
+ before(:each) do
5
+ @original_session = Sunspot::Session.new
6
+ @proxy = Sunspot::SessionProxy::DelayedJobSessionProxy.new(@original_session)
7
+ end
8
+
9
+ describe :proxy do
10
+ it "should wrap original session" do
11
+ @proxy.session.should == @original_session
12
+ end
13
+ end
14
+
15
+ describe "delegated method" do
16
+ [:config, :delete_dirty?, :dirty?, :more_like_this, :new_more_like_this, :new_search, :optimize, :search].each do |method|
17
+ it "#{method} should be called on the original session" do
18
+ args = Array.new(Sunspot::Session.instance_method(method).arity.abs) do
19
+ stub('arg')
20
+ end
21
+ @proxy.session.should_receive(method).with(*args)
22
+ @proxy.send(method, *args)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "queued method" do
28
+ [:batch, :commit, :index, :index!, :remove, :remove!, :remove_all, :remove_all!, :remove_by_id, :remove_by_id!].each do |method|
29
+ it "#{method} should queue a job" do
30
+ args = Array.new(Sunspot::Session.instance_method(method).arity.abs) do
31
+ stub('arg')
32
+ end
33
+ @proxy.send(method, *args).should == true
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ require 'sunspot'
2
+ require 'sunspot_matchers'
3
+ require 'delayed_sunspot/sunspot/session_proxy/delayed_job_session_proxy'
4
+ require 'delayed_sunspot/delayed_job/sunspot_job'
5
+
6
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
7
+
8
+ RSpec.configure do |config|
9
+ config.before :suite do
10
+ Sunspot.session = SunspotMatchers::SunspotSessionSpy.new(Sunspot.session)
11
+ end
12
+ config.include SunspotMatchers
13
+ end
@@ -0,0 +1,7 @@
1
+ module Delayed
2
+ class Job
3
+ def self.enqueue(objects)
4
+ true
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_sunspot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anthony Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sunspot
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sunspot_matchers
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Delayed Job support for Sunspot
79
+ email:
80
+ - anthony@sticksnleaves.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - delayed_sunspot.gemspec
92
+ - lib/delayed_sunspot.rb
93
+ - lib/delayed_sunspot/delayed_job/sunspot_job.rb
94
+ - lib/delayed_sunspot/sunspot/configuration.rb
95
+ - lib/delayed_sunspot/sunspot/session_proxy/delayed_job_session_proxy.rb
96
+ - lib/delayed_sunspot/version.rb
97
+ - spec/delayed_sunspot_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/support/delayed_job_stub.rb
100
+ homepage: ''
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.23
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Toss your Sunspot Solr commits into a background job.
124
+ test_files:
125
+ - spec/delayed_sunspot_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/delayed_job_stub.rb