Sha256: 3b361f7506644228081d93ff7d8a7100294716b8c4adcb833b17d883864b03a9

Contents?: true

Size: 2 KB

Versions: 20

Compression:

Stored size: 2 KB

Contents

module Sunspot #:nodoc:
  module MongoMapper #:nodoc:
    # 
    # This module provides Sunspot Adapter implementations for MongoMapper
    # models.
    #
    module Adapters
      class MongoMapperInstanceAdapter < Sunspot::Adapters::InstanceAdapter
        # 
        # Return the primary key for the adapted instance
        #
        # ==== Returns
        # 
        # Integer:: Database ID of model
        #
        def id
          @instance.id
        end
      end

      class MongoMapperDataAccessor < Sunspot::Adapters::DataAccessor
        # options for the find
        attr_accessor :include, :select

        #
        # Set the fields to select from the database. This will be passed
        # to MongoMapper.
        #
        # ==== Parameters
        #
        # value<Mixed>:: String of comma-separated columns or array of columns
        #
        def select=(value)
          value = value.join(', ') if value.respond_to?(:join)
          @select = value
        end
        
        # 
        # Get one MongoMapper instance out of the database by ID
        #
        # ==== Parameters
        #
        # id<String>:: Database ID of model to retreive
        #
        # ==== Returns
        #
        # MongoMapper::Base:: MongoMapper model
        # 
        def load(id)
          @clazz.first(options_for_find.merge(:id => id))
        end

        # 
        # Get a collection of MongoMapper instances out of the database by ID
        #
        # ==== Parameters
        #
        # ids<Array>:: Database IDs of models to retrieve
        #
        # ==== Returns
        #
        # Array:: Collection of MongoMapper models
        #
        def load_all(ids)
          @clazz.all(options_for_find.merge(:id => ids))
        end
        
        private
        
        def options_for_find
          returning({}) do |options|
            options[:include] = @include unless @include.blank?
            options[:select]  =  @select unless  @select.blank?
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
crystal-ext-0.0.3 lib/sunspot/mongo_mapper/adapters.rb
crystal-ext-0.0.2 lib/sunspot/mongo_mapper/adapters.rb
crystal-ext-0.0.1 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.29 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.28 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.27 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.25 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.24 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.23 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.22 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.21 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.20 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.19 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.18 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.17 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.16 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.15 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.14 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.13 lib/sunspot/mongo_mapper/adapters.rb
rails-ext-0.3.12 lib/sunspot/mongo_mapper/adapters.rb