solr_collection 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rake'
4
+ gem 'rspec', '~>2'
5
+ gem 'jeweler'
6
+ gem 'will_paginate'
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ gemcutter (0.6.1)
6
+ git (1.2.5)
7
+ jeweler (1.4.0)
8
+ gemcutter (>= 0.1.0)
9
+ git (>= 1.2.5)
10
+ rubyforge (>= 2.0.0)
11
+ json_pure (1.4.6)
12
+ rake (0.8.7)
13
+ rspec (2.0.1)
14
+ rspec-core (~> 2.0.1)
15
+ rspec-expectations (~> 2.0.1)
16
+ rspec-mocks (~> 2.0.1)
17
+ rspec-core (2.0.1)
18
+ rspec-expectations (2.0.1)
19
+ diff-lcs (>= 1.1.2)
20
+ rspec-mocks (2.0.1)
21
+ rspec-core (~> 2.0.1)
22
+ rspec-expectations (~> 2.0.1)
23
+ rubyforge (2.0.4)
24
+ json_pure (>= 1.1.7)
25
+ will_paginate (2.3.14)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ jeweler
32
+ rake
33
+ rspec (~> 2)
34
+ will_paginate
data/Rakefile CHANGED
@@ -1,15 +1,16 @@
1
1
  task :default => :spec
2
- require 'spec/rake/spectask'
3
- Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new(:spec) do |t|
4
+ t.rspec_opts = '--backtrace --color'
5
+ end
4
6
 
5
7
  begin
6
- project_name = 'solr_collection'
7
8
  require 'jeweler'
8
9
  Jeweler::Tasks.new do |gem|
9
- gem.name = project_name
10
+ gem.name = 'solr_collection'
10
11
  gem.summary = "Wrapper for solr results sets then behaves/feels like will_paginate collection"
11
12
  gem.email = "grosser.michael@gmail.com"
12
- gem.homepage = "http://github.com/grosser/#{project_name}"
13
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
13
14
  gem.authors = ["Michael Grosser"]
14
15
  gem.add_dependency "will_paginate"
15
16
  end
data/Readme.md ADDED
@@ -0,0 +1,27 @@
1
+ - Wrap acts_as_solr results to behave like WillPaginate (use same helpers!)
2
+ - Make arrays behave like solr results (e.g. mock an empty result when solr is down or throws errors)
3
+
4
+ Usage
5
+ =====
6
+ - As Rails plugin ` rails plugin install git://github.com/grosser/solr_collection.git `
7
+ - As gem ` sudo gem install solr_collection `
8
+
9
+ Example:
10
+ options = {:limit=>10, :offset=>100, :facets=>{....}, .... }
11
+ results = MyModel.find_by_solr("abc", options) rescue []
12
+ results = SolrCollection.new(results, options.slice(:limit, :offset))
13
+
14
+ do_something if results.facets
15
+ do_something if results.total_entries > results.size # more pages ?
16
+ will_paginate(results)
17
+
18
+ Other features:
19
+ #facet fields can be nil or [] or {} -> use has_facet_fields? to check
20
+ puts results.facets['facet_fields']['category_id_facet'] if results.has_facet_fields?
21
+ puts results.facet_field('category_id_facet')
22
+
23
+ Author
24
+ ======
25
+ [Michael Grosser](http://grosser.it)
26
+ grosser.michael@gmail.com
27
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -3,7 +3,7 @@ require 'will_paginate/collection'
3
3
  #Proxy that delegates all calls to contained subject
4
4
  # except solr related methods in MAPPED_FIELDS
5
5
  class SolrCollection
6
- MAPPED_FIELDS = [:facets, :spellcheck, {:total=>:total_entries}]
6
+ MAPPED_FIELDS = [:facets, :spellcheck, :total_entries]
7
7
 
8
8
  def initialize(solr_result, options={})
9
9
  options = options.dup
@@ -17,21 +17,18 @@ class SolrCollection
17
17
 
18
18
  #get fields from solr_result or set them to nil
19
19
  @solr_data = {}
20
- MAPPED_FIELDS.each do |fields|
21
- if fields.is_a? Hash
22
- solr_field = fields.keys.first
23
- collection_field = fields.values.first
24
- else
25
- solr_field = collection_field = fields
26
- end
27
-
28
- @solr_data[collection_field] = if solr_result.respond_to?(solr_field)
29
- solr_result.send(solr_field)
20
+ MAPPED_FIELDS.each do |field|
21
+ @solr_data[field] = if options[field]
22
+ options[field]
23
+ elsif solr_result.respond_to?(field)
24
+ solr_result.send(field)
30
25
  else
31
26
  nil
32
27
  end
33
28
  end
34
- @solr_data[:total_entries] ||= options[:total_entries] || results.size
29
+
30
+ # always set a total
31
+ @solr_data[:total_entries] ||= (results.respond_to?(:total) ? results.total : results.size)
35
32
 
36
33
  #build will_paginate collection from given options
37
34
  options = fill_page_and_per_page(options)
@@ -41,7 +38,7 @@ class SolrCollection
41
38
  options[:per_page],
42
39
  @solr_data[:total_entries]
43
40
  )
