rsolr-nokogiri 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2008-2010 Matt Mitchell
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,25 @@
1
+ =rsolr-nokogiri
2
+
3
+ == Installation:
4
+ gem sources -a http://gemcutter.org
5
+ sudo gem install rsolr-nokogiri
6
+
7
+ == Example:
8
+ require 'rubygems'
9
+ require 'rsolr-nokogiri'
10
+ solr = RSolr.connect :url=>'http://solrserver.com'
11
+ solr.message.use_nokogiri!
12
+ puts solr.message.add(:id => 1)
13
+
14
+ == Note on Patches/Pull Requests
15
+
16
+ * Fork the project.
17
+ * Make your feature addition or bug fix.
18
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
19
+ * Commit, do not mess with rakefile, version, or history
20
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
21
+ * Send me a pull request. Bonus points for topic branches.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2010 Matt Mitchell. See LICENSE for details.
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rsolr-nokogiri"
8
+ gem.summary = %Q{Gives RSolr the power to use Nokogiri instead of Builder}
9
+ gem.description = %Q{Gives RSolr the power to use Nokogiri instead of Builder... it's faster.}
10
+ gem.email = "goodieboy@gmail.com"
11
+ gem.homepage = "http://github.com/mwmitchell/rsolr-nokogiri"
12
+ gem.authors = ["Matt Mitchell"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "rsolr", ">= 0.12.1"
15
+
16
+ gem.files = FileList['lib/**/*.rb', 'LICENSE', 'README.rdoc', 'VERSION']
17
+ gem.test_files = ['spec/*', 'Rakefile']
18
+
19
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
+ end
21
+ # Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
+ end
25
+
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
33
+ spec.libs << 'lib' << 'spec'
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ task :spec => :check_dependencies
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "rsolr-nokogiri #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,108 @@
1
+ require 'rubygems'
2
+ require 'rsolr'
3
+
4
+ module RSolr::Nokogiri
5
+
6
+ module Nokogiriable
7
+
8
+ def use_nokogiri!
9
+ self.extend RSolr::Nokogiri::Generator
10
+ end
11
+
12
+ end
13
+
14
+ RSolr::Message::Generator.class_eval do
15
+ include Nokogiriable
16
+ end
17
+
18
+ module Generator
19
+
20
+ def build &block
21
+ require 'nokogiri'
22
+ b = Nokogiri::XML::Builder.new(:encoding => 'UTF-8')
23
+ yield b if block_given?
24
+ b.to_xml
25
+ end
26
+
27
+ # generates "add" xml for updating solr
28
+ # "data" can be a hash or an array of hashes.
29
+ # - each hash should be a simple key=>value pair representing a solr doc.
30
+ # If a value is an array, multiple fields will be created.
31
+ #
32
+ # "add_attrs" can be a hash for setting the add xml element attributes.
33
+ #
34
+ # This method can also accept a block.
35
+ # The value yielded to the block is a Message::Document; for each solr doc in "data".
36
+ # You can set xml element attributes for each "doc" element or individual "field" elements.
37
+ #
38
+ # For example:
39
+ #
40
+ # solr.add({:id=>1, :nickname=>'Tim'}, {:boost=>5.0, :commitWithin=>1.0}) do |doc_msg|
41
+ # doc_msg.attrs[:boost] = 10.00 # boost the document
42
+ # nickname = doc_msg.field_by_name(:nickname)
43
+ # nickname.attrs[:boost] = 20 if nickname.value=='Tim' # boost a field
44
+ # end
45
+ #
46
+ # would result in an add element having the attributes boost="10.0"
47
+ # and a commitWithin="1.0".
48
+ # Each doc element would have a boost="10.0".
49
+ # The "nickname" field would have a boost="20.0"
50
+ # if the doc had a "nickname" field with the value of "Tim".
51
+ #
52
+ def add data, add_attrs={}, &block
53
+ data = [data] unless data.is_a?(Array)
54
+ build do |xml|
55
+ xml.add(add_attrs) do |add_node|
56
+ data.each do |doc|
57
+ doc = RSolr::Message::Document.new(doc) if doc.respond_to?(:each_pair)
58
+ yield doc if block_given?
59
+ add_node.doc_(doc.attrs) do |doc_node|
60
+ doc.fields.each do |field_obj|
61
+ doc_node.field field_obj.value, field_obj.attrs
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # generates a <commit/> message
70
+ def commit(opts={})
71
+ build {|xml| xml.commit opts}
72
+ end
73
+
74
+ # generates a <optimize/> message
75
+ def optimize(opts={})
76
+ build {|xml| xml.optimize opts}
77
+ end
78
+
79
+ # generates a <rollback/> message
80
+ def rollback opts={}
81
+ build {|xml| xml.rollback opts}
82
+ end
83
+
84
+ # generates a <delete><id>ID</id></delete> message
85
+ # "ids" can be a single value or array of values
86
+ def delete_by_id(ids)
87
+ ids = [ids] unless ids.is_a?(Array)
88
+ build do |xml|
89
+ xml.delete do |delete_node|
90
+ ids.each { |id| delete_node.id_(id) }
91
+ end
92
+ end
93
+ end
94
+
95
+ # generates a <delete><query>ID</query></delete> message
96
+ # "queries" can be a single value or an array of values
97
+ def delete_by_query(queries)
98
+ queries = [queries] unless queries.is_a?(Array)
99
+ build do |xml|
100
+ xml.delete do |delete_node|
101
+ queries.each { |query| delete_node.query query }
102
+ end
103
+ end
104
+ end
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe RSolr::Nokogiri do
4
+
5
+ module NokogiriEnabledHelpers
6
+
7
+ def self.included base
8
+ base.let(:rsolr){
9
+ rsolr = RSolr.connect
10
+ rsolr.message.use_nokogiri!
11
+ rsolr
12
+ }
13
+ end
14
+
15
+ end
16
+
17
+ it 'should use Builder unless use_nokogiri! is called' do
18
+ rsolr = RSolr.connect
19
+ rsolr.message.should_not be_a(RSolr::Nokogiri::Generator)
20
+ end
21
+
22
+ it 'should use Nokogiri after use_nokogiri! is called' do
23
+ rsolr = RSolr.connect
24
+ rsolr.message.use_nokogiri!
25
+ rsolr.message.should be_a(RSolr::Nokogiri::Generator)
26
+ end
27
+
28
+ context 'RSolr::Client' do
29
+
30
+ include NokogiriEnabledHelpers
31
+
32
+ it 'should be using Nokogiri' do
33
+ rsolr.message.is_a? RSolr::Nokogiri::Generator
34
+ end
35
+
36
+ it 'should forward calls to message/generator' do
37
+ rsolr.should_receive(:update).
38
+ with("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<optimize/>\n")
39
+ rsolr.optimize
40
+ end
41
+
42
+ end
43
+
44
+ context "RSolr::Nokogiri::Generator" do
45
+
46
+ include NokogiriEnabledHelpers
47
+
48
+ it 'should generator proper add XML' do
49
+ expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<add>\n <doc>\n <field name=\"id\">1</field>\n </doc>\n</add>\n"
50
+ rsolr.message.add(:id=>1).should == expected
51
+ end
52
+
53
+ it 'should generator proper delete XML - single id' do
54
+ expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<delete>\n <id>1</id>\n</delete>\n"
55
+ rsolr.message.delete_by_id(1).should == expected
56
+ end
57
+
58
+ it 'should generator proper delete XML - multiple ids' do
59
+ expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<delete>\n <id>1</id>\n <id>2</id>\n <id>3</id>\n</delete>\n"
60
+ rsolr.message.delete_by_id([1,2,3]).should == expected
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rsolr-nokogiri'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsolr-nokogiri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Mitchell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-05 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rsolr
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.1
34
+ version:
35
+ description: Gives RSolr the power to use Nokogiri instead of Builder... it's faster.
36
+ email: goodieboy@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - VERSION
48
+ - lib/rsolr-nokogiri.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/mwmitchell/rsolr-nokogiri
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Gives RSolr the power to use Nokogiri instead of Builder
77
+ test_files:
78
+ - spec/rsolr-nokogiri_spec.rb
79
+ - spec/spec.opts
80
+ - spec/spec_helper.rb
81
+ - Rakefile