Sha256: 38c29e8c4a3a6a1a68e4ba4f854bce9747bce299682f77f8c622ec955bb9c8a0

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

require 'mongo_mapper'

module MongoMapper
  module Document
    module ClassMethods
      include OrmAdapter::ToAdapter
    end
    
    class OrmAdapter < ::OrmAdapter::Base
      # Do not consider these to be part of the class list
      def self.except_classes
        @@except_classes ||= []
      end

      # Gets a list of the available models for this adapter
      def self.model_classes
        ObjectSpace.each_object(Class).to_a.select {|klass| klass.ancestors.include? MongoMapper::Document}
      end

      # get a list of column names for a given class
      def column_names
        klass.column_names
      end

      # @see OrmAdapter::Base#get!
      def get!(id)
        klass.find!(wrap_key(id))
      end

      # @see OrmAdapter::Base#get
      def get(id)
        klass.first({ :id => wrap_key(id) })
      end

      # @see OrmAdapter::Base#find_first
      def find_first(conditions)
        conditions, order = extract_conditions_and_order!(conditions)
        conditions = conditions.merge(:sort => order) unless order.nil?
        klass.first(conditions_to_fields(conditions))
      end

      # @see OrmAdapter::Base#find_all
      def find_all(conditions)
        conditions, order = extract_conditions_and_order!(conditions)
        conditions = conditions.merge(:sort => order) unless order.nil?
        klass.all(conditions_to_fields(conditions))
      end

      # @see OrmAdapter::Base#create!
      def create!(attributes)
        klass.create!(attributes)
      end

    protected

      # converts and documents to ids
      def conditions_to_fields(conditions)
        conditions.inject({}) do |fields, (key, value)|
          if value.is_a?(MongoMapper::Document) && klass.key?("#{key}_id")
            fields.merge("#{key}_id" => value.id)
          else
            fields.merge(key => value)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
devise_sociable-0.1.0 vendor/bundle/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongo_mapper.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongo_mapper.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongo_mapper.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongo_mapper.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongo_mapper.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/orm_adapter-0.0.7/lib/orm_adapter/adapters/mongo_mapper.rb
orm_adapter-0.0.7 lib/orm_adapter/adapters/mongo_mapper.rb
orm_adapter-0.0.6 lib/orm_adapter/adapters/mongo_mapper.rb
orm_adapter-0.0.5 lib/orm_adapter/adapters/mongo_mapper.rb