44
- @subject.replace(results)
41
+ @subject.replace(results.to_ary)
45
42
  end
46
43
 
47
44
  def has_facet_fields?
@@ -49,6 +46,12 @@ class SolrCollection
49
46
  !!(facets and facets['facet_fields'] and not facets['facet_fields'].empty?)
50
47
  end
51
48
 
49
+ def facet_field(field)
50
+ if has_facet_fields? and value = facets['facet_fields'][field]
51
+ value.empty? ? nil : value
52
+ end
53
+ end
54
+
52
55
  private
53
56
 
54
57
  def fill_page_and_per_page(options)
@@ -5,18 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{solr_collection}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2009-11-22}
12
+ s.date = %q{2010-12-08}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
- s.extra_rdoc_files = [
15
- "README.markdown"
16
- ]
17
14
  s.files = [
18
- "README.markdown",
15
+ "Gemfile",
16
+ "Gemfile.lock",
19
17
  "Rakefile",
18
+ "Readme.md",
20
19
  "VERSION",
21
20
  "init.rb",
22
21
  "lib/solr_collection.rb",
@@ -27,11 +26,11 @@ Gem::Specification.new do |s|
27
26
  s.homepage = %q{http://github.com/grosser/solr_collection}
28
27
  s.rdoc_options = ["--charset=UTF-8"]
29
28
  s.require_paths = ["lib"]
30
- s.rubygems_version = %q{1.3.5}
29
+ s.rubygems_version = %q{1.3.6}
31
30
  s.summary = %q{Wrapper for solr results sets then behaves/feels like will_paginate collection}
32
31
  s.test_files = [
33
- "spec/spec_helper.rb",
34
- "spec/solr_collection_spec.rb"
32
+ "spec/solr_collection_spec.rb",
33
+ "spec/spec_helper.rb"
35
34
  ]
36
35
 
37
36
  if s.respond_to? :specification_version then
@@ -1,10 +1,15 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe SolrCollection do
4
- it "behaves like an array" do
5
- s = SolrCollection.new([1,2,3])
6
- s.size.should == 3
7
- s[1].should == 2
4
+ it "behaves like an array when built from solr result" do
5
+ a = []
6
+ def a.results; [1, 2, 3];end
7
+ SolrCollection.new(a)[2].should == 3
8
+ end
9
+
10
+ it "behaves like an array when built from array" do
11
+ a = [1, 2, 3]
12
+ SolrCollection.new(a)[2].should == 3
8
13
  end
9
14
 
10
15
  it "behaves like a will_paginate collection" do
@@ -19,22 +24,22 @@ describe SolrCollection do
19
24
  end
20
25
 
21
26
  describe "pages" do
22
- it "does not know page" do
23
- lambda{ SolrCollection.new([1,2,3]).page }.should raise_error
27
+ it "does not know non-will-paginate methods" do
28
+ lambda{ SolrCollection.new([1,2,3], :page => 3).page }.should raise_error
29
+ end
30
+
31
+ it "can set page" do
32
+ SolrCollection.new([1,2,3], :page=>3).current_page.should == 3
24
33
  end
25
34
 
26
35
  it "knows current_page" do
27
36
  SolrCollection.new([1,2,3]).current_page.should == 1
28
37
  end
29
38
 
30
- it "knows per_page" do
39
+ it "knows default per_page" do
31
40
  SolrCollection.new([1,2,3]).per_page.should == 10
32
41
  end
33
42
 
34
- it "can set page" do
35
- SolrCollection.new([1,2,3], :page=>3).current_page.should == 3
36
- end
37
-
38
43
  it "can set per_page" do
39
44
  SolrCollection.new([1,2,3], :per_page=>3).per_page.should == 3
40
45
  end
@@ -54,44 +59,57 @@ describe SolrCollection do
54
59
  end
55
60
  end
56
61
 
57
- describe "solr fields" do
62
+ describe "facets" do
58
63
  it "knows factes" do
59
64
  SolrCollection.new([]).facets.should == nil
60
65
  end
61
66
 
62
- it "knows spellcheck" do
63
- SolrCollection.new([]).spellcheck.should == nil
67
+ it "can set facets via options" do
68
+ SolrCollection.new([], :facets => [1]).facets.should == [1]
64
69
  end
65
70
 
66
- it "does not know non-solr-method" do
67
- lambda{ SolrCollection.new([]).fooo }.should raise_error
71
+ it "can set facets via collection" do
72
+ a = []
73
+ def a.facets; [1]; end
74
+ SolrCollection.new(a).facets.should == [1]
68
75
  end
69
76
 
70
- it "uses results as subject" do
77
+ it "prefers facets from options" do
71
78
  a = []
72
- def a.results; [1,2,3];end
73
- SolrCollection.new(a)[2].should == 3
79
+ def a.facets; [1]; end
80
+ SolrCollection.new([], :facets => [2]).facets.should == [2]
74
81
  end
82
+ end
75
83
 
76
- it "knows total_entries" do
77
- SolrCollection.new([1,2,3]).total_entries.should == 3
78
- end
84
+ it "knows spellcheck" do
85
+ SolrCollection.new([]).spellcheck.should == nil
86
+ end
79
87
 
80
- it "can get total_entries from an array" do
81
- a = [1,2,3]
82
- SolrCollection.new(a, :per_page=>2).total_pages.should == 2
83
- end
88
+ it "does not know non-solr-method" do
89
+ lambda{ SolrCollection.new([]).fooo }.should raise_error
90
+ end
84
91
 
85
- it "can get total_entries from a solr resultset" do
92
+ describe "total_entries" do
93
+ it "can get total_entries from a solr result" do
86
94
  a = []
87
95
  def a.total; 22;end
88
96
  SolrCollection.new(a).total_entries.should == 22
89
97
  end
90
98
 
91
- it "does not overwrite total from solr resultset with given total_entries" do
99
+ it "can get total_entries from array" do
100
+ a = [1,2,3]
101
+ SolrCollection.new(a).total_entries.should == 3
102
+ end
103
+
104
+ it "can get total_entries from self" do
105
+ a = SolrCollection.new([1,2,3])
106
+ SolrCollection.new(a).total_entries.should == 3
107
+ end
108
+
109
+ it "prefers total_entries from options" do
92
110
  a = []
93
111
  def a.total; 22;end
94
- SolrCollection.new(a, :total_entries=>33).total_entries.should == 22
112
+ SolrCollection.new(a, :total_entries=>33).total_entries.should == 33
95
113
  end
96
114
  end
97
115
 
@@ -112,4 +130,22 @@ describe SolrCollection do
112
130
  SolrCollection.new(a).has_facet_fields?.should == true
113
131
  end
114
132
  end
133
+
134
+ describe :facet_field do
135
+ it "gets info from facet_fields" do
136
+ SolrCollection.new([], :facets => {'facet_fields' => {'x' => {'xx' => 1}}}).facet_field('x').should == {'xx' => 1}
137
+ end
138
+
139
+ it "returns nil when field is empty" do
140
+ SolrCollection.new([], :facets => {'facet_fields' => {'x' => []}}).facet_field('x').should == nil
141
+ end
142
+
143
+ it "returns nil when field is not found" do
144
+ SolrCollection.new([], :facets => {'facet_fields' => []}).facet_field('x').should == nil
145
+ end
146
+
147
+ it "returns nil when facets are empty" do
148
+ SolrCollection.new([]).facet_field('x').should == nil
149
+ end
150
+ end
115
151
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solr_collection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 3
9
+ version: 0.1.3
5
10
  platform: ruby
6
11
  authors:
7
12
  - Michael Grosser
@@ -9,30 +14,34 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-11-22 00:00:00 +01:00
17
+ date: 2010-12-08 00:00:00 +01:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
- name: will_paginate
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
21
+ requirement: &id001 !ruby/object:Gem::Requirement
20
22
  requirements:
21
23
  - - ">="
22
24
  - !ruby/object:Gem::Version
25
+ segments:
26
+ - 0
23
27
  version: "0"
24
- version:
28
+ type: :runtime
29
+ version_requirements: *id001
30
+ prerelease: false
31
+ name: will_paginate
25
32
  description:
26
33
  email: grosser.michael@gmail.com
27
34
  executables: []
28
35
 
29
36
  extensions: []
30
37
 
31
- extra_rdoc_files:
32
- - README.markdown
38
+ extra_rdoc_files: []
39
+
33
40
  files:
34
- - README.markdown
41
+ - Gemfile
42
+ - Gemfile.lock
35
43
  - Rakefile
44
+ - Readme.md
36
45
  - VERSION
37
46
  - init.rb
38
47
  - lib/solr_collection.rb
@@ -52,21 +61,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
61
  requirements:
53
62
  - - ">="
54
63
  - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
55
66
  version: "0"
56
- version:
57
67
  required_rubygems_version: !ruby/object:Gem::Requirement
58
68
  requirements:
59
69
  - - ">="
60
70
  - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
61
73
  version: "0"
62
- version:
63
74
  requirements: []
64
75
 
65
76
  rubyforge_project:
66
- rubygems_version: 1.3.5
77
+ rubygems_version: 1.3.6
67
78
  signing_key:
68
79
  specification_version: 3
69
80
  summary: Wrapper for solr results sets then behaves/feels like will_paginate collection
70
81
  test_files:
71
- - spec/spec_helper.rb
72
82
  - spec/solr_collection_spec.rb
83
+ - spec/spec_helper.rb
data/README.markdown DELETED
@@ -1,29 +0,0 @@
1
- SolrCollection is a wrapper for acts_as_solr results sets that behaves/feels like will_paginate collection
2
-
3
- - no need to separate results from facets / spellcheck information
4
- - all pages can use the same pagination helpers
5
- - simple to mock an empty result when solr is down or throws errors
6
-
7
- Usage
8
- =====
9
- - As Rails plugin ` script/plugin install git://github.com/grosser/solr_collection.git `
10
- - As gem ` sudo gem install solr_collection `
11
-
12
- Example:
13
- options = {:limit=>10, :offset=>100, :facets=>{....}, .... }
14
- results = MyModel.find_by_solr("abc", options) rescue []
15
- results = SolrCollection.new(results, options)
16
-
17
- do_something if results.facets
18
- something_else if results.total_entries > results.size #more pages ?
19
- will_paginate(results)
20
-
21
- Other features:
22
- #facet fields can be nil or [] or {} -> use has_facet_fields? to check
23
- puts results.facets['facet_fields']['categories'] if results.has_facet_fields?
24
-
25
- Author
26
- ======
27
- [Michael Grosser](http://pragmatig.wordpress.com)
28
- grosser.michael@gmail.com
29
- Hereby placed under public domain, do what you want, just do not hold me accountable...