solr_collection 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/solr_collection.rb +26 -19
- data/solr_collection.gemspec +4 -4
- data/spec/solr_collection_spec.rb +32 -1
- data/spec/spec_helper.rb +1 -1
- metadata +13 -6
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            0.1. | 
| 1 | 
            +
            0.1.4
         | 
    
        data/lib/solr_collection.rb
    CHANGED
    
    | @@ -3,41 +3,44 @@ 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 | 
            -
               | 
| 6 | 
            +
              SOLR_FIELDS = [:facets, :spellcheck]
         | 
| 7 7 |  | 
| 8 | 
            -
              def initialize( | 
| 8 | 
            +
              def initialize(collection, options={})
         | 
| 9 9 | 
             
                options = options.dup
         | 
| 10 10 |  | 
| 11 | 
            -
                #solr result  | 
| 12 | 
            -
                results = if solr_result.respond_to?(:results)
         | 
| 13 | 
            -
                  solr_result.results
         | 
| 14 | 
            -
                else
         | 
| 15 | 
            -
                  solr_result
         | 
| 16 | 
            -
                end
         | 
| 17 | 
            -
             | 
| 18 | 
            -
                #get fields from solr_result or set them to nil
         | 
| 11 | 
            +
                # get fields from solr result-set or set them to nil
         | 
| 19 12 | 
             
                @solr_data = {}
         | 
| 20 | 
            -
                 | 
| 13 | 
            +
                SOLR_FIELDS.each do |field|
         | 
| 21 14 | 
             
                  @solr_data[field] = if options[field]
         | 
| 22 15 | 
             
                    options[field]
         | 
| 23 | 
            -
                  elsif  | 
| 24 | 
            -
                     | 
| 16 | 
            +
                  elsif collection.respond_to?(field)
         | 
| 17 | 
            +
                    collection.send(field)
         | 
| 25 18 | 
             
                  else
         | 
| 26 19 | 
             
                    nil
         | 
| 27 20 | 
             
                  end
         | 
| 28 21 | 
             
                end
         | 
| 29 22 |  | 
| 30 | 
            -
                #  | 
| 31 | 
            -
                 | 
| 32 | 
            -
             | 
| 33 | 
            -
                 | 
| 23 | 
            +
                # copy or generate pagination information
         | 
| 24 | 
            +
                options[:page] ||= (collection.respond_to?(:current_page) ? collection.current_page : nil)
         | 
| 25 | 
            +
                options[:per_page] ||= (collection.respond_to?(:per_page) ? collection.per_page : nil)
         | 
| 26 | 
            +
                options[:total_entries] ||= if collection.respond_to?(:total)
         | 
| 27 | 
            +
                  collection.total
         | 
| 28 | 
            +
                elsif collection.respond_to?(:total_entries)
         | 
| 29 | 
            +
                  collection.total_entries
         | 
| 30 | 
            +
                else
         | 
| 31 | 
            +
                  collection.size
         | 
| 32 | 
            +
                end
         | 
| 34 33 | 
             
                options = fill_page_and_per_page(options)
         | 
| 35 34 |  | 
| 35 | 
            +
                # build paginate-able object
         | 
| 36 36 | 
             
                @subject = WillPaginate::Collection.new(
         | 
| 37 37 | 
             
                  options[:page],
         | 
| 38 38 | 
             
                  options[:per_page],
         | 
| 39 | 
            -
                   | 
| 39 | 
            +
                  options[:total_entries]
         | 
| 40 40 | 
             
                )
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                # solr result-set has results in an array called results
         | 
| 43 | 
            +
                results = (collection.respond_to?(:results) ? collection.results : collection)
         | 
| 41 44 | 
             
                @subject.replace(results.to_ary)
         | 
| 42 45 | 
             
              end
         | 
| 43 46 |  | 
| @@ -52,6 +55,10 @@ class SolrCollection | |
| 52 55 | 
             
                end
         | 
| 53 56 | 
             
              end
         | 
| 54 57 |  | 
| 58 | 
            +
              def respond_to?(method)
         | 
| 59 | 
            +
                @solr_data.key?(method) or @subject.respond_to?(method) or super
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 55 62 | 
             
              private
         | 
| 56 63 |  | 
| 57 64 | 
             
              def fill_page_and_per_page(options)
         | 
| @@ -69,7 +76,7 @@ class SolrCollection | |
| 69 76 | 
             
              def method_missing(method, *args, &block)
         | 
| 70 77 | 
             
                method = method.to_sym
         | 
| 71 78 | 
             
                if @solr_data.key? method
         | 
| 72 | 
            -
                  @solr_data[method]
         | 
| 79 | 
            +
                  @solr_data[method] or (@subject.send(method, *args, &block) rescue nil)
         | 
| 73 80 | 
             
                else
         | 
| 74 81 | 
             
                  @subject.send(method, *args, &block)
         | 
| 75 82 | 
             
                end
         | 
    
        data/solr_collection.gemspec
    CHANGED
    
    | @@ -5,11 +5,11 @@ | |
| 5 5 |  | 
| 6 6 | 
             
            Gem::Specification.new do |s|
         | 
| 7 7 | 
             
              s.name = %q{solr_collection}
         | 
| 8 | 
            -
              s.version = "0.1. | 
| 8 | 
            +
              s.version = "0.1.4"
         | 
| 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{2010-12- | 
| 12 | 
            +
              s.date = %q{2010-12-24}
         | 
| 13 13 | 
             
              s.email = %q{grosser.michael@gmail.com}
         | 
| 14 14 | 
             
              s.files = [
         | 
| 15 15 | 
             
                "Gemfile",
         | 
| @@ -26,7 +26,7 @@ Gem::Specification.new do |s| | |
| 26 26 | 
             
              s.homepage = %q{http://github.com/grosser/solr_collection}
         | 
| 27 27 | 
             
              s.rdoc_options = ["--charset=UTF-8"]
         | 
| 28 28 | 
             
              s.require_paths = ["lib"]
         | 
| 29 | 
            -
              s.rubygems_version = %q{1.3. | 
| 29 | 
            +
              s.rubygems_version = %q{1.3.7}
         | 
| 30 30 | 
             
              s.summary = %q{Wrapper for solr results sets then behaves/feels like will_paginate collection}
         | 
| 31 31 | 
             
              s.test_files = [
         | 
| 32 32 | 
             
                "spec/solr_collection_spec.rb",
         | 
| @@ -37,7 +37,7 @@ Gem::Specification.new do |s| | |
| 37 37 | 
             
                current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
         | 
| 38 38 | 
             
                s.specification_version = 3
         | 
| 39 39 |  | 
| 40 | 
            -
                if Gem::Version.new(Gem:: | 
| 40 | 
            +
                if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
         | 
| 41 41 | 
             
                  s.add_runtime_dependency(%q<will_paginate>, [">= 0"])
         | 
| 42 42 | 
             
                else
         | 
| 43 43 | 
             
                  s.add_dependency(%q<will_paginate>, [">= 0"])
         | 
| @@ -36,13 +36,26 @@ describe SolrCollection do | |
| 36 36 | 
             
                  SolrCollection.new([1,2,3]).current_page.should == 1
         | 
| 37 37 | 
             
                end
         | 
| 38 38 |  | 
| 39 | 
            -
                it " | 
| 39 | 
            +
                it "has a default for per_page" do
         | 
| 40 40 | 
             
                  SolrCollection.new([1,2,3]).per_page.should == 10
         | 
| 41 41 | 
             
                end
         | 
| 42 42 |  | 
| 43 43 | 
             
                it "can set per_page" do
         | 
| 44 44 | 
             
                  SolrCollection.new([1,2,3], :per_page=>3).per_page.should == 3
         | 
| 45 45 | 
             
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                it "copies pagination information" do
         | 
| 48 | 
            +
                  page = 2
         | 
| 49 | 
            +
                  per_page = 3
         | 
| 50 | 
            +
                  total_entries = 7
         | 
| 51 | 
            +
                  collection = WillPaginate::Collection.new(page, per_page, total_entries)
         | 
| 52 | 
            +
                  collection = collection.replace([1,2,3,4])
         | 
| 53 | 
            +
                  solr = SolrCollection.new(collection)
         | 
| 54 | 
            +
                  solr.per_page.should == per_page
         | 
| 55 | 
            +
                  solr.current_page.should.should == page
         | 
| 56 | 
            +
                  solr.total_pages.should.should == 3
         | 
| 57 | 
            +
                  solr.total_entries.should.should == total_entries
         | 
| 58 | 
            +
                end
         | 
| 46 59 | 
             
              end
         | 
| 47 60 |  | 
| 48 61 | 
             
              describe "limit and offset" do
         | 
| @@ -148,4 +161,22 @@ describe SolrCollection do | |
| 148 161 | 
             
                  SolrCollection.new([]).facet_field('x').should == nil
         | 
| 149 162 | 
             
                end
         | 
| 150 163 | 
             
              end
         | 
| 164 | 
            +
             | 
| 165 | 
            +
              describe :respond_to do
         | 
| 166 | 
            +
                it "responds to own methods" do
         | 
| 167 | 
            +
                  SolrCollection.new([]).respond_to?(:has_facet_fields?).should == true
         | 
| 168 | 
            +
                end
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                it "responds to subjects methods" do
         | 
| 171 | 
            +
                  SolrCollection.new([]).respond_to?(:total_pages).should == true
         | 
| 172 | 
            +
                end
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                it "responds to missing methods" do
         | 
| 175 | 
            +
                  SolrCollection.new([]).respond_to?(:spellcheck).should == true
         | 
| 176 | 
            +
                end
         | 
| 177 | 
            +
             | 
| 178 | 
            +
                it "does not respond to unknown methods" do
         | 
| 179 | 
            +
                  SolrCollection.new([]).respond_to?(:foo).should == false
         | 
| 180 | 
            +
                end
         | 
| 181 | 
            +
              end
         | 
| 151 182 | 
             
            end
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,12 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: solr_collection
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              hash: 19
         | 
| 4 5 | 
             
              prerelease: false
         | 
| 5 6 | 
             
              segments: 
         | 
| 6 7 | 
             
              - 0
         | 
| 7 8 | 
             
              - 1
         | 
| 8 | 
            -
              -  | 
| 9 | 
            -
              version: 0.1. | 
| 9 | 
            +
              - 4
         | 
| 10 | 
            +
              version: 0.1.4
         | 
| 10 11 | 
             
            platform: ruby
         | 
| 11 12 | 
             
            authors: 
         | 
| 12 13 | 
             
            - Michael Grosser
         | 
| @@ -14,21 +15,23 @@ autorequire: | |
| 14 15 | 
             
            bindir: bin
         | 
| 15 16 | 
             
            cert_chain: []
         | 
| 16 17 |  | 
| 17 | 
            -
            date: 2010-12- | 
| 18 | 
            +
            date: 2010-12-24 00:00:00 +01:00
         | 
| 18 19 | 
             
            default_executable: 
         | 
| 19 20 | 
             
            dependencies: 
         | 
| 20 21 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 21 22 | 
             
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 23 | 
            +
                none: false
         | 
| 22 24 | 
             
                requirements: 
         | 
| 23 25 | 
             
                - - ">="
         | 
| 24 26 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 27 | 
            +
                    hash: 3
         | 
| 25 28 | 
             
                    segments: 
         | 
| 26 29 | 
             
                    - 0
         | 
| 27 30 | 
             
                    version: "0"
         | 
| 31 | 
            +
              name: will_paginate
         | 
| 32 | 
            +
              prerelease: false
         | 
| 28 33 | 
             
              type: :runtime
         | 
| 29 34 | 
             
              version_requirements: *id001
         | 
| 30 | 
            -
              prerelease: false
         | 
| 31 | 
            -
              name: will_paginate
         | 
| 32 35 | 
             
            description: 
         | 
| 33 36 | 
             
            email: grosser.michael@gmail.com
         | 
| 34 37 | 
             
            executables: []
         | 
| @@ -58,23 +61,27 @@ rdoc_options: | |
| 58 61 | 
             
            require_paths: 
         | 
| 59 62 | 
             
            - lib
         | 
| 60 63 | 
             
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 64 | 
            +
              none: false
         | 
| 61 65 | 
             
              requirements: 
         | 
| 62 66 | 
             
              - - ">="
         | 
| 63 67 | 
             
                - !ruby/object:Gem::Version 
         | 
| 68 | 
            +
                  hash: 3
         | 
| 64 69 | 
             
                  segments: 
         | 
| 65 70 | 
             
                  - 0
         | 
| 66 71 | 
             
                  version: "0"
         | 
| 67 72 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 73 | 
            +
              none: false
         | 
| 68 74 | 
             
              requirements: 
         | 
| 69 75 | 
             
              - - ">="
         | 
| 70 76 | 
             
                - !ruby/object:Gem::Version 
         | 
| 77 | 
            +
                  hash: 3
         | 
| 71 78 | 
             
                  segments: 
         | 
| 72 79 | 
             
                  - 0
         | 
| 73 80 | 
             
                  version: "0"
         | 
| 74 81 | 
             
            requirements: []
         | 
| 75 82 |  | 
| 76 83 | 
             
            rubyforge_project: 
         | 
| 77 | 
            -
            rubygems_version: 1.3. | 
| 84 | 
            +
            rubygems_version: 1.3.7
         | 
| 78 85 | 
             
            signing_key: 
         | 
| 79 86 | 
             
            specification_version: 3
         | 
| 80 87 | 
             
            summary: Wrapper for solr results sets then behaves/feels like will_paginate collection
         |